Skip to content

Commit

Permalink
wxGUI/psmap: remove loading EPS file via using Ghostscript image rend…
Browse files Browse the repository at this point in the history
…ering on OS MS Windows platform (OSGeo#2419)

PIL module has ability to load EPS file directly on this platform.
  • Loading branch information
tmszi authored Jun 8, 2022
1 parent c7495e2 commit 5995c7d
Show file tree
Hide file tree
Showing 3 changed files with 1 addition and 82 deletions.
5 changes: 0 additions & 5 deletions gui/wxpython/psmap/dialogs.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@

import os
import string
import sys
from copy import deepcopy

import wx
Expand Down Expand Up @@ -5947,10 +5946,6 @@ def OnImageSelectionChanged(self, event):
if os.path.splitext(file)[1].lower() == ".eps":
try:
pImg = PILImage.open(file)
if sys.platform == "win32":
import types

pImg.load = types.MethodType(loadPSForWindows, pImg)
img = PilImageToWxImage(pImg)
except IOError as e:
GError(message=_("Unable to read file %s") % file)
Expand Down
11 changes: 0 additions & 11 deletions gui/wxpython/psmap/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -469,12 +469,6 @@ def OnCmdDone(self, event):

im_array = np.array(im)
im = PILImage.fromarray(np.rot90(im_array, 3))

# hack for Windows, change method for loading EPS
if sys.platform == "win32":
import types

im.load = types.MethodType(loadPSForWindows, im)
im.save(self.imgName, format="PNG")
except (IOError, OSError) as e:
del busy
Expand Down Expand Up @@ -2472,11 +2466,6 @@ def DrawGraphics(
def DrawBitmap(self, pdc, filePath, rotation, bbox):
"""Draw bitmap using PIL"""
pImg = PILImage.open(filePath)
if sys.platform == "win32" and "eps" in os.path.splitext(filePath)[1].lower():
import types

pImg.load = types.MethodType(loadPSForWindows, pImg)

if rotation:
# get rid of black background
pImg = pImg.convert("RGBA")
Expand Down
67 changes: 1 addition & 66 deletions gui/wxpython/psmap/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,11 @@
@author Anna Kratochvilova <kratochanna gmail.com>
"""
import os
import wx
import string
from math import ceil, floor, sin, cos, pi

try:
from PIL import Image as PILImage
from PIL import Image as PILImage # noqa

havePILImage = True
except ImportError:
Expand Down Expand Up @@ -452,66 +450,3 @@ def BBoxAfterRotation(w, h, angle):
width = int(ceil(abs(x_max) + abs(x_min)))
height = int(ceil(abs(y_max) + abs(y_min)))
return width, height


# hack for Windows, loading EPS works only on Unix
# these functions are taken from EpsImagePlugin.py


def loadPSForWindows(self):
# Load EPS via Ghostscript
if not self.tile:
return
self.im = GhostscriptForWindows(self.tile, self.size, self.fp)
self.mode = self.im.mode
self.size = self.im.size
self.tile = []


def GhostscriptForWindows(tile, size, fp):
"""Render an image using Ghostscript (Windows only)"""
# Unpack decoder tile
decoder, tile, offset, data = tile[0]
length, bbox = data

import tempfile

file = tempfile.mkstemp()[1]

# Build ghostscript command - for Windows
command = [
"gswin32c",
"-q", # quite mode
"-g%dx%d" % size, # set output geometry (pixels)
"-dNOPAUSE -dSAFER", # don't pause between pages, safe mode
"-sDEVICE=ppmraw", # ppm driver
"-sOutputFile=%s" % file, # output file
]

command = string.join(command)

# push data through ghostscript
try:
gs = os.popen(command, "w")
# adjust for image origin
if bbox[0] != 0 or bbox[1] != 0:
gs.write("%d %d translate\n" % (-bbox[0], -bbox[1]))
fp.seek(offset)
while length > 0:
s = fp.read(8192)
if not s:
break
length = length - len(s)
gs.write(s)
status = gs.close()
if status:
raise IOError("gs failed (status %d)" % status)
im = PILImage.core.open_ppm(file)

finally:
try:
os.unlink(file)
except:
pass

return im

0 comments on commit 5995c7d

Please sign in to comment.