[IMAGE-SIG] [FYI & Q] Problems installing PIL and spectrogram

Harald Singer singer@itl.atr.co.jp
Tue, 17 Mar 1998 11:43:24 +0900


Fredrik Lundh wrote:
> 
> Oh, you obviously missed the patch kit and Skip's fixes to the
> palette attribute error posted to the image sig not long ago...
> 
> I've attached the patches, but you might as well wait until
> after the weekend: 0.3b1 (or maybe it's 0.3a4) will ship
> on Saturday or Sunday.

[1] 
Thanks for the info. I just noticed that
	http://www.pythonware.com/downloads.htm 
doesn't contain any mentioning of patch files. Do you have a public
place for patches?

[2] 
my palette problem was that I used
    image = Image.new("P",(128,64))
    image.im.putpalette("RGB", Colormap2Palette())
instead of 
    image = Image.new("P",(128,64))
    image.putpalette(Colormap2Palette(),"RGB")

It looks like my old version of PIL did not support 
image.putpalette, so now I have to do

    try:
	# 0.3a3 and later
	image.putpalette(Colormap2Palette(),"RGB")
    except:
	# before 0.3a3, but needs patch of ImageTk.py
	image.im.putpalette("RGB", Colormap2Palette())


[3] I still have a very serious problem: I am trying to put 
an image onto a tk button/label but the button/label stays grey.

	# this just gives me a grey button. why????
	Tkinter.Button(self,image=ImageTk.PhotoImage(image),command=self.quit).pack()

	# this (sometimes) works
	x = ImageTk.PhotoImage(image)
	Tkinter.Button(self,image=x,command=self.quit).pack()

However, when I use the same technique in a real program it doesn't
work, i.e. always grey. No warning messages and image.show(),
i.e. using xv, always works. Is this a timing problem?? Or do I make
some fundamental mistake using PIL with tcl/tk? (tcl/tk8.0p2,
python1.5 on Linux 2.0.32 slackware). I had no problem with
tcl7.4/tk4.2 :-(

[4] Here is the complete test script

#!/bin/sh
""":"
exec python $0 ${1+"$@"}
"""
""" 
  H. Singer, DEC97, ATR ITL
  preparation for spectrogram display for speech files
  requires PIL
"""
import string,os
import Image, ImageDraw, ImageTk  # from PIL

# a colormap
colormapjet =  """
         0         0    0.5625
         0         0    0.6250
         0         0    0.6875
         0         0    0.7500
         0         0    0.8125
         0         0    0.8750
         0         0    0.9375
         0         0    1.0000
         0    0.0625    1.0000
         0    0.1250    1.0000
         0    0.1875    1.0000
         0    0.2500    1.0000
         0    0.3125    1.0000
         0    0.3750    1.0000
         0    0.4375    1.0000
         0    0.5000    1.0000
         0    0.5625    1.0000
         0    0.6250    1.0000
         0    0.6875    1.0000
         0    0.7500    1.0000
         0    0.8125    1.0000
         0    0.8750    1.0000
         0    0.9375    1.0000
         0    1.0000    1.0000
    0.0625    1.0000    1.0000
    0.1250    1.0000    0.9375
    0.1875    1.0000    0.8750
    0.2500    1.0000    0.8125
    0.3125    1.0000    0.7500
    0.3750    1.0000    0.6875
    0.4375    1.0000    0.6250
    0.5000    1.0000    0.5625
    0.5625    1.0000    0.5000
    0.6250    1.0000    0.4375
    0.6875    1.0000    0.3750
    0.7500    1.0000    0.3125
    0.8125    1.0000    0.2500
    0.8750    1.0000    0.1875
    0.9375    1.0000    0.1250
    1.0000    1.0000    0.0625
    1.0000    1.0000         0
    1.0000    0.9375         0
    1.0000    0.8750         0
    1.0000    0.8125         0
    1.0000    0.7500         0
    1.0000    0.6875         0
    1.0000    0.6250         0
    1.0000    0.5625         0
    1.0000    0.5000         0
    1.0000    0.4375         0
    1.0000    0.3750         0
    1.0000    0.3125         0
    1.0000    0.2500         0
    1.0000    0.1875         0
    1.0000    0.1250         0
    1.0000    0.0625         0
    1.0000         0         0
    0.9375         0         0
    0.8750         0         0
    0.8125         0         0
    0.7500         0         0
    0.6875         0         0
    0.6250         0         0
    0.5625         0         0
"""

def Colormap2Palette(colormap=colormapjet):
    """ convert a matlab type colormap to a PIL palette """
    x = map(lambda x,colormap=colormap: int(string.atof(x)*255), string.split(colormap))
    palette = [(0,0,0)] * 256
    for i in range(len(x)/3):
	palette[i] = (x[i*3], x[i*3+1], x[i*3+2])
    palette = map(lambda a: chr(a[0])+chr(a[1])+chr(a[2]), palette)
    palette = string.join(palette, "")
    return palette

def _test(xvPreview=1):
    """ display the range of values in the palette """

    image = Image.new("P",(128,64))
    image.putpalette(Colormap2Palette(),"RGB")

    # create some data between 0 and 63
    data = map(lambda x: chr(x), range(0,64))
    data = string.join(data,'')
    data = data * 128

    image.fromstring(data)
    out = image.transpose(Image.ROTATE_90)

    if xvPreview:
	out.show()

    return out

import Tkinter
class Test(Tkinter.Frame):
    def __init__(self):
	Tkinter.Frame.__init__(self)
	Tkinter.Pack.config(self)

	image = _test()

	# this just gives me a grey button. why????
	# Tkinter.Button(self,image=ImageTk.PhotoImage(image),command=self.quit).pack()

	# this works
	x = ImageTk.PhotoImage(image)
	Tkinter.Button(self,image=x,command=self.quit).pack()

	self.mainloop()

if __name__ == '__main__':
    Test()
### EOF
-- 
Harald Singer, ATR ITL, tel +81-7749-5-1389   singer@itl.atr.co.jp



_______________
IMAGE-SIG - SIG on Image Processing with Python

send messages to: image-sig@python.org
administrivia to: image-sig-request@python.org
_______________