Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to crop an image using PIL?

I want to crop image in the way by removing first 30 rows and last 30 rows from the given image. I have searched but did not get the exact solution. Does somebody have some suggestions?

like image 734
Taj Koyal Avatar asked Apr 02 '12 20:04

Taj Koyal


People also ask

How do I crop a picture in PIL?

crop() To crop an image to a certain area, use the PIL function Image. crop(left, upper, right, lower) that defines the area to be cropped using two points in the coordinate system: (left, upper) and (right, lower) pixel values. Those two points unambiguously define the rectangle to be cropped.

How do you crop part of an image in Python?

The crop() function of the image class in Pillow requires the portion to be cropped as rectangle. The rectangle portion to be cropped from an image is specified as a four-element tuple and returns the rectangle portion of the image that has been cropped as an image Object.

How do you crop a PNG in Python?

You can use PIL to find rows and cols of your image that are made up purely of your border color. Using this information, you can easily determine the extents of the inlaid image. PIL again will then allow you to crop the image to remove the border.


1 Answers

There is a crop() method:

w, h = yourImage.size yourImage.crop((0, 30, w, h-30)).save(...) 
like image 167
ninjagecko Avatar answered Oct 09 '22 18:10

ninjagecko