0

i'm able to resize each image one at a time,but how can I resize all images in one folder in a similar way? Please help me.

from PIL import Image
from resizeimage import resizeimage
    with open('test-image.jpeg', 'r+b') as f:
        with Image.open(f) as image:
            cover = resizeimage.resize_cover(image, [200, 100])
            cover.save('test-image-cover.jpeg', image.format)
xxxx
  • 1
  • 1
  • Welcome to StackOverflow. Looks like you've got some code that should do something - can you share an example of the output/error message that you are getting? That would help people to understand your problem. Also, it looks like you are using the `resizeimage` package - can you tell us what that is? There is a function within `PIL` to resize images - have you tried using that? – 4Oh4 Mar 08 '19 at 22:52
  • Have a look at this question: https://stackoverflow.com/questions/273946/how-do-i-resize-an-image-using-pil-and-maintain-its-aspect-ratio – 4Oh4 Mar 08 '19 at 22:52

2 Answers2

0

Just iterate over the files in the current directory

import os
from PIL import Image
from resizeimage import resizeimage

base = '/the/path'
for path in os.listdir(base):
    with Image.open(os.path.join(base, path)) as image:
        cover = resizeimage.resize_cover(image, [200, 100])
        cover.save(path, image.format)
Pablo Martinez
  • 449
  • 2
  • 6
0

Updated: Now uses os.listdir() instead of glob.glob() because of the need generate new file names from the original. Code now saves the resized images in same folder with its original file plus an added suffix.

Note that Image.open() wants a file path passed to it, not an opened file.

import os
from PIL import Image
from resizeimage import resizeimage

img_folder = '/path/to/img_folder'
fileext = '.jpg'
suffix = '_RESIZED'

for img_filename in os.listdir(img_folder):
    filename, ext = os.path.splitext(img_filename)

    if ext == fileext:
        print(filename, ext)
        src_img_filepath = os.path.join(img_folder, img_filename)
        dst_img_filepath = os.path.join(img_folder, filename+suffix, ext)

        with Image.open(src_img_filepath) as image:
            cover = resizeimage.resize_cover(image, [200, 100])
            cover.save(dst_img_filepath, image.format)
martineau
  • 119,623
  • 25
  • 170
  • 301
  • i don't know why, but it is giving out only one image as output,not all the images present in my folder – xxxx Mar 08 '19 at 23:03
  • That could happen if there's only one image file with the `.png` extension in the folder. You could try `*.*` to get all the files in the folder. – martineau Mar 08 '19 at 23:08
  • I have 30 images and all of them have .jpg extension, even then,I'm getting only one image as output – xxxx Mar 08 '19 at 23:17
  • The code, which I simply copied from yours, saves all of the resized images to the same output file named `'test-image-cover.jpeg'`. You need to change that—which could be done by adding a prefix or suffix to each one, **or** using the same file name, but save it in a different folder. – martineau Mar 08 '19 at 23:25