Search code examples
pythonpython-imaging-librarycrop

Python PIL: How to save cropped image?


I have a script which creates an image and the it crops it. The problem is that after I call the crop() method it does not save on the disk

crop = image.crop(x_offset, Y_offset, width, height).load()
return crop.save(image_path, format)

Solution

  • You need to pass the arguments to .crop() in a tuple. And don't use .load()

    box = (x_offset, Y_offset, width, height)
    crop = image.crop(box)
    return crop.save(image_path, format)
    

    That's all you need. Although, I'm not sure why you're returning the result of the save operation; it returns None.