← Назад към блога
How to Crop an Image with Python
Cropping an image is one of the most common image-processing tasks. Whether you want to remove unwanted borders, focus on a specific area, or prepare images for machine learning, the **Pillow** library makes it simple. In this tutorial, we'll crop **100 pixels from each side** of an image. ## Prerequisites Install Pillow if you haven't already: ```bash pip install pillow ``` ## Python Code ```python from PIL import Image input_file = "input.png" output_file = "output.png" img = Image.open(input_file) width, height = img.size # Crop 100px from each side cropped = img.crop((100, 100, width - 100, height - 100)) cropped.save(output_file) print(f"Saved: {output_file}") ``` ## How It Works ### 1. Open the Image ```python img = Image.open(input_file) ``` This loads the image into memory. ### 2. Get the Image Size ```python width, height = img.size ``` The `size` property returns the image width and height in pixels. For example: ``` Width: 1920 Height: 1080 ``` ### 3. Crop the Image ```python cropped = img.crop((100, 100, width - 100, height - 100)) ``` The `crop()` method accepts a tuple in the following format: ```python (left, upper, right, lower) ``` Where: - **left** – X coordinate of the left edge - **upper** – Y coordinate of the top edge - **right** – X coordinate of the right edge - **lower** – Y coordinate of the bottom edge In this example: - Left = `100` - Top = `100` - Right = `width - 100` - Bottom = `height - 100` This removes **100 pixels** from every side of the image. If the original image is: ``` 1920 × 1080 ``` The cropped image becomes: ``` 1720 × 880 ``` ### 4. Save the Cropped Image ```python cropped.save(output_file) ``` The image is saved as: ``` output.png ``` ### 5. Print a Confirmation ```python print(f"Saved: {output_file}") ``` Output: ``` Saved: output.png ``` ## Understanding Pillow Coordinates Pillow uses a coordinate system where `(0, 0)` is located in the **top-left corner** of the image. ``` (0,0) +---------------------------+ | | | | | | | | +---------------------------+ (width,height) ``` The crop rectangle is defined as: ```python (left, upper, right, lower) ``` The left and upper boundaries are included, while the right and lower boundaries are excluded. ## Changing the Crop Amount To crop a different border size: ```python border = 50 cropped = img.crop(( border, border, width - border, height - border )) ``` This removes only **50 pixels** from each side. ## Cropping a Specific Region You can also crop any rectangular area. Example: ```python cropped = img.crop((300, 200, 900, 700)) ``` This extracts the area between: - X = 300 to 900 - Y = 200 to 700 ## Supported Image Formats Pillow supports many popular image formats, including: - PNG - JPEG - BMP - TIFF - GIF - WebP The output format is determined automatically from the file extension. ## Complete Example ```python from PIL import Image input_file = "input.png" output_file = "output.png" img = Image.open(input_file) width, height = img.size border = 100 cropped = img.crop(( border, border, width - border, height - border )) cropped.save(output_file) print("Original:", img.size) print("Cropped:", cropped.size) ``` Example output: ``` Original: (1920, 1080) Cropped: (1720, 880) Saved: output.png ``` ## Conclusion Cropping images with Pillow is simple and efficient. By defining the left, top, right, and bottom boundaries, you can quickly remove borders or extract specific regions from an image. Pillow's straightforward API makes it an excellent choice for image processing tasks in Python.