Attachment 'ImgConv.py'
Download 1 # ImgConv.py
2
3 """
4 Based on pyWiki 'Working With Images' @ http://wiki.wxpython.org/index.cgi/WorkingWithImages
5 Modified to properly copy, create or remove any alpha in all conversion permutations.
6 A wx.App must be created in order for the various wx functions to work.
7
8 Win32IconImagePlugin
9 Alternate PIL plugin for dealing with Microsoft .ico files.
10 http://code.google.com/p/casadebender/wiki/Win32IconImagePlugin
11
12 Notes:
13 - The terms "plane", "band", "layer" and "channel" are used interchangibly.
14 - When the phrase "{argument name} is given" is used, it means
15 this optional argument is specified in the function call's argument list.
16
17 Tested on Win7 64-bit (6.1.7600) and Win XP SP3 (5.1.2600) using Python unicode 32-bit.
18
19 Platform Windows 6.1.7600
20 Python 2.5.4 (r254:67916, Dec 23 2008, 15:10:54) [MSC v.1310 32 bit (Intel) (x86)]
21 Python wx 2.8.10.1
22 Pil 1.1.7
23
24 Ray Pasco
25 pascor(at)verizon(dot)net
26
27 Last modification: 2012-12-25
28 Documentation fixes and clarifications
29 Deleted line [ import WxImageFromPilImage ].
30
31 This code may be freely modified and distributed for any purpose whatsoever.
32
33 """
34
35 import wx # WxBmap <==> wxImage
36 import Image # wxImage <==> PilImage. Used when converting to or from Pil images.
37
38 #------------------------------------------------------------------------------
39
40 # image type image type image type
41 # 1 2 3
42 # wx.Bitmap <==> wx.Image <==> pilImage
43
44 #----------------------------
45
46 def WxBitmapFromPilImage( pilImage, addAlphaLayer=False, delAlphaLayer=False ) :
47 """
48 3 ==> 1
49
50 The default parameter values preserve any existing transparency,
51 but does not create any kind of transparency layer if none already exists.
52
53 If the given pilImage mode is RGB, this function can optionally create
54 a new wxImage alpha transparency plane (layer) by calling with the
55 argument [ addAlphaLayer=True ].
56
57 If the given pilImage mode is RGBA, this function can optionally delete
58 any transparency plane by calling with the argument [ delAlphaLayer=True ].
59
60 If given *both* addAlphaLayer=True *and* delAlphaLayer=True,
61 which is nonsensical, [ addAlphaLayer=True ] will take precedence.
62
63 """
64
65 # Pass on the given parameters.
66 wxImage = WxImageFromPilImage( pilImage, addAlphaLayer, delAlphaLayer )
67 wxBitmap = wxImage.ConvertToBitmap()
68
69 """
70
71 # Pass on the given parameters.
72 wxImage = WxImageFromPilImage( pilImage, addAlphaLayer, delAlphaLayer )
73 wxBitmap = wxImage.ConvertToBitmap()
74
75 return wxBitmap
76
77 #end def
78
79 #----------------------------
80
81 def WxImageFromPilImage( pilImage, addAlphaLayer=False, delAlphaLayer=False ) :
82 """
83 3 ==> 2
84
85 The default parameter values preserve any existing transparency,
86 but do not add one if it none exists in the source image.
87
88 If the given source has no alpha, a new image with alpha transparency
89 can be returned by calling with addAlphaLayer=True.
90
91 If the given source image has alpha, a new image without alpha
92 can be returned by calling with delAlphaLayer=True.
93
94 """
95
96 wxImage = wx.EmptyImage( *pilImage.size ) # Has no transparency plane.
97
98 pilMode = pilImage.mode
99 hasAlpha = pilImage.mode[-1] == 'A'
100
101 if hasAlpha or (not hasAlpha and addAlphaLayer) :
102
103 pilImageRGBA = pilImage.copy() # Image mode might now be RGB, not RGBA.
104 if pilImage.mode != 'RGBA' :
105 pilImageRGBA = pilImageRGBA.convert( 'RGBA' )
106 #end if
107 # The image mode is now gauranteed to be RGBA.
108
109 pilImageStr = pilImageRGBA.tostring() # Convert all 4 image planes.
110
111 # Extract just the RGB data
112 pilRgbStr = pilImageRGBA.copy().convert( 'RGB').tostring()
113 wxImage.SetData( pilRgbStr )
114
115 # Extract just the existing pilImage alpha plane data.
116 pilAlphaStr = pilImageStr[3::4] # start at index 3 with a stride (skip) of 4.
117 wxImage.SetAlphaData( pilAlphaStr )
118
119 elif delAlphaLayer or ((not hasAlpha) and (not addAlphaLayer)) :
120
121 pilImageRGB = pilImage.copy().convert( 'RGB' )
122
123 wxImage.SetData( pilImageRGB.tostring() )
124
125 #end if
126
127 return wxImage
128
129 #end def
130
131 #----------------------------
132
133 def WxBitmapFromWxImage( wxImage, threshold=128 ) :
134 """
135 2 ==> 1
136
137 Any transparency mask or alpha transparency will be copied, to0.
138 """
139
140 wxBmap = wxImage.ConvertToBitmap()
141
142 return wxBmap
143
144 #end def
145
146 #----------------------------
147
148 def WxImageFromWxBitmap( wxBmap ) :
149 """
150 1 ==> 2
151
152 Any transparency mask or alpha transparency will be copied, to0.
153 """
154 return wx.ImageFromBitmap( wxBmap )
155 #end def
156
157 #------------------------------------------------------------------------------
158
159 def PilImageFromWxBitmap( wxBmap, keepTransp=True, createTransp=False, debug=False ) :
160 """
161 1 ==> 3
162
163 The default parameter values preserve any existing transparency,
164 but do not add one if it none exists in the source image.
165
166 If the given source has no alpha, a new image with alpha transparency
167 can be returned by calling with addAlphaLayer=True.
168
169 If the given source image has alpha, a new image without alpha
170 can be returned by calling with delAlphaLayer=True.
171
172 """
173
174 wxImage = WxImageFromWxBitmap( wxBmap )
175
176 return PilImageFromWxImage( wxImage ) # Always preserves any transparency.
177 #end def
178
179 #----------------------------
180
181 def PilImageFromWxImage( wxImage, keepTransp=True, createTransp=False, debug=False ) :
182 """
183 2 ==> 3
184
185 Default preserves any transparency.
186 """
187
188 # These can never be simultaneous.
189 hasMask = wxImage.HasMask()
190 hasAlpha = wxImage.HasAlpha()
191 if debug :
192 print '>>>> PilImageFromWxImage(): Input Image has Alpha'
193
194 # Always convert a mask into an aplha layer.
195 # Deal with keeping or discarding this alpha later on.
196 if hasMask : # Is always mutually exclusive with hasAlpha.
197
198 if debug :
199 print '>>>> PilImageFromWxImage(): Converting Input Image Mask to Alpha'
200
201 wxImage.InitAlpha() # Covert the separate mask to a 4th alpha layer.
202 hasAlpha = True
203
204 #end if
205
206 image_size = wxImage.GetSize() # All images here have the same size.
207
208 # Create an RGB pilImage and stuff it with RGB data from the wxImage.
209 pilImage = Image.new( 'RGB', image_size )
210 pilImage.fromstring( wxImage.GetData() )
211
212 # May need the separated planes if an RGBA image is needed. later.
213 r_pilImage, g_pilImage, b_pilImage = pilImage.split()
214
215 if hasAlpha :
216
217 if keepTransp : # Ignore createTransp - has no meaning
218
219 if debug :
220 print '>>>> PilImageFromWxImage(): Keeping Transparency'
221
222 # Must recompose the pilImage from 4 layers.
223 r_pilImage, g_pilImage, b_pilImage = pilImage.split()
224
225 # Create a Black L pilImage and stuff it with the alpha data
226 # extracted from the alpha layer of the wxImage.
227 pilImage_L = Image.new( 'L', image_size )
228 pilImage_L.fromstring( wxImage.GetAlphaData() )
229
230 # Create an RGBA PIL image from the 4 layers.
231 pilImage = Image.merge( 'RGBA', (r_pilImage, g_pilImage, b_pilImage, pilImage_L) )
232
233 elif (not keepTransp) :
234
235 # The RGB pilImage can be returned as it is now.
236 if debug :
237 print '>>>> PilImageFromWxImage(): Returning an RGB PIL Image.'
238
239 #end if
240
241 elif (not hasAlpha) and createTransp : # Ignore keepTransp - has no meaning
242
243 if debug :
244 print '>>>> PilImageFromWxImage(): Creating a New Transparency Layer'
245
246 # Create a Black L mode pilImage. The resulting image will still
247 # look the same, but will allow future transparency modification.
248 pilImage_L = Image.new( 'L', image_size )
249
250 # Create an RGBA pil image from the 4 bands.
251 pilImage = Image.merge( 'RGBA', (r_pilImage, g_pilImage, b_pilImage, pilImage_L) )
252
253 #end if
254
255 return pilImage
256
257 #end PilImageFromWxImage def
258
259 #------------------------------------------------------------------------------
260
Attached Files
To refer to attachments on a page, use attachment:filename, as shown below in the list of files. Do NOT use the URL of the [get] link, since this is subject to change and can break easily.You are not allowed to attach a file to this page.