[Image-SIG] Re: Palette problem with ImageGrab

Alfred Milgrom fredm at smartypantsco.com
Sun Nov 16 19:23:01 EST 2003


Hi:

Thank you for offering to look at my problem.

>"setting the mode to 'P'" sounds a bit scary; the "mode" attribute
>shouldn't be modified.  (to convert between different modes, use
>the "convert" method).

Apologies for not being accurate in my language :(
I meant that I set the mode to 'P' by using the convert method.

>can you perhaps post some code?

Please find my sample code at the bottom of this post.

The output of this test program on my machine is as follows:

Information from grabbed image:
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


You will see that the image grabbed has 6 colours, but when I look at the 
palette information I get the 'gradated colours' palette:
(0,1,2), (3,4,5), ... (f4, f5, f6)

But after I save the image and load it back in, I get the correct colours 
(somewhat black, stippled yellow and somewhat white :)
So the original grabbed image has the correct palette information in it 
somewhere.

My question is: Am I doing something wrong? How can I get the palette 
information without having to save and reload the image?

Thanks in advance,
Fred Milgrom

----------------------------------------------------------------------------------------------------------

# 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 "Information from grabbed image:"
         self.colourinfo(im)

         path = os.getcwd()
         im.save(os.path.join(path, 'mytest.gif'))

         im = Image.open(os.path.join(path, 'mytest.gif'))
         print "Information from saved image:"
         self.colourinfo(im)

if __name__ == "__main__":
     board = GUI()





More information about the Image-SIG mailing list