check the width and height of an image

python python-2.7 image-processing

3478 просмотра

1 ответ

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
Автор: mido Источник Размещён: 08.11.2019 11:15

Ответы (1)


2 плюса

Решение
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

Автор: be_good_do_good Размещён: 20.08.2016 01:44
Вопросы из категории :
32x32