1

I'm using Python on Windows 10 with PyCharm. My script contains this line:

img = PIL.Image.open(io.BytesIO(ps.encode('utf-8')))

It triggers this error:

Traceback (most recent call last):
  File "C:/Users/x/Desktop/ytg2/main.py", line 504, in <module>
    generate_terrain(driver)
  File "C:/Users/x/Desktop/ytg2/main.py", line 129, in generate_terrain
    img = open_eps(ps, dpi=95.5)
  File "C:/Users/x/Desktop/ytg2/main.py", line 32, in open_eps
    img.load(scale=math.ceil(scale))
  File "C:\Users\x\AppData\Local\Programs\Python\Python37\lib\site-packages\PIL\EpsImagePlugin.py", line 332, in load
    self.im = Ghostscript(self.tile, self.size, self.fp, scale)
  File "C:\Users\x\AppData\Local\Programs\Python\Python37\lib\site-packages\PIL\EpsImagePlugin.py", line 134, in Ghostscript
    raise OSError("Unable to locate Ghostscript on paths")
OSError: Unable to locate Ghostscript on paths

Process finished with exit code 1

So what I understand is that the function load of the object returned by PIL.Image.open uses the package Ghostscript that can't be found with the interpreter.

So here is, in the order, what I've tried to do:

  1. In PyCharm's packages manager, I've installed the following packages: python3-ghostscript and ghostscript.

  2. In Windows 10 Environments Variables, I have added this variable: (name="Ghostscript" ; value="C:\Program Files\gs\gs9.52\bin\gswin64.exe"). Previously, I've of course manually installed Ghostscript (https://www.ghostscript.com/download/gsdnld.html). I 've tried this value too: %ProgramFiles%\gs%\gs9.52%\bin%\gswin64.exe.

However the problem is still here. What could I do?

KenS
  • 30,202
  • 3
  • 34
  • 51
JarsOfJam-Scheduler
  • 2,809
  • 3
  • 31
  • 70
  • 1
    Did you try adding it to the PATH variable? Check [this](https://stackoverflow.com/questions/44587376/oserror-unable-to-locate-ghostscript-on-paths#:~:text=Go%20to%3A%20Control%20Panel%20%2D%3E,your%20ghostscript%20binary%20folder%2C%20e.g.) out. – Parth Shah Jul 25 '20 at 20:34
  • @ParthShah I think it's my bullet N°2 no? :-) – JarsOfJam-Scheduler Jul 25 '20 at 20:42
  • 1
    Oops my bad. Another experience I have had with PyCharm is as follows: you need to restart terminal/refresh window/restart application for OS changes to take effect. Have you tried that? – Parth Shah Jul 25 '20 at 20:43
  • @ParthShah Yes, I have restarded my computer each time I've tried to modify my win10 path^^ – JarsOfJam-Scheduler Jul 25 '20 at 20:57
  • 1
    You've added the entire path and executable name to $PATH, you should only add the path, not the executable name. So in your case "c:\program Files\gs\gs9.52\bin". I don't know how you are adding that to the Windows environment variables (nor do I know if Python reads those, or uses its own). If I were adding it to Windows it would be Control Panel->System and Security->System->Advanced System Settings, then press the 'environment variables' button, In the lower list box (labelled System variables) select 'Path' and press Edit. Then press add and type in the path (not the executable name) – KenS Jul 26 '20 at 07:53

1 Answers1

0
  1. The PIL.Image.open(io.BytesIO(ps.encode('utf-8'))) uses shutils.which('gswin64c') to find gswin64c (I knew that by clicking on a file link that the Python Interpreter shown in the PyCharm's console, in the error logs - this link is: one of the two last lines beginning with the word File in the error logs I've shown in the OP, if I remember well).
  2. shutils.which('gswin64c') was returning None (indeed, I made myself a print of it) ; so I prompted os.environ["PATH"] and indeed, it was not contained in the printed output. Then to be sure, I typed echo %path% in the Windows 10 CLI, and I made the same constatation.
  3. My conclusion was: I thought I was correctly adding the path of gswin64c in the way I mentionned in the OP of this SOflw Question (via the admin panel) but in fact, I was wrong.
  4. (Maybe this step is optional.) So: first, since it doesn't work, I have deleted the path of gswin64c that I have added via the admin panel (cf.: the OP). This deletion was done via the admin panel too.
  5. Then, to correctly add the path of gswin64c, I've typed, in the Windows CLI: setx path "%path%;c:\Program Files\..........\" (this path must contain gswin64c). Then I've restarted Windows 10 (if I remember well, it was required).
  6. Then I re-printed the result of shutils.which('gswin64c') and gswin64c is found now. Also os.environ["PATH"] and echo %path% correctly output the path of gswin64c.

I hope this answer could help someone. In fact it was not very difficult: one just has to know how to correctly add a path on Windows 10.... Lol.

I was "inspired" by: https://www.windows-commandline.com/set-path-command-line/ ;-) .

JarsOfJam-Scheduler
  • 2,809
  • 3
  • 31
  • 70