• 0 Vote(s) - 0 Average
• 1
• 2
• 3
• 4
• 5
[Tut] How to Crop an Image Using PIL?

How to Crop an Image Using PIL?

## Problem Formulation

• Given an image stored at `image.jpeg`,
• a target `width` and `height` in pixels, and
• a target starting point (upper-left) `x` and `y` in the coordinate system.

How to crop the given image in Python PIL so that the resulting image has `width * height` size?

Here’s an example of how the original image is cropped to a smaller area from (100, 20) upper-left to (540, 210) bottom-right:

## Solution: img.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.

Here’s the example of how to crop an image with `width=440` and `height=190` pixels and upper-left starting points `x=100` and `y=20` pixels as shown in the graphic before.

```from PIL import Image # Given information
img = Image.open("image.jpg")
width, height = 440, 190
x, y = 100, 20 # Select area to crop
area = (x, y, x+width, y+height) # Crop, show, and save image
cropped_img = img.crop(area)
cropped_img.show()
cropped_img.save("cropped_image.jpg")```

You can play around with this example—including the original and cropped images shown here—in our interactive playground:

Here’s the original image:

And here’s the cropped image:

The post How to Crop an Image Using PIL? first appeared on Finxter.

https://www.sickgaming.net/blog/2021/03/...using-pil/

 Possibly Related Threads… Thread Author Replies Views Last Post [Tut] How to Display an Image as Grayscale in Python Matplotlib? xSicKxBot 0 8 04-11-2021, 09:01 PM Last Post: xSicKxBot [Tut] Python OpenCV Image Processing – Resize, Blend, Blur, Threshold, Convert xSicKxBot 0 22 03-23-2021, 12:13 PM Last Post: xSicKxBot [Tut] What’s the Best Way to Save Image Metadata Alongside a TIFF? xSicKxBot 0 30 03-10-2021, 11:07 PM Last Post: xSicKxBot

Forum Jump:

 Active Threads Unigine 2.14 Released Last Post: xSicKxBot Today 03:06 PM » Replies: 0 » Views: 2 News - Lawson Will Offer New Pokémon Sna... Last Post: xSicKxBot Today 03:06 PM » Replies: 0 » Views: 1 News - Nacon Announces WRC 10, Speeds On... Last Post: xSicKxBot Today 03:06 PM » Replies: 0 » Views: 1 News - Blizzard Arcade Collection Gets T... Last Post: xSicKxBot Today 03:06 PM » Replies: 0 » Views: 2 (Indie Deal) FREE Stranded In Time | XCO... Last Post: xSicKxBot Today 07:54 AM » Replies: 0 » Views: 8 Microsoft - 5 reasons to attend Azure St... Last Post: xSicKxBot Today 07:54 AM » Replies: 0 » Views: 7 News - Deals: Ring Fit Adventure Is Down... Last Post: xSicKxBot Today 07:54 AM » Replies: 0 » Views: 8 News - Cuphead Will Have An Awesome Deca... Last Post: xSicKxBot Today 07:54 AM » Replies: 0 » Views: 8 News - Nintendo Actually Responded To A ... Last Post: xSicKxBot Today 01:50 AM » Replies: 0 » Views: 9 News - Rumour: Dragon Ball Z: Kakarot Co... Last Post: xSicKxBot Today 01:50 AM » Replies: 0 » Views: 9