Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Resize thousands of images with Python

DZone's Guide to

Resize thousands of images with Python

· Java Zone
Free Resource

Microservices! They are everywhere, or at least, the term is. When should you use a microservice architecture? What factors should be considered when making that decision? Do the benefits outweigh the costs? Why is everyone so excited about them, anyway?  Brought to you in partnership with IBM.

Image resizer  

I created the script to resize thousands images in a single-run Python command in Linux (Ubuntu). This will also work in Windows i guess. It can resize thousands of images ( I run it before for > 50K images)

Suppose we have images stored in Folder structures like this : 

images

----  Tigers

-------------1_tigers.jpg

-------------2_tigers.jpg

-------------2_tigers.png

-------------n_tigers.jpg

---- Cats

-------------1_cats.jpg

-------------2_tcats.jpg

-------------2_cats.png

-------------n_cats.jpg

---- n

-------------1_n.jpg

-------------2_n.jpg

-------------3_n.png

-------------n_n.jpg

.............................

ans so on. 

Run the script with this steps : 

1. Open your terminal, and type

2. python resizer.py {img source folder} {the size you want the image to be} {destination folder}

   example : 

   python resizer.py /home/ubuntu/images/ 128 /home/ubuntu/new_images

3. Drink your coffee and watch the terminal running the process 

Notes: 
1. Install PIL image if you dont have one ( another library considered faster than this)
2. ANTIALIAS and quality=90 is to prevent the image from downgrading quality for Down-Scalling
3. This code works for Down-Scalling resize , yo can use another method beside ANTIALIAS for Free-Scalling
4. Images will be converted to .jpg extension ( its up to you, you can change it your way)
5. If you have different folder structures from above example, modify the code
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)

readf()

Discover how the Watson team is further developing SDKs in Java, Node.js, Python, iOS, and Android to access these services and make programming easy. Brought to you in partnership with IBM.

Topics:
python

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}