Jump to content

Simple batch script iconvert


Recommended Posts

Should be simple but I don't know much about batch scripting

On Windows, I want to convert a folder with randomly named (some of the sequences, some not) images to rat with the same name but no numbers at the end. For example:

bla_3.hrd

bla_2.hdr

bla_-1.hdr

gthg.hrd

dumdeedum.txt

should become something like

bla_3_t.rat

bla_2_t.rat

bla_-1_t.rat

gthg.rat

How do I go about that?

Edited by Macha
Link to comment
Share on other sites

I wrote a little PowerShell script for this, take a look. Open it in any text editor, and edit first 3 variables. :rolleyes:

The only thing you need to do is to figure it out, how to run PowerShell scripts :) There is a permission issue.

It's easy to fix... link

convertRAT.rar

Edited by Stalkerx777
Link to comment
Share on other sites

For such purposes I use simple Shelf Tool with python code like this:

from os import listdir,path,system,environ
root = environ["HIP"]+"/textures"
root = "d:/imgs"
for file in listdir(root):
    file = path.join(root,file)
    system("iconvert "+file+" "+file[:-3]+"rat")

It's easy to modify the root folder with images or expression for filenames.

Link to comment
Share on other sites

Here is my shelf script for batch-converting images from within Houdini:

import hou
import os

iconvert = hou.houdiniPath()[3] + '/' + "iconvert.exe"
folder = hou.ui.selectFile(title='Images Folder')
files = os.listdir(folder)
dot = "."
extension = "rat"
exts = ('.exr', '.jpg', '.tga', '.png','.tif', '.hdr')

def rat_folder():
    for file in files:
        file_ext = file[-4:]

        if file_ext in exts:       
            export = iconvert + ' ' + '"' + folder + file + '"' + ' ' + '"' + folder + file[:-4] + dot + extension + '"'

            exists = os.path.exists(folder + file[:-4] + dot + extension)

            if exists is True:
                status = "File exists. Skipping..."
            else:
                os.system(export)
                status = "Success!"

            print "----------------------------------------------"
            print "RAT FOLDER STATUS: " + status
            print "original=" + folder + file
            print "new=" + folder + file[:-4] + dot + extension
            print "----------------------------------------------"

if folder != '':
    safety = hou.ui.displayMessage("Are you sure you want to convert all images in " + folder + " ?", buttons=('Yes', 'No'))

    if safety == 0 and folder != '':
        rat_folder()

Just paste it into a shelf button. When you press the button, a dialogue will appear that asks for the path. Just select the folder and not any file and press Accept. I have a second button that runs gconvert with extension = "bgeo" for batch object conversions.

  • Like 1
Link to comment
Share on other sites

  • 3 years later...

Here is my shelf script for batch-converting images from within Houdini:

 

import hou
import os

iconvert = hou.houdiniPath()[3] + '/' + "iconvert.exe"
folder = hou.ui.selectFile(title='Images Folder')
files = os.listdir(folder)
dot = "."
extension = "rat"
exts = ('.exr', '.jpg', '.tga', '.png','.tif', '.hdr')

def rat_folder():
    for file in files:
        file_ext = file[-4:]
        
        if file_ext in exts:       
            export = iconvert + ' ' + '"' + folder + file + '"' + ' ' + '"' + folder + file[:-4] + dot + extension + '"'
            
            exists = os.path.exists(folder + file[:-4] + dot + extension)
                
            if exists is True:
                status = "File exists. Skipping..."
            else:
                os.system(export)
                status = "Success!"

            print "----------------------------------------------"
            print "RAT FOLDER STATUS: " + status
            print "original=" + folder + file
            print "new=" + folder + file[:-4] + dot + extension
            print "----------------------------------------------"

if folder != '':
    safety = hou.ui.displayMessage("Are you sure you want to convert all images in " + folder + " ?", buttons=('Yes', 'No'))
        
    if safety == 0 and folder != '':
        rat_folder()

Just paste it into a shelf button. When you press the button, a dialogue will appear that asks for the path. Just select the folder and not any file and press Accept. I have a second button that runs gconvert with extension = "bgeo" for batch object conversions.

 

I've modified your script a little bit to accept files with extensions wich are longer or shorter then three symbols (ex. *.tx or *.tiff)

 

 

import hou

import os

iconvert = hou.houdiniPath()[3] + '/' + "iconvert.exe"

folder = hou.ui.selectFile(title='Images Folder')

files = os.listdir(folder)

dot = "."

extension = "rat"

exts = ('.exr', '.jpg', '.tga', '.png','.tif', '.hdr', '.tx')

def rat_folder():

    for file in files:

        file_ext = os.path.splitext(file)[1]

            

        

        if file_ext in exts:       

            export = iconvert + ' ' + '"' + folder + file + '"' + ' ' + '"' + folder + os.path.splitext(file)[0] + dot + extension + '"'

            

            exists = os.path.exists(folder + os.path.splitext(file)[0] + dot + extension)

                

            if exists is True:

                status = "File exists. Skipping..."

            else:

                os.system(export)

                status = "Success!"

            print "----------------------------------------------"

            print "RAT FOLDER STATUS: " + status

            print "original=" + folder + file

            print "new=" + folder + file[:-4] + dot + extension

            print "----------------------------------------------"

if folder != '':

    safety = hou.ui.displayMessage("Are you sure you want to convert all images in " + folder + " ?", buttons=('Yes', 'No'))

        

    if safety == 0 and folder != '':

        rat_folder()

 

Link to comment
Share on other sites

  • 9 months later...

I was trying to get this work on linux, but failed miserably, what should be changed if need to run on linux?

Please help.

 

Thanks

 

I've modified your script a little bit to accept files with extensions wich are longer or shorter then three symbols (ex. *.tx or *.tiff)

Link to comment
Share on other sites

  • 1 year later...

I changed the script to work on Linux. THANKS to Ole You only need to change the Path to your Houdini Linux Install(iconvert = "/opt/hfs16.0.600/bin/iconvert-bin")

 

import hou
import os

#the script converts all files in the same directory like your file selection


iconvert = "/opt/hfs16.0.600/bin/iconvert-bin"
file = hou.ui.selectFile(title="Image Selection")
folder = os.path.dirname(file)+"/"
files = os.listdir(folder)
dot = "."
extension = "rat"
exts = ('.exr', '.jpg', '.tga', '.png','.tif', '.hdr')

def rat_folder():
    for file in files:
        file_ext = file[-4:]

        if file_ext in exts:       
            export = iconvert + ' ' + '"' + folder + file + '"' + ' ' + '"' + folder + file[:-4] + dot + extension + '"'

            exists = os.path.exists(folder + file[:-4] + dot + extension)

            if exists is True:
                status = "File exists. Skipping..."
            else:
                os.system(export)
                status = "Success!"

            print "----------------------------------------------"
            print "RAT FOLDER STATUS: " + status
            print "original=" + folder + file
            print "new=" + folder + file[:-4] + dot + extension
            print "----------------------------------------------"

if folder != '':
    safety = hou.ui.displayMessage("Are you sure you want to convert all images in " + folder + " ?", buttons=('Yes', 'No'))

    if safety == 0 and folder != '':
        rat_folder()

Link to comment
Share on other sites

Update: i managed to make it running (hopefully) on all Operating systems not just Linux. Thanks to Symek and Legomyrstan

 

import hou
import os, sys

#the script converts all files in the same directory like your file selection

iconvert = hou.expandString("$HFS"+"/bin/"+"iconvert")

#iconvert = "/opt/hfs16.0.600/bin/iconvert-bin"
file = hou.ui.selectFile(title="Image Selection")
folder = os.path.dirname(file)+"/"
files = os.listdir(folder)
dot = "."
extension = "rat"
exts = ('.exr', '.jpg', '.tga', '.png','.tif', '.hdr')

def rat_folder():
    for file in files:
        file_ext = file[-4:]

        if file_ext in exts:       
            export = iconvert + ' ' + '"' + folder + file + '"' + ' ' + '"' + folder + file[:-4] + dot + extension + '"'

            exists = os.path.exists(folder + file[:-4] + dot + extension)

            if exists is True:
                status = "File exists. Skipping..."
            else:
                os.system(export)
                status = "Success!"

            print "----------------------------------------------"
            print "RAT FOLDER STATUS: " + status
            print "original=" + folder + file
            print "new=" + folder + file[:-4] + dot + extension
            print "----------------------------------------------"

if folder != '':
    safety = hou.ui.displayMessage("Are you sure you want to convert all images in " + folder + " ?", buttons=('Yes', 'No'))

    if safety == 0 and folder != '':
        rat_folder()

Link to comment
Share on other sites

  • 2 years later...

Hi,

Many thanks for sharing your code. None of the above versions works on Win 10, Houdini 17.5.258.

I have coded the snippet below that (hopefully) solves several version  & OS related problems.

I checked it on Windows only and works.

Hope you can find it useful, give me some feedback if you encounter problems.

import hou, os

iconvert = hou.getenv("HFS")  + "/bin/" +"iconvert.exe"
iconvert = os.path.abspath(iconvert)

folder = hou.ui.selectFile(title='Images Folder', file_type=hou.fileType.Directory)
folder = hou.expandString(folder)

files = os.listdir(folder)

dot = "."
extension = "rat"
exts = ('.exr', '.jpg', '.tga', '.png','.tif', '.hdr')

def rat_folder():
    for file in files:
        realName = os.path.abspath( folder + file)

        file_ext = realName[-4:]
        
        if file_ext in exts:
           
            export = iconvert + " " +  realName + " " + realName[:-4] + dot + extension 

            exists = os.path.exists(realName[:-4] + dot + extension)

            if exists is True:
                status = "File exists. Skipping..."
            else:
                os.system(export)
                status = "Success!"

            print "----------------------------------------------"
            print "RAT FOLDER STATUS: " + status
            print "original = " + realName
            print "new ="  + realName[:-4] + dot + extension
            print "----------------------------------------------"

if folder != '':
    safety = hou.ui.displayMessage("Are you sure you want to convert all images in " + folder + " ?", buttons=('Yes', 'No'))

    if safety == 0 and folder != '':
        rat_folder()

Kind regards,

Ippokratis

  • Thanks 1
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...