You are currently viewing a single post of this guestblog.

Click here to go back the complete page. I would be glad to hear your comments, so don’t hesitate to leave me your feedback below. It will appear on the main guestblog page.

in categories  opensource  linux  permalink:  38   posted on 22.02.2011 14:15
From Yorik

Small utility to join alpha and diffuse images

The other day we bought a "people" texture pack from pro-viz. Great resource, I liked much their cool, contemporary, world-citizen people. Fits in almost any project in the world. The only small problem, the texture come in 2 separate image sets, one for diffuse and one for alpha. This is pretty common among alpha texture packs you can find on the net.

So I built up this little utility which scans a folder for alpha and diffuse subfolders, and joins both corresponding images into one .png image. It works on linux, probably out of the box on mac too (imagemagick must be installed), and supposedly on windows too if imagemagick's "convert" utility is in the search path.

import sys, os

args = sys.argv[1:]
if len(args) == 2:
    diffusedir = args[0]
    alphadir = args[1]
elif len(args) == 0:
    diffusedir = 'Diffuse'
    alphadir = 'Alpha'
    diffusedir = alphadir = None
    print '''                                                                              
        joinalpha [Diffuse Folder] [Alpha Folder] - joins alpha and diffuse images         
        If no folder is specified, defaults are "Alpha" and "Diffuse"                      

if diffusedir and alphadir:
    diffusedir = os.getcwd() + os.sep + diffusedir
    alphadir = os.getcwd() + os.sep + alphadir
    if os.path.exists(diffusedir) and os.path.exists(alphadir):
        alphanames = os.listdir(alphadir)
        diffusenames = os.listdir(diffusedir)
        if len(alphanames) == len(diffusenames):
             print 'joining ' + str(len(alphanames)) + ' files...'
             for i in range(len(alphanames)):
                f1 = diffusedir + os.sep + diffusenames[i]
                f2 = alphadir + os.sep + alphanames[i]
                fn = str(i+1).zfill(4) + '.png'
                options = '-colorspace RGB -alpha Off -compose copy_opacity -composite'
                execute = 'convert "' + f1 + '" "' + f2 + '" ' + options + ' "' + fn + '"'
                print 'creating ' + fn
            print "Alpha and Diffuse folder have a different number of files"
            print alphanames, len(alphanames), diffusenames, len(diffusenames)
        print "Couldn't find Alpha or Diffuse folders:",alphadir,diffusedir
    print "No Alpha and Diffuse directories specified, or no 'Alpha' and 'Diffuse' direcories in current folder"

First and foremost, your name:

And your message:

To publish it, just press this ...