PIL problem

J Kenneth King james at agentultra.com
Wed Oct 8 12:04:04 EDT 2008


bfrederi <brfredericks at gmail.com> writes:

> I am having a problem using PIL. I am trying to crop and image to a
> square, starting from the center of the image, but when I try to crop
> the image, it won't crop. Here are the relevant code snippets:
>
> ### Function I am testing ###
> def create_square_image(file_name):
>     """ Creates a thumbnail sized image and turns it into a square """
>     image = Image.open(open(file_name))
>
>     size_tuple = image.size
>     width = size_tuple[0]
>     height = size_tuple[1]
>
>     square_length = 75
>
>     x1 = (width / 2) - (square_length / 2)
>     x2 = x1 + square_length
>     y1 = (height / 2) - (square_length / 2)
>     y2 = y1 + square_length
>
>     image.crop((x1,y1,x2,y2))
>     image.save(file_name, "JPEG")

def create_square_image(filename, size=75):
    file = open(filename, 'rb')
    image = Image.open(file)
    w, h = image.size
    
    x1 = (w / 2) - (size / 2)
    x2 = x1 + size
    y1 = (h / 2) - (size / 2)
    y2 = y1 + size

    thumb = image.crop((x1,y1,x2,y2))
    thumb.save("thumb_" + filename, "JPEG")

...

of course PIL has a thumbnail method that does this type of stuff.



More information about the Python-list mailing list