Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python/PIL Resize all images in a folder

I have the following code that I thought would resize the images in the specified path But when I run it, nothing works and yet python doesn't throw any error so I don't know what to do. Please advise. Thanks.

from PIL import Image import os, sys  path = ('C:\Users\Maxxie\color\complete')  def resize(): for item in os.listdir(path):     if os.path.isfile(item):         im = Image.open(item)         f, e = os.path.splitext(item)         imResize = im.resize((200,200), Image.ANTIALIAS)         imResize.save(f + ' resized.jpg', 'JPEG', quality=90)  resize() 
like image 290
user3237883 Avatar asked Feb 02 '14 23:02

user3237883


People also ask

How do I resize a whole image dataset in Python?

You can use python-resize-image.


1 Answers

#!/usr/bin/python from PIL import Image import os, sys  path = "/root/Desktop/python/images/" dirs = os.listdir( path )  def resize():     for item in dirs:         if os.path.isfile(path+item):             im = Image.open(path+item)             f, e = os.path.splitext(path+item)             imResize = im.resize((200,200), Image.ANTIALIAS)             imResize.save(f + ' resized.jpg', 'JPEG', quality=90)  resize() 

Your mistake is belong to full path of the files. Instead of item must be path+item

like image 60
Sanjar Stone Avatar answered Sep 29 '22 18:09

Sanjar Stone