image python
Florian Schmidt
schmidt_florian at gmx.de
Mon Oct 1 15:30:22 EDT 2007
On Mon, Oct 01, 2007 at 11:21:09AM -0700, oruccim at gmail.com wrote:
> hi
> I want understanding pictures colorfull
> for examle colorfull or black-white
> image.google.com there are understand it.
i'm not sure if i understand your question. assuming you want to decide if
an image has only grayscale or multi-channel color data you could use
the python image library
http://www.pythonware.com/products/pil/
example:
>>> from PIL import Image
>>> im = Image.open("gray.png")
>>> im.mode
'L'
>>> im = Image.open("color.jpeg")
>>> im.mode
'RGB'
>>> im = Image.open("another_gray.jpeg")
>>> im.mode
'L'
but this doesn't tell you if color.jpeg really uses non-gray colors...
if you really want to do an expensive search, if an RGB image contains
only gray colors, you could do something like that:
>>> im = Image.open("color.jpeg")
>>> im.mode
'RGB'
im.getcolors() returns a list like [(color_count, (red, green, blue)),
...]
to only select the channel values one could do:
>>> colors = set(lambda cv: cv[1], im.getcolors())
lets define a gray color as a color with all three color channels having the
same value like (30, 30, 30). to detect if a sequence has only equal items
one could create a set of this sequence and see how many items the set
contains afterwards:
>>> set((30, 30, 30))
set([30])
>> len(set((30, 30, 30)))
1
to check if an rgb image uses only grayscale colors we can do
>>> map(lambda cv: len(set(cv[1])), im.getcolors())
[1, 1, 1, 1, ....
the returned list contain only 1's if there are only gray values in the
image.
to easyly show this we can again create a set from this list:
>>> set(map(lambda cv: len(set(cv[1])), im.getcolors()))
set([1])
so "im" is an grayscale image even though it has three channels.
this method does not always work:
- with indexed image data,
- with another definition/understanding of "gray" pixels
but maybe PIL can help you anyway!
flo.
--
Florian Schmidt // www.fastflo.de
More information about the Python-list
mailing list