Setting the corner color in rotated PIL images
Fredrik Lundh
fredrik at pythonware.com
Fri May 6 08:57:02 EDT 2005
"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>
More information about the Python-list
mailing list