Re: [Image-SIG] Re: Returning Colormap values for an image?
by Alfred Milgrom other posts by this author
Aug 11 2004 4:33AM messages near this date
[Image-SIG] Re: jpg to PCL
|
[Image-SIG] pattern fill
Hi:
I am having problems in getting the colours for a grabbed image
(obtained using ImageGrab under Windows).
The following code illustrates my problem:
####################################################################
# Test of colour palette returned from ImageGrab
from Tkinter import *
import ImageGrab
import Image
import os
class GUI (Frame):
top = Tk()
def __init__(self, parent=top):
Frame.__init__(self,parent)
self.master.title('Grab Test')
self.Board = Canvas(self, width=200 , height=200 , bg="#FFF000")
self.Board.pack(side=TOP, padx=10, pady=10)
fButtons = Frame(self)
self.bGrab = Button(fButtons, width=15, text="Grab",
command=self.grabBoard)
self.bGrab.pack(side=LEFT, anchor=W, padx=10, pady=2)
self.bQuit = Button(fButtons, width=15, text="Quit",
command=self.top.destroy)
self.bQuit.pack(side=LEFT, anchor=W, padx=10, pady=2)
fButtons.pack(side=BOTTOM, fill=X)
self.pack()
self.Board.create_rectangle(70, 70, 90, 90, fill='white')
def colourinfo(self, im):
hist = im.histogram()
palette = im.palette
for item in hist:
coloursUsed = [i for i in range(len(hist)) if hist[i]]
palettestring = palette.tostring()
paletteUsed = [palettestring[3*i:3*i+3] for i in coloursUsed]
print "There are %s colours in this image" %len(coloursUsed)
print "Grabbed palette is:"
for item in paletteUsed:
for char in item:
print '%x' %ord(char),
print
def grabBoard(self):
x0 = self.Board.winfo_rootx()+2
y0 = self.Board.winfo_rooty()+2
x1 = x0 + 100
y1 = y0 + 100
im = ImageGrab.grab((x0, y0, x1, y1))
im = im.convert('P')
print "The grabbed image is in %s mode" %im.mode
self.colourinfo(im)
path = os.getcwd()
im.save(os.path.join(path, 'mytest.gif'))
im2 = Image.open(os.path.join(path, 'mytest.gif'))
print "\nInformation from saved image:"
self.colourinfo(im2)
if __name__ == "__main__":
board = GUI()
####################################################################
Basically I grab an image and use my function colourinfo to tell me
how many colours there are and what they are.
I then save the image and call colourinfo again.
The output is as follows:
The grabbed image is in P mode
There are 6 colours in this image
Grabbed palette is:
0 1 2
3 4 5
9 a b
75 76 77
87 88 89
f4 f5 f6
Information from saved image:
There are 6 colours in this image
Grabbed palette is:
0 0 0
1 1 1
3 3 3
ff cc 0
ff ff 0
fc fc fc
As you can see, the information from the grabbed image is basically
garbage data, whereas the saved image has the correct information.
How can I get the correct data from the grabbed image without having
to save it first?
Any help would be appreciated. (Apologies if this has previously been
answered. I have posted this problem previously but must have missed
the reply)
Thanks,
Alfred Milgrom
On Wed, 4 Aug 2004 10:03:09 +0200, Fredrik Lundh <fredrik@[...].com> wrote:
> "gohaku" wrote:
>
> > I would like to know if there is a function to return an array of an
> > Image's colormap.
>
> you want the colors in the palette? here's one way to do it:
>
> http://article.gmane.org/gmane.comp.python.general/347330
>
> </F>
>
>
>
>
> _______________________________________________
> Image-SIG maillist - Image-SIG@[...].org
> http://mail.python.org/mailman/listinfo/image-sig
>
_______________________________________________
Image-SIG maillist - Image-SIG@[...].org
http://mail.python.org/mailman/listinfo/image-sig
|