Image resizing script for python 2.6

sureronald 0 Tallied Votes 725 Views Share

An image resize script that I have written to scale images from a digital camera. I couldn't used the gimp (my favourite image editor) to scale each of them one by one.
This script takes two arguments which are both directory names. The first one is the directory containing images and the second one is the directory where the resized images are going to be stored. Adjust the global variables declared before the class imageResize to your liking.
Hope this script will be helpful to anyone wishing to go about resizing images in large scale.
Note that this class needs the PIL library and has been tested on python 2.6.

Happy times everyone!

Remember this is free software (as in freedom)!

#!/usr/bin/env python
"""

An image resize script that I have written to scale images from a digital camera. I couldn't used the gimp (my favourite image editor) to scale each of them one by one.
This script takes to arguments which are both directory names. The first one is the directory containing images and the second one is the directory where the resized images are going to be stored. Adjust the global variables declared before the class imageResize to your liking.
Hope this script will be helpful to anyone wishing to go about resizing images in large scale.

"""

__author__='Osure Ronald O. :: R - Labs (c) 2010'

from PIL import Image
import glob,os,sys

EXT_SUFFIX = '-rlabs' #Resized image file name suffix before extension name example, xxy-rlabs.png
SCALE_TO = 26 #Percentage to scale to
IMG_EXTENSIONS = ['*.jpg','*.JPG','*.png','*.PNG'] #Add your image extension types here

class imageResize():
	def resize(self,img_object,filename):
		size=self.getdimensions(img_object,filename)
		
		return img_object.resize(size,Image.ANTIALIAS)
	
	def getdimensions(self,img_object,filename):
		i_width,i_height=img_object.size
		new_width=int(round(SCALE_TO.__truediv__(100)*i_width))
		new_height=int(round(SCALE_TO.__truediv__(100)*i_height))
		
		print "%s resized from: (%d,%d) to -> (%d,%d)" % (filename,i_width,i_height,new_width,new_height) #Debug
		return (new_width,new_height)
	
	def __init__(self):
		if len(sys.argv) < 3:
			print "Expecting 2 arguments: /dir/with/pics/ /dir/to/store/new/pics"
			sys.exit(1)
		
		self.FILES=[]
		
		os.chdir(sys.argv[1])
		
		for w in IMG_EXTENSIONS:
			self.FILES.extend(glob.glob(w))
		
		path_to_store=os.path.abspath(sys.argv[2]) #Get absolute path

		#/dir/to/store/new/pics ends with a separator..? if not we append it
		if path_to_store[-1] != os.path.sep:
			path_to_store=path_to_store+os.path.sep
		
		for infile in self.FILES:
			try:
				img=Image.open(infile)
			except IOError:
				print "Could not open file: %s" % (infile)
				continue
			resized_img=self.resize(img,infile)
			filename,ext=os.path.splitext(infile)
			resized_img.save(path_to_store+filename.lower()+EXT_SUFFIX+ext)

if __name__=='__main__':
	imageResize()
skecher 0 Newbie Poster

Code Snippet Image resizing script for python 2.6

mymore 0 Newbie Poster

thank you for sharing your script. may i ask whether the image resizing and image scaling is the same thing. i am confused here. new beginner on image processing, hope to get some help!

Member Avatar for Budy_1
Budy_1

I wrote almost the same, but mine is just for resizing to square images. it can resize thousands of images

dzone.com/snippets/resize-thousands-images-python

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.