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?
4 Answers
There is a crop()
method:
w, h = yourImage.size
yourImage.crop((0, 30, w, h-30)).save(...)

- 12,060
- 8
- 56
- 63

- 88,546
- 24
- 137
- 145
-
2Yes, I know that im.crop(box) is used for cropping the image. But I want to crop only upper and lower part of image not left and right, although box() take 4 tuple but I am not getting how to cropping the upper and lower part of image. – Taj Koyal Apr 02 '12 at 20:43
-
5@TajKoyal: Exactly what ninjagecko is showing you is how you crop off the top and bottom. He is specifying a rectangle for the new image. You can see that he shaves off 30 pixels from the y-value on the top and bottom points. If you offset the x values in any way, THAT would affect the left and right sides. – jdi Apr 02 '12 at 21:24
-
29For someone as lazy as me `Parameters: box – The crop rectangle, as a (left, upper, right, lower)-tuple.` – Rishav Dec 12 '18 at 18:29
You need to import PIL (Pillow) for this. Suppose you have an image of size 1200, 1600. We will crop image from 400, 400 to 800, 800
from PIL import Image
img = Image.open("ImageName.jpg")
area = (400, 400, 800, 800)
cropped_img = img.crop(area)
cropped_img.show()

- 1,754
- 15
- 12
(left, upper, right, lower) means two points,
- (left, upper)
- (right, lower)
with an 800x600 pixel image, the image's left upper point is (0, 0), the right lower point is (800, 600).
So, for cutting the image half:
from PIL import Image
img = Image.open("ImageName.jpg")
img_left_area = (0, 0, 400, 600)
img_right_area = (400, 0, 800, 600)
img_left = img.crop(img_left_area)
img_right = img.crop(img_right_area)
img_left.show()
img_right.show()
The Python Imaging Library uses a Cartesian pixel coordinate system, with (0,0) in the upper left corner. Note that the coordinates refer to the implied pixel corners; the centre of a pixel addressed as (0, 0) actually lies at (0.5, 0.5).
Coordinates are usually passed to the library as 2-tuples (x, y). Rectangles are represented as 4-tuples, with the upper left corner given first. For example, a rectangle covering all of an 800x600 pixel image is written as (0, 0, 800, 600).

- 5,325
- 1
- 14
- 23