blob: ac261b5d37be7876c559e7d1eff2e572d16483ad [file] [log] [blame]
Jack Jansend35509a2000-09-22 12:46:19 +00001'''
2A really quick and dirty hack to extend PixMapWrapper
3They are mere copies of the toImage and fromImage methods.
4Riccardo Trocca (rtrocca@libero.it)
5'''
6from PixMapWrapper import *
7import Numeric
8
9class ExtPixMapWrapper(PixMapWrapper):
10
11 def toNumeric(self):
12
13 data = self.tostring()[1:] + chr(0)
14 bounds = self.bounds
15 tmp=Numeric.fromstring(data,Numeric.UnsignedInt8)
16 #tmp.shape=(bounds[3]-bounds[1],bounds[2]-bounds[0],4)
17 tmp.shape=(bounds[2]-bounds[0],bounds[3]-bounds[1],4)
18 return Numeric.transpose(tmp,(1,0,2))
19
20 def fromNumeric(self,num):
21 s=num.shape
22 x=num.shape[1]
23 y=num.shape[0]
24 #bands=1 Greyscale image
25 #bands=3 RGB image
26 #bands=4 RGBA image
27 if len(s)==2:
28 bands=1
29 num=Numeric.resize(num,(y,x,1))
30 else:
31 bands=num.shape[2]
32
33 if bands==1:
34 num=Numeric.concatenate((num,num,num),2)
35 bands=3
36 if bands==3:
37 alpha=Numeric.ones((y,x))*255
38 alpha.shape=(y,x,1)
39 num=Numeric.concatenate((num,alpha),2)
40
41 data=chr(0)+Numeric.transpose(num,(1,0,2)).astype(Numeric.UnsignedInt8).tostring()
42 PixMapWrapper.fromstring(self,data,x,y)
43
44
45
46