[Image-SIG] Re: Returning Colormap values for an image?
Alfred Milgrom
milgrom at gmail.com
Thu Aug 12 09:07:05 CEST 2004
Thank you for your prompt answer to my problem. I am sorry to trouble
you further.
Unfortunately I think I am missing something as far as the
resize/convert technique is concerned. Basically I do not understand
why all the colours will be preserved if the image is resized, nor why
the colours will then be neatly arranged in the lut Image.
I have adapted my colourinfo function to use the resize/convert
technique, but I only get two different colours in the resulting
256-pixel image instead of 6.
I get the same difference in results from either the grabbed image or
the saved image.
Not only that, but I get different number of colours and colours after
I convert the image to P mode.
I would be grateful if you could point out what I am doing wrong.
Thanks in advance,
Alfred 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()
coloursUsed = len([x for x in hist if x])
print "There are %s colours in this image" %coloursUsed
print "Colours using resize/convert colours method:"
lut = im.resize((256,1)).convert("RGB").getdata()
print [x for x in lut if x not in locals()['_[1]'].__self__]
print "Colours using all data"
lut = im.convert("RGB").getdata()
print [x for x in lut if x not in locals()['_[1]'].__self__]
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))
print "The grabbed image is in %s mode" %im.mode
self.colourinfo(im)
im = im.convert('P')
print "\nThe grabbed image is now 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()
On Wed, 11 Aug 2004 15:34:59 +0200, Fredrik Lundh
<fredrik at pythonware.com> wrote:
> Alfred Milgrom wrote:
>
> > How can I get the correct data from the grabbed image without having
> > to save it first?
>
> the palette string may have different formats depending on the
> data source (the palette.rawmode attribute is used to describe
> the actual format).
>
> instead of peeking into the palette object, use the resize/convert
> technique described in this message:
>
>
>
> http://article.gmane.org/gmane.comp.python.general/347330
>
> </F>
>
> _______________________________________________
> Image-SIG maillist - Image-SIG at python.org
> http://mail.python.org/mailman/listinfo/image-sig
>
More information about the Image-SIG
mailing list