Преглед на файлове

replace PIL obsolete function tostring to tobytes

git-svn-id: https://svn.osgeo.org/grass/grass/trunk@67011 15284696-431f-4ddb-bdfa-cd5b030d7da7
Anna Petrášová преди 9 години
родител
ревизия
0baaeca811

+ 8 - 5
gui/wxpython/core/utils.py

@@ -1021,19 +1021,22 @@ def PilImageToWxImage(pilImage, copyAlpha = True):
     """
     import wx
     hasAlpha = pilImage.mode[-1] == 'A'
-    if copyAlpha and hasAlpha :  # Make sure there is an alpha layer copy.
+    if copyAlpha and hasAlpha:  # Make sure there is an alpha layer copy.
         wxImage = wx.EmptyImage(*pilImage.size)
         pilImageCopyRGBA = pilImage.copy()
         pilImageCopyRGB = pilImageCopyRGBA.convert('RGB')    # RGBA --> RGB
-        pilImageRgbData = pilImageCopyRGB.tostring()
+        fn = getattr(pilImageCopyRGB, "tobytes", getattr(pilImageCopyRGB, "tostring"))
+        pilImageRgbData = fn()
         wxImage.SetData(pilImageRgbData)
-        wxImage.SetAlphaData(pilImageCopyRGBA.tostring()[3::4])  # Create layer and insert alpha values.
+        fn = getattr(pilImageCopyRGBA, "tobytes", getattr(pilImageCopyRGBA, "tostring"))
+        wxImage.SetAlphaData(fn()[3::4])  # Create layer and insert alpha values.
 
-    else :    # The resulting image will not have alpha.
+    else:    # The resulting image will not have alpha.
         wxImage = wx.EmptyImage(*pilImage.size)
         pilImageCopy = pilImage.copy()
         pilImageCopyRGB = pilImageCopy.convert('RGB')    # Discard any alpha from the PIL image.
-        pilImageRgbData = pilImageCopyRGB.tostring()
+        fn = getattr(pilImageCopyRGB, "tobytes", getattr(pilImageCopyRGB, "tostring"))
+        pilImageRgbData = fn()
         wxImage.SetData(pilImageRgbData)
 
     return wxImage

+ 1 - 1
gui/wxpython/psmap/dialogs.py

@@ -52,7 +52,7 @@ except ImportError:
 import grass.script as grass
 
 from core               import globalvar
-from core.utils import _
+from core.utils import _, PilImageToWxImage
 from dbmgr.vinfo        import VectorDBInfo
 from gui_core.gselect   import Select
 from core.gcmd          import RunCommand, GError, GMessage

+ 1 - 1
gui/wxpython/psmap/frame.py

@@ -35,7 +35,7 @@ from core.gconsole      import CmdThread, EVT_CMD_DONE
 from psmap.toolbars     import PsMapToolbar
 from core.gcmd          import RunCommand, GError, GMessage
 from core.settings      import UserSettings
-from core.utils import _
+from core.utils import _, PilImageToWxImage
 from gui_core.forms     import GUI
 from gui_core.dialogs   import HyperlinkDialog
 from gui_core.ghelp     import ShowAboutDialog

+ 1 - 23
gui/wxpython/psmap/utils.py

@@ -362,29 +362,7 @@ def getRasterType(map):
         return rasterType
     else:
         return None
-   
-def PilImageToWxImage(pilImage, copyAlpha = True):
-    """Convert PIL image to wx.Image
-    
-    Based on http://wiki.wxpython.org/WorkingWithImages
-    """
-    hasAlpha = pilImage.mode[-1] == 'A'
-    if copyAlpha and hasAlpha :  # Make sure there is an alpha layer copy.
-        wxImage = wx.EmptyImage( *pilImage.size )
-        pilImageCopyRGBA = pilImage.copy()
-        pilImageCopyRGB = pilImageCopyRGBA.convert('RGB')    # RGBA --> RGB
-        pilImageRgbData = pilImageCopyRGB.tostring()
-        wxImage.SetData(pilImageRgbData)
-        wxImage.SetAlphaData(pilImageCopyRGBA.tostring()[3::4])  # Create layer and insert alpha values.
-
-    else :    # The resulting image will not have alpha.
-        wxImage = wx.EmptyImage(*pilImage.size)
-        pilImageCopy = pilImage.copy()
-        pilImageCopyRGB = pilImageCopy.convert('RGB')    # Discard any alpha from the PIL image.
-        pilImageRgbData = pilImageCopyRGB.tostring()
-        wxImage.SetData(pilImageRgbData)
-
-    return wxImage
+
 
 def BBoxAfterRotation(w, h, angle):
     """Compute bounding box or rotated rectangle

+ 1 - 1
lib/python/imaging/images2gif.py

@@ -744,7 +744,7 @@ class NeuQuant:
 
         # Initialize
         self.setconstants(samplefac, colors)
-        self.pixels = np.fromstring(image.tostring(), np.uint32)
+        self.pixels = np.fromstring(getattr(image, "tobytes", getattr(image, "tostring"))(), np.uint32)
         self.setUpArrays()
 
         self.learn()

+ 2 - 1
lib/python/imaging/images2swf.py

@@ -167,7 +167,8 @@ class BitArray:
         return self._len  # self.data.shape[0]
 
     def __repr__(self):
-        return self.data[:self._len].tostring().decode('ascii')
+        fn = getattr(self.data[:self._len], "tobytes", getattr(self.data[:self._len], "tostring"))
+        return fn().decode('ascii')
 
     def _checkSize(self):
         # check length... grow if necessary

+ 2 - 1
scripts/wxpyimgview/wxpyimgview_gui.py

@@ -78,7 +78,8 @@ class Frame(wx.Frame):
 	dc = wx.PaintDC(self)
 	data = app.imgbuf.reshape((app.i_height, app.i_width, 4))
 	data = data[::,::,2::-1]
-	image = wx.ImageFromData(app.i_width, app.i_height, data.tostring())
+	fn = getattr(data, "tobytes", getattr(data, "tostring"))
+	image = wx.ImageFromData(app.i_width, app.i_height, fn())
 	dc.DrawBitmap(wx.BitmapFromImage(image), x0, y0, False)
 
     def redraw(self, ev):