473,320 Members | 1,722 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,320 software developers and data experts.

Auto color selection PIL

Hi,

I'm trying to plot some points in an image. Each point has an
associating type and I'd like to have different colors (preferrably of
high contrast) for different types. The number of types in the data
file is unknown a priori. Is there a way to do this at runtime?

The "solution" I have so far has been to manually create a list of
10-20 contrasting colors and just go off that list at runtime. This
works most of the time since the number of types is usually less than
10. But I'd like a general solution.

Thank you.

Sep 27 '06 #1
5 1778
Xiaolei wrote:
I'm trying to plot some points in an image. Each point has an
associating type and I'd like to have different colors (preferrably of
high contrast) for different types. The number of types in the data
file is unknown a priori. Is there a way to do this at runtime?
How about:

from colorsys import hsv_to_rgb

def colors(n):
incr = 1.0 / n
hue = 0.0
for i in xrange(n):
r, g, b = hsv_to_rgb(hue, 1.0, 1.0)
yield int(r*255), int(g*255), int(b*255)
hue += incr
Sep 27 '06 #2
Xiaolei wrote:
I'm trying to plot some points in an image. Each point has an
associating type and I'd like to have different colors (preferrably of
high contrast) for different types. The number of types in the data
file is unknown a priori. Is there a way to do this at runtime?
If you don't know how many colors are needed even at run time, this code
might be helpful. But it generates colors that look similar pretty
quickly, so I wouldn't use it unless you have to. (Anyone know of a
better algorithm for this?)

from itertools import count
from colorsys import hsv_to_rgb

def hues():
yield 0.0
for i in count():
for j in xrange(2**i):
yield (1.0 / 2**(i+1)) + ((1.0 / 2**i) * j)

def colors():
for hue in hues():
r, g, b = hsv_to_rgb(hue, 1.0, 1.0)
yield int(r*255), int(g*255), int(b*255)
Sep 27 '06 #3
At Wednesday 27/9/2006 20:43, Leif K-Brooks wrote:
I'm trying to plot some points in an image. Each point has an
associating type and I'd like to have different colors (preferrably of
high contrast) for different types. The number of types in the data
file is unknown a priori. Is there a way to do this at runtime?

If you don't know how many colors are needed even at run time, this code
might be helpful. But it generates colors that look similar pretty
quickly, so I wouldn't use it unless you have to. (Anyone know of a
better algorithm for this?)
Try this. It first chooses 0, 1/2, then 1/4, 3/4, then */8...
It's the best I could make if you don't know the number of colors
beforehand. If you *do* know how many colors, your previous response is OK.
from colorsys import hsv_to_rgb

def hues():
denom = 1
yield 0
while True:
num = 1
while num<denom:
yield float(num)/denom
num += 2
denom += denom

def colors():
for hue in hues():
r, g, b = hsv_to_rgb(hue, 1.0, 1.0)
yield int(r*255), int(g*255), int(b*255)


Gabriel Genellina
Softlab SRL

__________________________________________________
Preguntá. Respondé. Descubrí.
Todo lo que querías saber, y lo que ni imaginabas,
está en Yahoo! Respuestas (Beta).
¡Probalo ya!
http://www.yahoo.com.ar/respuestas

Sep 28 '06 #4
Gabriel Genellina wrote:
Try this. It first chooses 0, 1/2, then 1/4, 3/4, then */8...
It's the best I could make if you don't know the number of colors
beforehand. If you *do* know how many colors, your previous response is OK.
Um, that's the same thing my second suggestion does:
>>h = hues()
h.next()
0.0
>>h.next()
0.5
>>h.next()
0.25
>>h.next()
0.75
>>h.next()
0.125

Your implementation is less terse than mine, though. And I suspect it
runs faster, though I haven't checked that.
Sep 28 '06 #5
Leif K-Brooks wrote:
Gabriel Genellina wrote:
>Try this. It first chooses 0, 1/2, then 1/4, 3/4, then */8...
It's the best I could make if you don't know the number of colors
beforehand. If you *do* know how many colors, your previous response
is OK.
I've no better suggestion than either of you, _but_ note that in
choosing colors for keys it is generally considered better ergonomics
to vary more than simply the hue if you don't want to penalize the
colorblind. Consider varying the other two parameters simultaneously
(perhaps in restricted ranges and varying orders); the contrast may be
more substantial even to a viewer with full color vision.

--
--Scott David Daniels
sc***********@acm.org
Oct 1 '06 #6

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

Similar topics

11
by: Mr. Smith | last post by:
Hello all, My code can successfully open, write to, format and save several worksheets in a workbook then save it by a given name, close and quit excel. My problem is that if I try and do it...
0
by: Waran | last post by:
I need to create a Auto suggests Textboox like in http://www.google.com/webhp?complete=1&hl=en I have completed this using AJAX.NET for Framework 1.1 . I have some design issues after the data is...
10
by: BNicholeN | last post by:
Hi I need to know how to get a form to auto populate, i.e. When I pick a city from Center Location, I would like the Operations Manger and SVP to populate once a selection is chosen. A related...
4
by: Mr. Wilson | last post by:
On this page (just a demo) I can’t get IE6 to auto-select the first item in the dynamically-generated drop-down menus. Well, that isn’t entirely true. Sometimes it works properly, but never on the...
4
by: scolivas | last post by:
I think this is a me thing. but can't remember how to do it. I have a form that I am using and would like for a txt box to automatically populate based on what is selected in a combo box. here...
4
by: magmike | last post by:
I've got a control on my form that allows the user to select a record based on a form field (in this example, the drop down menu shows the company name, followed by the contact name but uses the...
4
by: whamo | last post by:
I have the need to populate a field based on the selection in a combo box. Starting out simple. (2) tables tbl_OSE_Info and tbl_Input; tbl_OSE_Info has three fields: Key, OSE_Name and OSE_Wt...
1
by: BSTAFFORD | last post by:
I need help. I have a form with a 5 comboboxes. the form also contains a subform query with the saved records from those 5 combobox categories. What I'd like to happen is as a new record is...
2
by: Ronald | last post by:
I hope somebody can help. I can't get into the specifics of my project, but I'll try to create a simple example: tblVehicle * VIN (text box) * Make (text box) * Model (text box) frmRepair
6
by: dkyadav80 | last post by:
Hi sir, I'm new about xml, javascript. I have two selection field(html) first is city and second is state. the city and state values should be store in xml file. when user select city then all...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.