Re: PIL problem



On Oct 8, 10:39 am, bfrederi <brfrederi...@xxxxxxxxx> wrote:
On Oct 8, 10:30 am, Marc 'BlackJack' Rintsch <bj_...@xxxxxxx> wrote:



On Wed, 08 Oct 2008 08:10:02 -0700, bfrederi wrote:
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))

This doesn't change `image` but creates and returns a new cropped image
which you simply ignore.

    image.save(file_name, "JPEG")

Ciao,
        Marc 'BlackJack' Rintsch

How do I output it to an actual file then? Or overwrite the existing
file?

Nevermind, I gotcha. I needed to do this:

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

new_image = image.crop((x1,y1,x2,y2))
try:
new_image.save(file_name, "JPEG")
except IOError:
print "Cannot create square image for", file_name

I needed to output the cropped image by getting the cropped image in
"new_image" and outputting the new file. I see what you were saying.
.



Relevant Pages

  • Re: PIL problem
    ... 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: ... I needed to output the cropped image by getting the cropped image in ...
    (comp.lang.python)
  • Re: PIL problem
    ... 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: ... Marc 'BlackJack' Rintsch ...
    (comp.lang.python)
  • Re: PIL problem
    ... 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: ... Or overwrite the existing ...
    (comp.lang.python)
  • Re: Resizing Picture
    ... square and I need a 150 x 150 pixel shot for the web page. ... Left click/Drag a box around the portion you ... wish to crop. ... the selection frame and dragging the double ...
    (microsoft.public.publisher)
  • Re: Can we twist a digital picture to be square with the camera?
    ... Can we 'twist' the digital photo to be square with the camera? ... I need to crop the photo but I can NEVER get the ... edges to be exactly square with the digital camera photograph of the ... Therefore I always find myself losing a few millimeters of painting so ...
    (rec.photo.digital)