Jump to content
Macha

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

Share this post


Link to post
Share on other sites

i would get your hands on adobe bridge it has a really nice built in batch/sequence renaming tool (since you're in windows)

Share this post


Link to post
Share on other sites

for %i in (*.*) do iconvert %i %~ni.rat

hth.

petz

  • Like 1

Share this post


Link to post
Share on other sites

Thanks Petz. I'm also embarrassed now.

I should really learn that one day.

Share this post


Link to post
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

Share this post


Link to post
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.

Share this post


Link to post
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

Share this post


Link to post
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.

 

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()

 

Share this post


Link to post
Share on other sites

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)

Share this post


Link to post
Share on other sites

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()

Share this post


Link to post
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()

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×