fefinining % of c'm'y and k

John Lenton john at grulic.org.ar
Mon Jan 17 01:42:16 EST 2005

On Sun, Jan 16, 2005 at 09:57:46PM -0800, moshebg at shamaut.co.il wrote:
> hello
> i need a program (and pleas shoe me the modol in the softwar) that :
> if i have a scaned photo
> i want to define out of each poligon color ,as it seems in the photo,
> the cmyk
> in % (percets) of the color/
> 4 exampl from poligon color orang defin the cmyk in %
> like that: (example)
> c: 30%
> m:56%
> y:78%
> k: 10%
> moshe
> thanks

if I understand you correctly, then what you want to do is to
determine the amounts of each color an image has. I can think of two
ways of doing this, both using PIL; one faster,

  img = Image.open('foo.png')
  dot = img.resize((1,1),1)
  avg = dot.getpixel((0,0))
  for i in zip(dot.getbands(), avg):
      print "%s: %s" % i

and the other, thorougher

  img = Image.open('foo.png')
  width, height = img.size
  numdots = width*height
  avg = [sum(i)/numdots
         for i in zip(*[img.getpixel((x,y))
                        for x in xrange(width)
                        for y in xrange(height)])]
  for i in zip(dot.getbands(), avg):
      print "%s: %s" % i

the first is usually pretty close to the second, but YMMV.

Converting from these image-specific average values to CMYK is a
non-trivial problem (impossible in the general casew); see for example
where this issue was discussed.

John Lenton (john at grulic.org.ar) -- Random fortune:
Excellent day to have a rotten day.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 196 bytes
Desc: Digital signature
URL: <http://mail.python.org/pipermail/python-list/attachments/20050117/fd14b432/attachment.sig>

More information about the Python-list mailing list