Certainly! You can concatenate (join) two cropped parts of an image using the Python Imaging Library (PIL), now known as Pillow. Below is a step-by-step guide on how to achieve this:
-
Install Pillow: If you haven't already, you need to install the Pillow library.
pip install pillow
-
Crop the Image: First, crop the two parts of the image you want to concatenate.
-
Concatenate the Cropped Parts: Use the Image.new() method to create a new image and then paste the cropped parts into this new image.
Here is a complete example:
from PIL import Image
def concatenate_images(image_path, crop_box1, crop_box2, direction='horizontal'):
"""
Concatenate two cropped parts of an image.
Parameters:
- image_path: Path to the original image.
- crop_box1: A tuple representing the crop box for the first part (left, upper, right, lower).
- crop_box2: A tuple representing the crop box for the second part (left, upper, right, lower).
- direction: 'horizontal' to concatenate horizontally, 'vertical' to concatenate vertically.
"""
# Open the original image
original_image = Image.open(image_path)
# Crop the first part
cropped_image1 = original_image.crop(crop_box1)
# Crop the second part
cropped_image2 = original_image.crop(crop_box2)
# Determine the size of the new image
if direction == 'horizontal':
new_width = cropped_image1.width + cropped_image2.width
new_height = max(cropped_image1.height, cropped_image2.height)
elif direction == 'vertical':
new_width = max(cropped_image1.width, cropped_image2.width)
new_height = cropped_image1.height + cropped_image2.height
else:
raise ValueError("Direction must be 'horizontal' or 'vertical'.")
# Create a new image with the calculated size
new_image = Image.new('RGB', (new_width, new_height))
# Paste the first cropped image
if direction == 'horizontal':
new_image.paste(cropped_image1, (0, 0))
new_image.paste(cropped_image2, (cropped_image1.width, 0))
elif direction == 'vertical':
new_image.paste(cropped_image1, (0, 0))
new_image.paste(cropped_image2, (0, cropped_image1.height))
return new_image
# Example usage
image_path = 'path/to/your/image.jpg'
crop_box1 = (0, 0, 100, 100) # Example crop box for the first part
crop_box2 = (100, 0, 200, 100) # Example crop box for the second part
# Concatenate horizontally
concatenated_image = concatenate_images(image_path, crop_box1, crop_box2, direction='horizontal')
concatenated_image.show() # Display the concatenated image
# Save the concatenated image
concatenated_image.save('path/to/save/concatenated_image.jpg')
Explanation:
-
Open the Original Image:
original_image = Image.open(image_path) opens the image file.
-
Crop the Images:
cropped_image1 = original_image.crop(crop_box1) crops the first part of the image.
cropped_image2 = original_image.crop(crop_box2) crops the second part of the image.
-
Determine the Size of the New Image:
- If concatenating horizontally, the new width is the sum of the widths of the cropped images, and the new height is the maximum height of the two cropped images.
- If concatenating vertically, the new height is the sum of the heights of the cropped images, and the new width is the maximum width of the two cropped images.
-
Create a New Image:
new_image = Image.new('RGB', (new_width, new_height)) creates a new image with the calculated size.
-
Paste the Cropped Images:
- Depending on the direction, the cropped images are pasted into the new image at the appropriate positions.
-
Display and Save the Concatenated Image:
concatenated_image.show() displays the concatenated image.
concatenated_image.save('path/to/save/concatenated_image.jpg') saves the concatenated image to a file.
Adjust the crop_box1 and crop_box2 tuples to specify the regions you want to crop from the original image.