473,402 Members | 2,050 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,402 software developers and data experts.

Setting the corner color in rotated PIL images

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?

All I really need in this case is that they be a solid color, the
same color they were before being rotated.

--
rzed
Jul 19 '05 #1
6 8482
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.

Frederic

----- Original Message -----
From: "rzed" <je***@comics.com>
Newsgroups: comp.lang.python
To: <py*********@python.org>
Sent: Sunday, May 01, 2005 1:17 PM
Subject: Setting the corner color in rotated PIL images

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?

All I really need in this case is that they be a solid color, the
same color they were before being rotated.

--
rzed
--
http://mail.python.org/mailman/listinfo/python-list


Jul 19 '05 #2
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" <je***@comics.com>
Newsgroups: comp.lang.python
To: <py*********@python.org>
Sent: Tuesday, May 03, 2005 8:13 PM
Subject: Re: Setting the corner color in rotated PIL images

"Anthra Norell" <an***********@tiscalinet.ch> wrote in
news:mailman.91.1115117893.6583.py*********@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


Jul 19 '05 #3
[Following up]
----- Original Message -----
From: "rzed" <je***@comics.com>
Newsgroups: comp.lang.python
To: <py*********@python.org>
Sent: Sunday, May 01, 2005 1:17 PM
Subject: Setting the corner color in rotated PIL images

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.

"Anthra Norell" <an***********@tiscalinet.ch> wrote in
news:mailman.91.1115117893.6583.py*********@python .org:
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.

Frederic


I found another method that doesn't require the larger size and
cropping :) but does require two copies of the original image :(
(sort of).

I copy the image and rotate the copy, then I create an all-white
image of the same size as the original and rotate it by the same
amount. Then I use ImageChops composite() to combine the rotated
copy, the original copy, and the black-and-white version
(parameters in that order). The black corners of the b/w version
serve as a mask to paste the original corners onto the copy.

It still seems like a lot of trouble to go to, but I don't think
there is a ready solution otherwise. I think there's a C-code
memset of all zeroes that underlies the black corners thing, and
that's not likely to change.

--
rzed
Jul 19 '05 #4
"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>

Jul 19 '05 #5
"Anthra Norell" <an***********@tiscalinet.ch> wrote in
news:ma*************************************@pytho n.org:
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.


[Code snipped]

Yes, that is essentially similar to the slow flood-fill approach I
used initially. I did in fact make use of your previous suggestion,
which works but requires oversizing the image, calculating the crop
rectangle and so on -- not overly difficult, just annoying -- and I
also use another approach (outlined in another message) that
involves pasting a rotated copy of the image back onto the original
under control of a mask. It depends on what I want to see in the
corners, essentially. And, having coded the workarounds, I get on
with the process without worrying about it.

But ... it would be nice if I could specify a default solid color
to replace the black in the corners, and have the rotation take
place in one operation without resizing and recalculating and
duplicating images and all.

Somewhere down in the C code, the "corner" color is being set to
black. I wouldn't think it would be terribly hard at that stage to
set those bytes to other values instead, and exposing that color
through PIL's interface. But I suppose it's more trouble than it's
worth for Fredrik, or nobody else has been bothered by it, or by
the lack of a flood-fill function. To me, these are
uncharacteristically odd omissions from PIL.

--
rzed
Jul 19 '05 #6
"Fredrik Lundh" <fr*****@pythonware.com> wrote in
news:ma*************************************@pytho n.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
Jul 19 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Tim Jarman | last post by:
Apologies in advance for the long post - I wanted to be sure I included all the relevant details. The answer is probably very, very simple. I am doing something stupid here, but I don't know what...
5
by: Roger Withnell | last post by:
I'm updating a record by opening a recordset, setting the fields and the updating it with objRS.Update. I need to set an image datatype to NULL. objRS("field") = NULL works for datatypes int...
1
by: kurt sune | last post by:
I am trying to do a rotated font. I get a handle alright which I convert to a font-object by Font2 = Font.FromHfont(handleFont) g.DrawString(Text-to draw, Font2, Brush, CurrentX, CurrentY) ...
7
by: Randy Vande Hei | last post by:
Does anyone know how to change the the GDI+ coordinate system like you used to be able to do in VB6 with the picturebox.scale method. The picturebox.scale method took an x,y point defining the...
8
by: johkar | last post by:
I have two problems I cannot work out in the following tab code with Netscape 6. Problems are marked with all cap comments. One is that the background image is not shown in NS 6 (two places in...
2
by: Tilo Pätzold | last post by:
Hi Everybody (especially Microsoft), we build EMF files with rotated text for export to office (powerpoint, word). It is planned that the text can be edited in the office document. Without the...
10
by: harish | last post by:
Hi everyone, How to make a round corner dorders using div? with images or without images. Pls help me! -- harish ------------------------------------------------------------------------...
7
by: halo combat22 | last post by:
I need help on a project i was assigned. I need a way to recognize these numbers. Any help is appreciated.
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.