mido
mido

Reputation: 69

check the width and height of an image

i am trying to check the width and height of an image in a directory by using python . the directory consist of two folders and each folder there are pictures want to check the width and height to resize them if they are not matched. here is my code:

def Resize(imageFolder, factor_1, factor_2):


    for path, dirs, files in os.walk(imageFolder):
       for fileName in files:

         image = Image.open(fileName)
         image_size = image.size
         width = image_size[0]
         height = image_size[1]
         if ((width and height) == 224):
            print("the width and height are equals")

            continue
         print("the width and height are not equals, so we should resize it")    
        resize_pic(path, fileName, factor_1, factor_2)

when I run the code gives me error, i think the loop is not right. any help ?

 Traceback (most recent call last):

 File "resize.py", line 62, in <module>
 Resize(imageFolder, resizeFactor , resizeFactor_h)

 File "resize.py", line 46, in Resize
   image = Image.open(fileName)

 File "/usr/lib/python2.7/dist-packages/PIL/Image.py", line 2028, in open

 raise IOError("cannot identify image file")

 IOError: cannot identify image file

Upvotes: 0

Views: 4366

Answers (1)

be_good_do_good
be_good_do_good

Reputation: 4441

for (pth, dirs, files) in os.walk(imageFolder):
    for fileName in files:
        image = Image.open(fileName)
        with Image.open(os.path.join(pth, fileName)) as image:
            image_size = image.size

You need provide the absolute path of the file as well like above.

Also check on image file format

Upvotes: 2

Related Questions