Last active
April 10, 2017 09:16
-
-
Save teknosains/8700df86678f04adcdec805e68b74b6e to your computer and use it in GitHub Desktop.
Resize thousand of images with Python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#tutorial (INA): http://teknosains.com/i/tutorial-me-resize-puluhan-ribu-gambar-sekaligus-dengan-python-ubuntu | |
#tutorial (EN): https://dzone.com/articles/resize-thousands-images-python | |
import PIL | |
from PIL import Image | |
import os | |
import sys | |
def readf(): | |
try: | |
input_dir = str(sys.argv[1].rstrip('/')) #path to img source folder | |
img_size = str(sys.argv[2]) #The image size (128, 256,etc) | |
output_dir = str(sys.argv[3].rstrip('/')) #output directory | |
print "starting...." | |
print "Colecting data from %s " % input_dir | |
tclass = [ d for d in os.listdir( input_dir ) ] | |
counter = 0 | |
strdc = '' | |
hasil = [] | |
for x in tclass: | |
list_dir = os.path.join(input_dir, x ) | |
list_tuj = os.path.join(output_dir+'/', x+'/') | |
if not os.path.exists(list_tuj): | |
os.makedirs(list_tuj) | |
if os.path.exists(list_tuj): | |
for d in os.listdir(list_dir): | |
try: | |
img = Image.open(os.path.join(input_dir+'/'+x,d)) | |
img = img.resize((int(img_size),int(img_size)),Image.ANTIALIAS) | |
fname,extension = os.path.splitext(d) | |
newfile = fname+extension | |
if extension != ".jpg" : | |
newfile = fname + ".jpg" | |
img.save(os.path.join(output_dir+'/'+x,newfile),"JPEG",quality=90) | |
print "Resizing file : %s - %s " % (x,d) | |
except Exception,e: | |
print "Error resize file : %s - %s " % (x,d) | |
sys.exit(1) | |
counter +=1 | |
except Exception,e: | |
print "Error, check Input directory etc : ", e | |
sys.exit(1) | |
#run it | |
if __name__ == '__main__': | |
readf() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment