2 ImageMac.py by Trocca Riccardo (rtrocca@libero.it)
3 This module provides functions to display images and Numeric arrays
4 It provides two classes ImageMacWin e NumericMacWin and two simple methods showImage and
8 showImage(Image,"optional window title",zoomFactor)
9 the same for showNumeric
10 zoomfactor (defaults to 1) allows to zoom in the image by a factor of 1x 2x 3x and so on
11 I did't try with a 0.5x or similar.
12 The windows don't provide a scrollbar or a resize box.
13 Probably a better solution (and more similar to the original implementation in PIL and NumPy)
14 would be to save a temp file is some suitable format and then make an application (through appleevents) to open it.
15 Good guesses should be GraphicConverter or PictureViewer.
17 However the classes ImageMacWin e NumericMacWin use an extended version of PixMapWrapper in order to
18 provide an image buffer and then blit it in the window.
20 Being one of my first experiences with Python I didn't use Exceptions to signal error conditions, sorry.
25 from ExtPixMapWrapper
import *
30 class ImageMacWin(W
.Window
):
32 def __init__(self
,size
=(300,300),title
="ImageMacWin"):
33 self
.pm
=ExtPixMapWrapper()
36 W
.Window
.__init
__(self
,size
,title
)
38 def Show(self
,image
,resize
=0):
39 #print "format: ", image.format," size: ",image.size," mode: ",image.mode
40 #print "string len :",len(image.tostring())
41 self
.pm
.fromImage(image
)
44 self
.size
=(image
.size
[0]*resize
,image
.size
[1]*resize
)
45 W
.Window
.do_resize(self
,self
.size
[0],self
.size
[1],self
.wid
)
51 Qd
.RGBForeColor( (0,0,0) )
52 Qd
.RGBBackColor((65535, 65535, 65535))
53 Qd
.EraseRect((0,0,self
.size
[0],self
.size
[1]))
56 self
.pm
.blit(0,0,self
.size
[0],self
.size
[1])
58 def do_update(self
,macoswindowid
,event
):
62 class NumericMacWin(W
.Window
):
64 def __init__(self
,size
=(300,300),title
="ImageMacWin"):
65 self
.pm
=ExtPixMapWrapper()
68 W
.Window
.__init
__(self
,size
,title
)
70 def Show(self
,num
,resize
=0):
71 #print "shape: ", num.shape
72 #print "string len :",len(num.tostring())
73 self
.pm
.fromNumeric(num
)
76 self
.size
=(num
.shape
[1]*resize
,num
.shape
[0]*resize
)
77 W
.Window
.do_resize(self
,self
.size
[0],self
.size
[1],self
.wid
)
83 Qd
.RGBForeColor( (0,0,0) )
84 Qd
.RGBBackColor((65535, 65535, 65535))
85 Qd
.EraseRect((0,0,self
.size
[0],self
.size
[1]))
88 self
.pm
.blit(0,0,self
.size
[0],self
.size
[1])
90 def do_update(self
,macoswindowid
,event
):
95 Some utilities: convert an Image to a NumPy array and viceversa.
96 The Image2Numeric function doesn't make any color space conversion.
97 The Numeric2Image function returns an L or RGB or RGBA images depending on the shape of
104 def Image2Numeric(im
):
105 tmp
=fromstring(im
.tostring(),UnsignedInt8
)
107 if (im
.mode
=='RGB')|
(im
.mode
=='YCbCr'):
110 if (im
.mode
=='RGBA')|
(im
.mode
=='CMYK'):
116 tmp
.shape
=(im
.size
[0],im
.size
[1],bands
)
117 return transpose(tmp
,(1,0,2))
119 def Numeric2Image(num
):
120 #sometimes a monoband image's shape can be (x,y,1), other times just (x,y). Here w deal with both
121 if len(num
.shape
)==3:
129 return Image
.fromstring(mode
,(num
.shape
[1],num
.shape
[0]),transpose(num
,(1,0,2)).astype(UnsignedInt8
).tostring())
131 return Image
.fromstring('L',(num
.shape
[1],num
.shape
[0]),transpose(num
).astype(UnsignedInt8
).tostring())
133 def showImage(im
,title
="ImageWin",zoomFactor
=1):
134 imw
=ImageMacWin((300,200),title
)
137 imw
.Show(im
,zoomFactor
)
138 except MemoryError,e
:
140 print "ImageMac.showImage: Insufficient Memory"
143 def showNumeric(num
,title
="NumericWin",zoomFactor
=1):
144 #im=Numeric2Image(num)
145 numw
=NumericMacWin((300,200),title
)
148 numw
.Show(num
,zoomFactor
)
151 print "ImageMac.showNumeric Insufficient Memory"
154 GimmeImage pops up a file dialog and asks for an image file.
155 it returns a PIL image.
156 Optional argument: a string to be displayed by the dialog.
159 def GimmeImage(prompt
="Image File:"):
161 fsspec
, ok
= macfs
.PromptGetFile(prompt
)
163 path
= fsspec
.as_pathname()
164 return Image
.open(path
)
168 This is just some experimental stuff:
169 Filter3x3 a convolution filter (too slow use signal tools instead)
170 diffBWImage subtracts 2 images contained in NumPy arrays
171 averageN it computes the average of a list incrementally
172 BWImage converts an RGB or RGBA image (in a NumPy array) to BW
173 SplitBands splits the bands of an Image (inside a NumPy)
174 NumHisto and PlotHisto are some experiments to plot an intesity histogram
177 def Filter3x3(mul
,fi
,num
):
178 (a
,b
,c
,d
,e
,f
,g
,h
,i
)=fi
180 num
.shape
=(num
.shape
[0],num
.shape
[1])
182 for x
in range(1,num
.shape
[0]-1):
183 for y
in range(1,num
.shape
[1]-1):
188 res
[x
,y
]=int((a
*num
[xb
,yb
]+b
*num
[x
,yb
]+c
*num
[xa
,yb
]+d
*num
[xb
,y
]+e
*num
[x
,y
]+f
*num
[xa
,y
]+g
*num
[xb
,ya
]+h
*num
[x
,ya
]+i
*num
[xa
,ya
])/mul
)
191 def diffBWImage(num1
,num2
):
192 return 127+(num1
-num2
)/2
194 def averageN(N
,avrg
,new
):
195 return ((N
-1)*avrg
+new
)/N
199 bw
=array(((0.3086,0.6094,0.0820)))
201 bw
=array(((0.3086,0.6094,0.0820,0)))
202 res
=innerproduct(num
,bw
)
203 res
.shape
=(res
.shape
[0],res
.shape
[1])
210 return (reshape(num
[:,:,0],(x
,y
)),reshape(num
[:,:,1],(x
,y
)),reshape(num
[:,:,2],(x
,y
)))
212 return (reshape(num
[:,:,0],(x
,y
)),reshape(num
[:,:,1],(x
,y
)),reshape(num
[:,:,2],(x
,y
)),reshape(num
[:,:,3],(x
,y
)))
215 #print "type(datas) ",type(datas)
217 n
=searchsorted(sort(a
),arange(0,256))
218 n
=concatenate([n
,[len(a
)]])
221 def PlotHisto(datas
,ratio
=1):
222 from graphite
import *
226 #print "histo.shape: ",h.shape
228 #print "maxval ",maxval
232 datah
=concatenate([x
,h
],1)
236 g
.datasets
.append(Dataset(datah
))
238 f0
.lineStyle
= LineStyle(width
=2, color
=red
, kind
=SOLID
)
240 g
.axes
[X
].range = [0,255]
241 g
.axes
[X
].tickMarks
[0].spacing
= 10
242 #g.axes[X].tickMarks[0].labels = "%d"
243 g
.axes
[Y
].range = [0,maxval
/ratio
]
249 genOutput(g
,'QD',size
=(600,400))
256 fsspec
, ok
= macfs
.PromptGetFile("Image File:")
258 path
= fsspec
.as_pathname()
260 #im2=im.filter(ImageFilter.SMOOTH)
261 showImage(im
,"normal")
262 num
=Image2Numeric(im
)
263 #num=Numeric.transpose(num,(1,0,2))
265 showNumeric(num
,"Numeric")
267 print "num.shape ",num
.shape
268 showImage(Numeric2Image(num
),"difficile")
269 #showImage(im.filter(ImageFilter.SMOOTH),"smooth")
270 #showImage(im.filter(ImageFilter.FIND_EDGES).filter(ImageFilter.SHARPEN),"detail")
274 print "did not open file"
276 if __name__
== '__main__':