PIL crop and paste problem: Cropping doesn't create a cropped image

前端 未结 2 632
时光取名叫无心
时光取名叫无心 2020-12-20 03:41

I\'m trying to crop an image and then paste the cropped image into the centre of another image. Ideally I\'d like the cropped image to be smaller than the image its being pa

2条回答
  •  北海茫月
    2020-12-20 04:19

    The PIL documentation for the crop method states:

    Returns a rectangular region from the current image. The box is a 4-tuple defining the left, upper, right, and lower pixel coordinate.

    This is a lazy operation. Changes to the source image may or may not be reflected in the cropped image. To get a separate copy, call the load method on the cropped copy.

    So, you should try region = House.crop(box).load() to make sure you get an actual cropped copy.

    UPDATE:
    Actually, it seems the above only works if you're using PIL 1.1.6 and later. In versions before that, I guess load() doesn't return anything so you can't chain the operations. In that case, use:

    region = House.crop(box)
    region.load()
    

提交回复
热议问题