How to Crop an Image Using Pil

Crop an image in the centre using PIL

Assuming you know the size you would like to crop to (new_width X new_height):

import Image
im = Image.open(<your image>)
width, height = im.size # Get dimensions

left = (width - new_width)/2
top = (height - new_height)/2
right = (width + new_width)/2
bottom = (height + new_height)/2

# Crop the center of the image
im = im.crop((left, top, right, bottom))

This will break if you attempt to crop a small image larger, but I'm going to assume you won't be trying that (Or that you can catch that case and not crop the image).

Crop the border of an image using PIL

img = Image.open('your_wonderful_image.png')
nonwhite_positions = [(x,y) for x in range(img.size[0]) for y in range(img.size[1]) if img.getdata()[x+y*img.size[0]] != (255,255,255)]
rect = (min([x for x,y in nonwhite_positions]), min([y for x,y in nonwhite_positions]), max([x for x,y in nonwhite_positions]), max([y for x,y in nonwhite_positions]))
img.crop(rect).save('out.png')

Crop an image in PIL using the 4 points of a rotated rectangle

If you start with this image:

Sample Image

You can do it like this using a QuadTransform:

#!/usr/bin/env python3

from PIL import Image, ImageTransform

# Open starting image and ensure RGB
im = Image.open('a.png').convert('RGB')

# Define 8-tuple with x,y coordinates of top-left, bottom-left, bottom-right and top-right corners and apply
transform=[31,146,88,226,252,112,195,31]
result = im.transform((200,100), ImageTransform.QuadTransform(transform))

# Save the result
result.save('result.png')

![enter image description here

How to crop the Image using Python PIL or CV?

from PIL import Image
from IPython.display import display, HTML
from IPython.display import Image as displayImage
img=Image.open('image.png')
imag_rgb = img.convert('RGB')
width, height = imag_rgb.size
print (width, height)
for pixel_x in range(width):
for pixel_y in range(height):
r, g, b = rgb_img.getpixel((pixel_x, pixel_y))
if(r == 255 and g == 255 and b == 255):
min_pixel_x = pixel_x
min_pixel_y = pixel_y
max_pixel_x = pixel_x
max_pixel_y = pixel_y
print (pixel_x, pixel_y)
cropped_image = img.crop((min_pixel_x, min_pixel_y,max_pixel_x + 1, max_pixel_y + 1))
#### +1 because of first x,y is 0,0
display(cropped_image)

PIL Image not cropping the way I want

You need to assign the result of the crop function to a variable. The image is not altered by the crop function, but the cropped version is returned, see also the docs, using this input:

Sample Image

And this modified code:

import PIL
from PIL import ImageEnhance
from PIL import Image

image = Image.open("img.jpg").convert("RGB")

contact_sheet = PIL.Image.new(image.mode,(1920,1080))

enhancer = ImageEnhance.Color(image)

images = []
current_location = 0
for i in range(10):
images.append(enhancer.enhance(i/10))

for img in images:
# Changed here slightly, current_slice is what we want to paste into the new image
current_slice = img.crop((current_location,0,current_location+192,0+1080))
contact_sheet.paste(current_slice,(current_location,0))
current_location+=192

#display(contact_sheet)
imagesave = contact_sheet.save("CroppedContactSheet.jpg")

We get this output:

Sample Image

Image taken from http://www.desktopwallpaperhd.net/view/raptor-sample-ford-auto-cartoon-205618.html

Addition:

I don't know if it was intended, but from your question it sounds like you wanted to go from enhancer.enhance(0) to enhancer.enhance(1), but in the code you provided you are going from 0 to 9/10, so the last slice is not at full color, but at 90%. If we change the code to enhancer.enhance(i/9), then the ouptut is:

Sample Image

Cropping an image with Python Pillow

The problem is with logic, not Pillow. Pillow is nearly 100% PIL compatible. You created an image of 0 * 0 (left = right & top = bottom) size. No display can show that. My code is as follows

from PIL import Image

test_image = "Fedora_19_with_GNOME.jpg"
original = Image.open(test_image)
original.show()

width, height = original.size # Get dimensions
left = width/4
top = height/4
right = 3 * width/4
bottom = 3 * height/4
cropped_example = original.crop((left, top, right, bottom))

cropped_example.show()

Most probably this is not what you want. But this should guide you towards a clear idea of what should be done.

Python PIL image crop increases file size

It looks like you're using a JPEG file extension, but actually saving as a BMP:

imCrop.save(f + 'Crop.jpg', "BMP", quality=50,optimize=True)

BMP isn't a very efficient format, but JPEG isn't good for this kind of image either. I suggest using a PNG:

imCrop.save(f + 'Crop.png', quality=50, optimize=True)


Related Topics



Leave a reply



Submit