OSError: Unable to locate Ghostscript on paths - python

I tried to open an EPS image with Pyzo, I have installed PIL and Ghostscript (as I saw that it is necessary on some other website topics), my code is:
from PIL import Image
im = Image.open('''myimage.eps''')
im.show()
but when I run the code, Pyzo return me:
OSError: Unable to locate Ghostscript on paths
I tried to look into it on several websites but it seems pretty complicated for a novice coding student.

In case someone else encounters this issue: It seems that Ghostscript has not been added to the paths properly. For those running Win7, here is a fix:
Go to:
Control Panel -> System -> Advanced system settings -> Environment Variables...
Find the variable "PATH" -> Edit... -> add the path to your ghostscript binary folder, e.g.
C:\Program Files\gs\gs9.22\bin\;
to the end of the variable. It should be separated from the previous entry by a semicolon.
I had to restart for the changes to take effect.

You need ghostscript.
download: https://www.ghostscript.com/download/gsdnld.html
Tell the variable(EpsImagePlugin.gs_windows_binary) what the path of EXE(gswin64c, gswin32c, gs ) it is. (If you don't want to change the system path.)
from PIL import EpsImagePlugin
EpsImagePlugin.gs_windows_binary = r'X:\...\gs\gs9.52\bin\gswin64c'
im = Image.open('myimage.eps')
im.save('myimage.png')
You can see the following on PIL.EpsImagePlugin.py
# EpsImagePlugin.py
__version__ = "0.5"
...
gs_windows_binary = None # 👈
def Ghostscript(tile, size, fp, scale=1):
"""Render an image using Ghostscript"""
...
if gs_windows_binary is not None:
if not gs_windows_binary: # 👈
raise WindowsError("Unable to locate Ghostscript on paths")
command[0] = gs_windows_binary
So that's why I tell you to set the gs_windows_binary will work.

Related

Image moves or deletes when displaying with PIL

Recently, I have been working on one of my python3 programs and then I wanted to to open a picture. Here is the code that I used to do it:
from PIL import Image
r = Image.open('C:/Users/sudam/OneDrive/Desktop/programming/python/projects/good night app/morning.png' )
r.show()
But as soon as I run this code,the windows photo viewer opens and gives and error saying that the specified file was moved. I tried googling this question but all of the answers I got only worked for python2, but not for python3.
Because you have whitespace in your path you need to use the r"string" format.
Also you need to use:
PIL.ImageShow.show(r)
to show your image.
It's also recommended to check if the file exist before opening any file.
You can do like this:
from pathlib import Path
from PIL import Image, ImageShow
path =r"C:/Users/sudam/OneDrive/Desktop/programming/python/projects/good night app/morning.png"
if Path(path).is_file():
r = Image.open(path)
ImageShow.show(r)
else:
print(f'{path} not exist')

(Python) Tesseract Installation Problem in Windows

I've read a couple other answers on this, but I'm still stuck. I imagine I'm doing something stupid, but this doesn't work:
import pytesseract
from PIL import Image
def tryTesseract(u):
return(pytesseract.image_to_string(Image.open(u)))
loc = 'C:\\Python\\Lineups\\558.png'
print(pytesseract)
print(tryTesseract(loc))
The first line prints:
<module 'pytesseract' from 'C:\Python\lib\site-packages\pytesseract\init.py'>
But the second prints several lines of error and culminates in:
pytesseract.pytesseract.TesseractNotFoundError: tesseract is not installed or it's not in your PATH. See README file for more information.
This seems weird if the first line works. I BELIEVE I have correctly added it to path though, and it is correctly installed, as in this screenshot:
Full error message:
Edited for exciting new error. I followed user3250052's advice and am now getting a new error (CMD window on top of Python window here:)
from PIL import Image
def tryTesseract(u):
return(pytesseract.image_to_string(Image.open(u)))
loc = os.path.join('C','Python','Lineups','558.png')
pytesseract.tesseract_cmd = r'C:\Program Files\Tesseract-OCR'
print(pytesseract)
print(tryTesseract(loc))```
That is a file not fond error.
Try
loc = os.path.join('C','Python','Lineups','558.png')
you might also need
pytesseract.tesseract_cmdloc = r'<full_path_to_your_tesseract_executable>'

Python Pillow Image.show() Path Issue?

Honest warning—I'm a total beginner.
I'm using Python 3.8.2 in IDLE and sometimes in Spyder.
My goal: to open an image (located in a folder) in Preview using Pillow
My code:
from PIL import Image
my_image = Image.open("flower.jpg")
my_image.show(r"/Users/User/Desktop/flower.jpg")
I run this, and it works! But it only works if the jpg is on the Desktop. I want to put the image in a folder. So I changed my last line of code to something like this:
my_image.show(r"/Users/User/Desktop/folder/flower.jpg")
I put the image in the folder, run the program, and get this error:
**FileNotFoundError: [Errno 2] No such file or directory: 'flower.jpg'**
Oddly, if I put the jpg back onto the Desktop and still use the path with "folder" in it, the program runs successfully and shows me the image.
Is this a problem with the path? How can I use Image.show() to open an image that is located somewhere other than the Desktop?
Thank you!
If you want to open and view an image using an absolute path, you'll have to change this line:
my_image = Image.open(path_to_image_dir)
You've incorrectly passed a string to PIL.Image.show. This doesn't throw an error, since PIL.Image.show happens to take an optional string parameter title, which it will use as a title in the image window. Don't pass any parameters to show, and change "flower.jpg" in the line above to the actual path.

Why doesn't SimpleITK display image with imageJ?

I am new in ITK. I wrote a small program to read image using sitk and display the image using imageJ.
import os
import SimpleITK as sitk
pathDicom = 'C://Users//clouds.png'
input = sitk.ReadImage(pathDicom)
sitk.Show( input , debugOn=True)
To link simpleITK to imageJ I set the environment variable. But I am getting following error for sitk.Show():
return _SimpleITK.Show(*args, **kwargs)
RuntimeError: Exception thrown in SimpleITK Show: C:\d\VS14-Win32-pkg\SimpleITK\Code\IO\src\sitkShow.cxx:580:
sitk::ERROR: Error in administrating child process: [Access is denied].
imageJ output is shown in the attached figure. Can someone tell what's the problem?
What version of SimpleITK are you running. It looks like it is not correctly finding the ImageJ executable. The 2nd line of the output shows that it's trying to execute 'C:\Users\Harish\fiji-win64\Fiji.app'. That's the Fiji directory, not the actual executable.
Can you try moving the Fiji.app directory to be directly inside your home directory? So move it up a directory to get rid of fiji-win64 in the path.
By default it searches there, so maybe the fiji-win64 directory is messing it up.
The other thing you can try is the SITK_SHOW_COMMAND environment variable. Set it to the full path of the ImageJ executable. That will override the search for ImageJ.

Fonts not found

I am trying to add simple text to a mp4 file. I have the mp4 file and I am using moviepy to add the text clip, however, I am continually running into errors.
My code:
import moviepy.editor as mpy
video_name='Test.mp4'
video = mpy.VideoFileClip('test_movie.mp4')
text = mpy.TextClip("Test", font="Arial", fontsize=40, color='white')
text = text.set_position('upper left').set_duration(video.duration)
test = mpy.CompositeVideoClip([video, text])
test.write_videofile('{}.mp4'.format(video_name))
And here is the error:
IOError: MoviePy Error: creation of None failed because of the following error:
convert: delegate library support not built-in '/Library/Fonts//Arial.ttf' (Freetype) # warning/annotate.c/RenderFreetype/1847.
convert: no decode delegate for this image format `PNG' # error/constitute.c/ReadImage/512.
convert: no images defined `PNG32:/var/folders/l3/m81nh88n57s9ck30_fqd09cmcfj06p/T/tmp06GxjE.png' # error/convert.c/ConvertImageCommand/3275.
.
.This error can be due to the fact that ImageMagick is not installed on your computer, or (for Windows users) that you didn't specify the path to the ImageMagick binary in file conf.py, or that the path you specified is incorrect
ImageMagick is installed on my system and I have been through a few threads on here but nothing has pointed me in the right direction. My font library is not empty and I do not know what it is having this issue. Any help would be greatly appreciated. I am on a Mac and using Spyder for a GUI.
Try to install ImageMagick from their website.
If the problem still there, find the moviepy/config_defaults.py and after the last line add:
IMAGEMAGICK_BINARY = "C:\\Program Files\\ImageMagick_VERSION\\convert.exe

Categories