473,756 Members | 8,108 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 8509
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.c om>
Newsgroups: comp.lang.pytho n
To: <py*********@py thon.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.c om>
Newsgroups: comp.lang.pytho n
To: <py*********@py thon.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.658 3.py*********@p ython.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.c om>
Newsgroups: comp.lang.pytho n
To: <py*********@py thon.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.658 3.py*********@p ython.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(a ngle)
bg.paste(im, im)
return bg

here's a more general solution:

def rotate(image, angle, color, filter=Image.NE AREST):
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(an gle, filter),
matte.rotate(an gle, filter)
)
return bg

</F>

Jul 19 '05 #5
"Anthra Norell" <an***********@ tiscalinet.ch> wrote in
news:ma******** *************** **************@ python.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
uncharacteristi cally odd omissions from PIL.

--
rzed
Jul 19 '05 #6
"Fredrik Lundh" <fr*****@python ware.com> wrote in
news:ma******** *************** **************@ 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(a ngle)
bg.paste(im, im)
return bg

here's a more general solution:

def rotate(image, angle, color, filter=Image.NE AREST):
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(an gle, filter),
matte.rotate(an gle, 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
6367
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 it is. I'm writing an application with a Tkinter GUI (Python 2.4, Tcl/Tk 8.4.) and I want to put a custom icon on the main window. I've followed (so far as I understand it) the recipe in the eff-bot's splendid Introduction to Tkinter - see:...
5
2947
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 and varchar but not for datatype image, although there is no error - the image field still contains the image! I'm using SQL server and running an UPDATE statement including image = NULL works.
1
2176
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) However the font doesnt get rotated. Anybody any tips? /k
7
17486
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 upper left corner and a second x,y point defining the lower right corner. In VB6 the 0,0 location was the upper left like in GDI+, but I could at least reset it so I could use it with lat/lons. Basically all I want to do is be able to use the...
8
1990
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 CSS). The second problem is that the top right rounded corner is not showing up in the Tier 2 tabs. Could you have a look and tell me if it is possible to correct? This code seems to work fine in IE 5+ and Firefox. Note that since I can't post...
2
5793
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 rotate transformation the text stays as a block and can be edited in the office document after ungrouping. But a rotated text leads to single charackters when ungrouping. The following sample code creates an emf file. The file can be dragged to...
10
3741
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 ------------------------------------------------------------------------ harish's Profile: http://www.highdots.com/forums/m11 View this thread: http://www.highdots.com/forums/t174579
7
2187
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
9456
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10040
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9873
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9713
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8713
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7248
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6534
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5142
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5304
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.