bg

Using PIL Image to Crop Image with Respect to Center


Posted on April 24, 2022

_19
def crop_image()::
_19
im = Image.open('example.jpg')
_19
width, height = im.size
_19
if width > height:
_19
left = int((width - height) / 2)
_19
top = 0
_19
right = width - left
_19
bottom = height
_19
crop = im.crop((left, top, right, bottom))
_19
if height > width:
_19
left = 0
_19
top = int((height - width) / 2)
_19
right = width
_19
bottom = height-top
_19
crop = im.crop((left, top, right, bottom))
_19
# square
_19
else:
_19
return
_19
crop.save('example.jpg')

© cocdeshijie. All rights reserved.