Setting the corner color in rotated PIL images

Anthra Norell anthra.norell at tiscalinet.ch
Fri May 6 06:58:23 EDT 2005


What do you mean 'is required'? I tend to think that getting ahead with a
job is what is required. I don't sneer at work-arounds if they save time.

Frederic

A somewhat craftier solution, if still pretty hackish, would be to go
through your image pixel by pixel, look what color each one is (color =
image.getpixel (here)) and change the ones with the wrong color (if color ==
wrong_color: putpixel (here, right_color)).
      If the color of  the corners does not occur inside your picture, you
can go throught the entire image. Else you'd have to stop changing colors at
the first occurrence of a pixel that does not have the wrong color, coming
inward from each of the lateral edges. (Code below (untested)).
      If you have elements in your picture that not only have the same color
as the corners, but also run into them, then you might have to refine your
code further in order for the inner loop not stray into the image.

# Left edge
for y in range (image.size [1]):
   for x in range (image.size [0]):
      color = image.getpixel ((x,y))
      if color != WRONG_COLOR:
         break
      image.putpixel ((x,y), RIGHT_COLOR)

# Right edge
for y in range (image.size [1]):
   for x in range (image.size [0]-1), -1,  -1):
      color = image.getpixel ((x,y))
      if color != WRONG_COLOR:
         break
      image.putpixel ((x,y), RIGHT_COLOR)


----- Original Message -----
From: "rzed" <jello at comics.com>
Newsgroups: comp.lang.python
To: <python-list at python.org>
Sent: Tuesday, May 03, 2005 8:13 PM
Subject: Re: Setting the corner color in rotated PIL images


> "Anthra Norell" <anthra.norell at tiscalinet.ch> wrote in
> news:mailman.91.1115117893.6583.python-list at python.org:
>
> [in response to:
> >> I'm using PIL to generate some images which may be rotated at
> >> the user's option. When they are rotated, the original image is
> >> cropped in the new image (which is fine), and the corners are
> >> black (which is not, in this case). I can't find any documented
> >> way to change the default fill color (if that's what it is) for
> >> the corners, and PIL also doesn't seem to support a flood fill.
> >> I have created a flood fill in Python, which works but which
> >> markedly slows image generation.]
>
> > I just had the same problem the other day. I solved it by
> > starting out with an image large enough to retain enough white
> > area following the rotation.
>
> Well, that's a workaround I could try, but I'm half-hearted about
> it. I don't like to think that it's *required*. Another possible
> solution is to make the outer portion black, so the rotation seems
> to do the right things, but in the cases I'm dealing with, that's
> either out or more trouble than it's worth. I can haul the rotated
> images into a paint program and manually touch up the corners, too,
> but I don't like to have to do that either.
>
> It seems strange that there wouldn't be some way to change the
> black to another color, or (maybe just as good) to transparent. PIL
> is so useful that it strikes me as an aberrant oversight. More
> likely, there *is* a better way, but I just don't know it and can't
> find it in the docs.
>
> --
> rzed
>
> --
> http://mail.python.org/mailman/listinfo/python-list




More information about the Python-list mailing list