Setting the corner color in rotated PIL images
rzed
jello at comics.com
Tue May 10 19:07:18 EDT 2005
"Fredrik Lundh" <fredrik at pythonware.com> wrote in
news:mailman.97.1115384328.29826.python-list at python.org:
> "rzed" wrote:
>
>> 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.
>>
>> Can anyone suggest a better way to set the color of the
>> corners?
>
> if you're doing this on RGB images, the quickest way to do this
> is:
>
> def rotate(image, angle, color):
> bg = Image.new("RGB", image.size, color)
> im = image.convert("RGBA").rotate(angle)
> bg.paste(im, im)
> return bg
>
> here's a more general solution:
>
> def rotate(image, angle, color, filter=Image.NEAREST):
> if image.mode == "P" or filter == Image.NEAREST:
> matte = Image.new("1", image.size, 1) # mask
> else:
> matte = Image.new("L", image.size, 255) # true matte
> bg = Image.new(image.mode, image.size, color)
> bg.paste(
> image.rotate(angle, filter),
> matte.rotate(angle, filter)
> )
> return bg
>
> </F>
>
Fredrik:
Thank you for the reply. It just showed up on my server, and, of
course, it works perfectly.
--
rzed
More information about the Python-list
mailing list