Image Conversion Tool

Miscellaneous Forums/General Discussion/Image Conversion Tool

Hi,

I'd like to automate part of my graphics processing pipeline. Basically I have scores of PNG files that need to be converted to a several other widths. I'm looking for an image conversion tool that:

- Runs under Windows XP
- Can be controlled entirely through the command line
- Processes a set of files from one directory eg. dir1/*.png
- Resizes the images to a given width while retaining the aspect ratio eg. -w200
- Preserves the Alpha channel
- Writes the results with the same file name to a different directory eg. -odir2

The multiple output widths could be separate invocations in a script file.

Other options like converting to different formats would be nice but not essential.

Free or inexpensive tools only please.

Any suggestions based on actual use ?

Thanks.

Sounds like something that could be put together in an hour in BlitzMax?

Irfanview should do the trick for the majority of those, albeit I'm unsure about the diff directory thing.

ImageMagick is very good too.

An hour? More like 20 minutes.

GENEXI2 - Thanks. I'll take a look.

ERVIN - I tried ImageMagick already thanks. Can't seem to scale images to a specified width and let the height adjust according to the source aspect ratio.

As to the other suggestions to write it myself - sure I could but I have a rule to always look for existing full featured tools before writing my own utility. Otherwise you end up building and maintaining tools rather than the actual product.

If I can't find something today then I will write it myself either in BlitzMax or C#.

Python and the Python Imaging Library.

Would take you all of 2 minutes to write a program to do that, if someone else hasn't already.

Actually I recently wrote a script to do something like that. It was designed to be run as a cron job, but since you don't need that you can just remove all the lockfile junk (which won't work on Windows anyhow).
#!/usr/bin/python

from PIL import Image
import sys, os, shutil

LOCKFILE = u"./imgconv.lock"

class ImgConv:

	def setParams(self):
		try:
			index = sys.argv.index("--sourceDir")
			self.src_dir = os.path.join(sys.argv[index + 1] , "%s")
		except ValueError:
			self.src_dir = "./%s"
		
		try:
			index = sys.argv.index("--destDir")
			self.dest_dir = os.path.join(sys.argv[index + 1] , "%s")
		except ValueError:
			self.dest_dir = "./%s"
	
		try:
			index = sys.argv.index("--maxHeight")
			self.maxHeight = sys.argv[index + 1]
		except ValueError:
			self.maxHeight = 600
	
		try:
			index = sys.argv.index("--maxWidth")
			self.maxWidth = sys.argv[index + 1]
		except ValueError:
			self.maxWidth = 800
	
	
	def rescaleImage(self , imageName):
		myImage = Image.open(self.src_dir % imageName)
		myImage.thumbnail((self.maxWidth , self.maxHeight) , Image.ANTIALIAS)
		myImage.save(self.dest_dir % imageName , quality=100)

def main():
	if len(sys.argv) > 1:
		myIC = ImgConv()
		myIC.setParams()
		files = os.listdir(myIC.src_dir % "")
		for each in files:
			if each.lower().endswith(u".jpg"):
				xmlfile = each.replace(u".jpg" , u".xml")
				if os.path.isfile( myIC.src_dir % xmlfile):
					myIC.rescaleImage( each )
					shutil.copy( myIC.src_dir % xmlfile , myIC.dest_dir % xmlfile )
					os.unlink(myIC.src_dir % each)
					os.unlink(myIC.src_dir % xmlfile)
	else:
		print "imagecon.py - a batch image resampling script."
		print "Copyright 2007 Virksomheds-IT"
		print "Useage: imgconv.py [OPTIONS]"
		print"\t--sourceDir: The directory where all source images and their corresponding xml files are dumped (default = ./)"
		print"\t--destDir:   The directory where all the converted images and their xml is copied to (default = ./)"
		print"\t--maxWidth:  The maximum width of the destination image (default = 800)"
		print"\t--maxHeight: The maximum height of the destination image (default = 600)"

if __name__ == "__main__":
    try:
        fd = os.open(LOCKFILE, os.O_CREAT+os.O_EXCL)
    except OSError:
        sys.exit(1)
    try:
        main()
    finally:
        os.remove(LOCKFILE)