9951 explained code solutions for 126 technologies


python-pillowHow to crop an image in the center


from PIL import Image, ImageDraw, ImageFont

im = Image.open('/var/www/examples/heroine.png')
crop = (400, 400)
left = round((im.size[0] - crop[0])/2)
top = round((im.size[1] - crop[1])/2)
im = im.crop((left, top, crop[0]+left, crop[1] + top))

im.show()ctrl + c
PIL

import Pillow package modules

Image.open

open given image with Pillow

/var/www/examples/heroine.png

path to sample image to open

.crop(

crops image to a given area

(im.size[0]

crop[0])/2` - left coordinate of a crop area to position in the horizontal center

(im.size[1]

crop[1])/2` - top coordinate of a crop area to position in the vertical center

.show()

displays resulting image

(400, 400)

cropping area width and height


How to crop an image in the center, python pillow

Usage example

from PIL import Image, ImageDraw, ImageFont

im = Image.open('/var/www/examples/heroine.png')
crop = (400, 400)
left = round((im.size[0] - crop[0])/2)
top = round((im.size[1] - crop[1])/2)
im = im.crop((left, top, crop[0]+left, crop[1] + top))

im.show()