CodeGym /Courses /Python SELF EN /Image Cropping and Rotation for Content Prep

Image Cropping and Rotation for Content Prep

Python SELF EN
Level 45 , Lesson 2
Available

1. Image Cropping

What is Cropping and How Do We Use It?

Alright, let’s dive into how to crop and rotate images. This skill is super handy for a bunch of different projects—from prepping graphics for websites to creating content for social media.

Cropping an image is like cutting off the unnecessary edges of a photo so you can focus on the main subject. Imagine snapping an awesome sunset pic, but there are random curious onlookers at the edges. Cropping helps you get rid of them (virtually, of course) without any drama!

The crop() Method

The crop() method takes the coordinates of the rectangular area you want to cut out of the image. It’s done using four values: (left, top, right, bottom).


# Cropping the image
cropped_image = image.crop((100, 100, 400, 400))

# Saving the cropped image
cropped_image.save("cropped_example.jpg")
        

In this example, the image will be cropped to an area with the top-left corner at (100, 100) and the bottom-right corner at (400, 400). This method creates a new image from the specified part of the original image.

Dynamic Cropping Based on Image Dimensions

If you want to crop the image to its center, you can calculate the crop coordinates based on its dimensions:


width, height = image.size
left = (width - 200) / 2
top = (height - 200) / 2
right = (width + 200) / 2
bottom = (height + 200) / 2

center_cropped_image = image.crop((left, top, right, bottom))
center_cropped_image.save("center_cropped_example.jpg")
        

This code crops a 200x200 pixel area at the center of the image. The coordinate calculations ensure the cropped area is perfectly centered.

2. Image Rotation

Rotating an image lets you change its orientation, which is especially useful if a photo was taken with the wrong orientation or you want to add some artistic flair.

The rotate() Method

The rotate() method rotates an image by the specified angle counterclockwise. The angle is given in degrees, and the method creates a new rotated image.


# Rotating an image 90 degrees counterclockwise
rotated_image = image.rotate(90)
rotated_image.save("rotated_90_example.jpg")
        

In this example, the image will be rotated 90 degrees counterclockwise. You can also use other angles (like 45, 180, or 270 degrees) to achieve your desired effect.

Handling White Borders When Rotating

By default, rotate() adds white borders (or transparent ones if using RGBA mode) around the image to preserve all its content. If you want the image to fill the entire frame without borders, use the expand=True parameter.


# Rotate with expansion to fill the entire frame
rotated_expanded_image = image.rotate(45, expand=True)
rotated_expanded_image.save("rotated_expanded_example.jpg")
        

In this case, Pillow will enlarge the image dimensions to fit all its content, which is useful for rotations at non-standard angles (like 45 degrees).

3. Mirroring an Image

Mirroring flips an image horizontally or vertically. This is handy when you want to change the direction of an object in the photo or create a symmetrical effect.

The transpose() Method

The transpose() method is used for mirroring and supports the following parameters:

  • Image.FLIP_LEFT_RIGHT: Horizontal flip.
  • Image.FLIP_TOP_BOTTOM: Vertical flip.

# Horizontal flip
flipped_horizontal = image.transpose(Image.FLIP_LEFT_RIGHT)
flipped_horizontal.save("flipped_horizontal_example.jpg")

# Vertical flip
flipped_vertical = image.transpose(Image.FLIP_TOP_BOTTOM)
flipped_vertical.save("flipped_vertical_example.jpg")
        

These commands flip the image horizontally and vertically, which you can use for various effects or to fix orientation.

Practical Examples of Cropping, Rotating, and Mirroring

  1. Focusing on Specific Areas of an Image: Cropping lets you highlight the part of the image you need, which is useful for portraits, cutting out unnecessary details, or prepping images for social media or websites.
  2. Fixing Image Orientation: Rotation is used to fix the orientation of a photo, especially if the device captured it incorrectly. For example, fixing a landscape-oriented photo for posting in portrait orientation.
  3. Creating Symmetrical Effects: Mirroring is often used in design to create symmetrical images, interesting visual effects, and improved compositions.

4. Common Mistakes and How to Avoid Them

While working with Pillow, you might run into some common mistakes, especially if you’re just starting out. One example is trying to crop an image outside its actual dimensions. Always check the size of your image and make sure the crop coordinates stay within those bounds.

Another thing to keep in mind—when rotating, the image might look cropped if you use the rotate method without extra settings. This happens because of resizing during the rotation. To prevent this, use the expand=True parameter, which automatically adjusts the dimensions:


# Rotating the image 45 degrees with expansion to avoid cutting corners
rotated_image = image.rotate(45, expand=True)
rotated_image.save("rotated_expanded.jpg")
        
1
Task
Python SELF EN, level 45, lesson 2
Locked
Basic Image Cropping
Basic Image Cropping
2
Task
Python SELF EN, level 45, lesson 2
Locked
Rotate an image by a specified angle
Rotate an image by a specified angle
3
Task
Python SELF EN, level 45, lesson 2
Locked
Mirroring an Image
Mirroring an Image
4
Task
Python SELF EN, level 45, lesson 2
Locked
Combined Image Processing
Combined Image Processing
Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION