Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python PIL Crop all Images in a Folder

Currently I am trying to crop all images inside a folder under the address of: C:\\Users\\xie\\Desktop\\tiff\\Bmp and then resave them into the same folder. Below is the code I am trying to experiment with, both run without error but does nothing. Also note I am using windows as platform.

Code 1:

from PIL import Image
import os.path, sys

path = "C:\\Users\\xie\\Desktop\\tiff\\Bmp"
dirs = os.listdir(path)

def crop():
    for item in dirs:
        if os.path.isfile(path+item):
            im = Image.open(path+item)
            f, e = os.path.splitext(path+item)
            imCrop = im.crop(30, 10, 1024, 1004)
            imCrop.save(f + 'Cropped.bmp', "BMP", quality=100)

crop()

Code 2:

for f in os.listdir("C:\\Users\\xie\\Desktop\\tiff\\Bmp"):
    for f in ("C:\\Users\\xie\\Desktop\\tiff\\Bmp"):
        if f.endswith('.bmp'):
            print (f, end=" ")
            i = Image.open(f)
            area = (30, 10, 1024, 1004)
            cropped_i = i.crop(area)
            cropped_i.show()
            cropped_i.save('Cropped{}.bmp', "BMP", quality=100, optimize=True)

Thanks, any help or suggestions are greatly appreciated!

like image 662
RayX Avatar asked Dec 13 '17 05:12

RayX


People also ask

How do I crop multiple images at once in Python?

You can resize multiple images in Python with the awesome PIL library and a small help of the os (operating system) library. By using os. listdir() function you can read all the file names in a directory. After that, all you have to do is to create a for loop to open, resize and save each image in the directory.

How do I resize a folder of pictures?

Select all of the photos that you need to resize. Right-click them and choose “Open with Preview”. When you are in Preview, click on “Edit” and then choose “Select All”. After all the pictures are selected, head up to “Tools” and select “Adjust Size”.

How do you crop an image using Python PIL?

crop() method is used to crop a rectangular portion of any image. Parameters: box – a 4-tuple defining the left, upper, right, and lower pixel coordinate. Return type: Image (Returns a rectangular region as (left, upper, right, lower)-tuple).


1 Answers

Code 1 : Corrected

This is your corrected code, you almost had it right, you have to join the path correctly, in your code you weren't adding a separator / between the path and the filename. by using os.path.join you can combine a directory path and a filename.

Furthermore, crop takes a tuple of 4, not 4 arguments.

from PIL import Image
import os.path, sys

path = "C:\\Users\\xie\\Desktop\\tiff\\Bmp"
dirs = os.listdir(path)

def crop():
    for item in dirs:
        fullpath = os.path.join(path,item)         #corrected
        if os.path.isfile(fullpath):
            im = Image.open(fullpath)
            f, e = os.path.splitext(fullpath)
            imCrop = im.crop((30, 10, 1024, 1004)) #corrected
            imCrop.save(f + 'Cropped.bmp', "BMP", quality=100)

crop()
like image 101
user1767754 Avatar answered Sep 22 '22 01:09

user1767754