Python: PIL - [Errno 32] Broken pipe when saving .png - python

What I'm trying to do here is save the contents of a Tkinter Canvas as a .png image using PIL.
This is my save function ('graph' is the canvas).
def SaveAs():
filename = tkFileDialog.asksaveasfilename(initialfile="Untitled Graph", parent=master)
graph.postscript(file=filename+".eps")
img = Image.open(filename+".eps")
img.save(filename+".png", "png")
But it's getting this error:
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Python27\lib\lib-tk\Tkinter.py", line 1410, in __call__
return self.func(*args)
File "C:\Users\Adam\Desktop\Graphing Calculator\Graphing Calculator.py", line 352, in SaveAs
img.save(filename+".png", "png")
File "C:\Python27\lib\site-packages\PIL\Image.py", line 1406, in save
self.load()
File "C:\Python27\lib\site-packages\PIL\EpsImagePlugin.py", line 283, in load
self.im = Ghostscript(self.tile, self.size, self.fp)
File "C:\Python27\lib\site-packages\PIL\EpsImagePlugin.py", line 72, in Ghostscript
gs.write(s)
IOError: [Errno 32] Broken pipe
I'm running this on Windows 7, Python 2.7.1.
How do I make this work?

oh I just get the same error. I have solve it now
just do the following after installing PIL and Ghostscript
1) Open C:\Python27\Lib\site-packages\PIL\EpsImagePlugin.py
2) Change code near line 50 so that it looks like this:
Build ghostscript command
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
"-"
]
Make sure that gswin32c.exe is in the PATH
good luck

It looks like the Ghostscript executable is erroring out and then closing the connection. Others have had this same problem on different OSes.
So, first I would recommend that you confirm that PIL is installed correctly--see the FAQ page for hints. Next, ensure that Ghostscript is installed and working. Lastly, ensure that Python can find Ghostscript, for example by running a PIL script that works elsewhere.
Oh, also--here are some tips on catching the broken pipe error so your program can be more resilient, recognize the problem, and warn the end-user. Hope that helps!

I have realized that while Python 2.7 has this EPEImagePulgin.py, Anaconda also has it. And unfortunately Anaconda's file is an older version. And unfortunately again, when you run your from Spyder environment it was picking up the epsimageplugin.py file from anaconda folder.
So I was getting similar broken pipe error.
When I went into python 2.7 directory and opened python console and then ran my code, it ran just fine.
Because the lates epsimageplugin.py file takes into consideration the windows environment and appropriate ghostscript exe files. Hope this helps.

Related

PyCharm can't find Tesseract on Mac

I'm starting out with some basic python tutorials with OpenCV, and the first tutorial uses Tesseract, Pytesseract, and OpenCV. I have Tesseract downloaded and pip installed, and I have Pytesseract and OpenCV downloaded, installed, and included in my PyCharm packages, so I think the problem is how I'm addressing the Tesseract file in my code, since I'm new to using a Mac.
(I'm using Python 3.8, but also have Python 2.7 installed, because I needed it to get to this point. Weirdly enough, up to this point, the code only ran without error if I had Python 2.7 installed, but had 3.8 as my PyCharm interpreter.)
When I put Tesseract into my terminal, it tells me that the file address is simply 'Applications/tesseract'. But when I use this as the address in PyCharm, I get the error message below. If anyone could help me figure out how to handle this error, I would appreciate it a lot!!! (I'm new to everything computers, btw. This is how I'm learning.)
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/Users/george/PycharmProjects/pythonProject2/main.py", line 7, in <module>
print(pytesseract.image_to_string(img))
File "/Users/george/Library/Python/3.8/lib/python/site-packages/pytesseract/pytesseract.py", line 370, in image_to_string
return {
File "/Users/george/Library/Python/3.8/lib/python/site-packages/pytesseract/pytesseract.py", line 373, in <lambda>
Output.STRING: lambda: run_and_get_output(*args),
File "/Users/george/Library/Python/3.8/lib/python/site-packages/pytesseract/pytesseract.py", line 282, in run_and_get_output
run_tesseract(**kwargs)
File "/Users/george/Library/Python/3.8/lib/python/site-packages/pytesseract/pytesseract.py", line 254, in run_tesseract
raise TesseractNotFoundError()
pytesseract.pytesseract.TesseractNotFoundError: \Applications\tesseract is not installed or it's not in your PATH. See README file for more information."
I don't know what to look for in the README file, though.
Here is the code that kicked off the error message:
import cv2
import pytesseract
pytesseract.pytesseract.tesseract_cmd = '\\Applications\\tesseract'
img = cv2.imread('im1.png')
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
print(pytesseract.image_to_string(img))
cv2.imshow('Result', img)
cv2.waitKey(0)
On Macs (and other Unix-like OSes), the path separator is the forward slash, not the backslash.
pytesseract.pytesseract.tesseract_cmd = '/Applications/tesseract'
could work better.
However, do you actually need to explicitly set that? It's likely the library could be able to find Tesseract on its own

OSError: [WinError 126] when importing a library in python

i am using the cubemos skeleton tracking Api (using for extract skeleton keypoints from pictures and videos).
i have a very weird problem:
everything was working fine, until one day, i received this error (also added image):
Traceback (most recent call last):
File "C:/Users/omria/PycharmProjects/skeletonTracking/skeletonTracking.py", line 3, in <module>
from cubemos.skeleton_tracking.nativewrapper import Api, SkeletonKeypoints
File "C:\Users\omria\PycharmProjects\skeletonTracking\venv\lib\site-packages\cubemos\skeleton_tracking\__init__.py", line 1, in <module>
from .nativewrapper import *
File "C:\Users\omria\PycharmProjects\skeletonTracking\venv\lib\site-packages\cubemos\skeleton_tracking\nativewrapper.py", line 28, in <module>
os.path.join(cubemos_dir, "bin", "cubemos_skeleton_tracking.dll")
File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python37_64\lib\ctypes\__init__.py", line 364, in __init__
self._handle = _dlopen(self._name, mode)
OSError: [WinError 126] The specified module could not be found
enter image description here
and line 3 is when i'm importing the library:
from cubemos.skeleton_tracking.nativewrapper import Api
another weird thing, is that the problem was fixed randomly without me doing anything, and then it returned again!
i tried reinstalling the cubemos SDK, rebooting the computer, debug, and i really don't know what is the source of this problem.
it seems like its some problem with my OS (i have windows 10, and using python 3.7 and VENV and PIP for the project)
i tried to figure out if the path is the problem, but again, it was working before, and i can't really tell if that is the problem. what do you think is causing this?
i will appreciate any help.
thanks !
I had the same problem using a conda env. Demos worked but not the python sample. I explicitly added %CUBEMOS_SKEL_SDK%\bin to my Path environment since I had nothing before. That got it going. The %CUBEMOS_SKEL_SDK% was set up okay from the start.

Moviepy OSError Exec format error - Missing Shebang?

I am attempting to use MoviePy with Python 3.2.3 on Raspian.
I have installed it (for Python 2.7, 3.2 and 3.5... long story) and the line
from moviepy.editor import *
works fine.
When I try
clip = VideoFileClip("vid.mov")
which is the most basic command, it gives the error
Traceback (most recent call last):
File "/home/pi/QuickFlicsPics/moviepytest.py", line 8, in <module>
clip = VideoFileClip("vid.mov")
File "/usr/local/lib/python3.2/distpackages/moviepy/video/io/VideoFileClip.py", line 55, in __init__
reader = FFMPEG_VideoReader(filename, pix_fmt=pix_fmt)
File "/usr/local/lib/python3.2/dist-packages/moviepy/video/io/ffmpeg_reader.py", line 32, in __init__
infos = ffmpeg_parse_infos(filename, print_infos, check_duration)
File "/usr/local/lib/python3.2/dist-packages/moviepy/video/io/ffmpeg_reader.py", line 237, in ffmpeg_parse_infos
proc = sp.Popen(cmd, **popen_params)
File "/usr/lib/python3.2/subprocess.py", line 745, in __init__
restore_signals, start_new_session)
File "/usr/lib/python3.2/subprocess.py", line 1371, in _execute_child
raise child_exception_type(errno_num, err_msg)
OSError: [Errno 8] Exec format error
I have researched this error, and it appears to be something to do with a shebang line missing somewhere. Is this correct, if so, how do I go about finding where it is missing, and what do I add?
Thanks
Edit:
As per cxw's comment, I installed moviepy using the command
pip-3.2 install moviepy
(I may have used 'sudo' as well)
FFMPEG was supposed to download automatically when I first used moviepy:
MoviePy depends on the software FFMPEG for video reading and writing. > You don’t need to worry about that, as FFMPEG should be automatically > downloaded/installed by ImageIO during your first use of MoviePy (it takes a few seconds). If you want to use a specific version of FFMPEG, follow the instructions in file config_defaults.py.
[Quote from installation guide here]
Manually download ffmpeg, then before running your Python code, do
export FFMPEG_BINARY=path/to/ffmpeg
at the shell/terminal prompt.
As far as I can tell from the source, the automatic download of ffmpeg does not know about Raspberry Pis. The auto-download code pulls from the imageio github repo, which only knows "linux32" vs. "linux64". It doesn't look like it has an ARM-linux option. When the ARM kernel sees a non-ARM image, it throws the error you see.
Rather than using the environment variable, you can edit your moviepy config-defaults.py file to specify FFMPEG_BINARY = r"/path/to/ffmpeg".
Edit to find the path/to/ffmpeg after installing it with apt-get, do
dpkg -L ffmpeg | grep bin
at the shell/terminal prompt. It will probably be in /bin or /usr/bin, and will probably be called ffmpeg or ffmpeg-x.xx (with some version number).
Thanks to this answer for dpkg

Enthought Canopy crashes upon opening mac

So yesterday I updated to Enthought version 1.1 and now it refuses to open. I've rebooted my computer as well as did a re-install of enthought canopy. I keep getting the following error
Traceback (most recent call last):
File "build/bdist.macosx-10.5-i386/egg/canopy/app/bootstrap.py", line 1989, in main
File "build/bdist.macosx-10.5-i386/egg/canopy/app/bootstrap.py", line 1021, in main
File "build/bdist.macosx-10.5-i386/egg/canopy/app/bootstrap.py", line 1012, in _ kill_leftover_procs
File "build/bdist.macosx-10.5-i386/egg/canopy/app/running_process_manager.py", line 116, in kill_leftover_procs
File "/Applications/Canopy.app/appdata/canopy-1.1.0.1371.macosx-x86_64/Canopy.app/Contents/lib/python2.7/contextlib.py", line 17, in __enter__
return self.gen.next()
File "build/bdist.macosx-10.5-i386/egg/canopy/app/running_process_manager.py", line 59, in lock
LockError: Lock could not be acquired
I have no idea what's going on here. I've sent the error report to enthought but does anyone have any ideas?
I think I did it. Try searching for these files in your Terminal. They're inside your .canopy folder. Make sure that you're working on your root directory. They're not searchable via Finder. My Canopy's finally working now. Hope this helps.
proc_manager.lock
process.lck
running_procs.pkl
Somehow it seems like a lock file didn't get cleaned in the process. Look into the ~/.canopy folder and remove the process.lck file. You may also start your Activity Monitor and make sure there is no stray canopy or python process, and kill it if there is (or log out of OSX and log back in, which will do the same thing). Canopy will run as normally after that.

Problems with PIL and py2exe

I'm trying to convert a GUI program that uses .png images into an .exe using py2exe. It uses PIL to display these images. However, I'm having trouble with it converting. For some reason the .exe it makes wont run. Any ideas?
Edit
Error:
Traceback (most recent call last):
File "GUI1.2.8.py", line 454, in <module>
File "GUI1.2.8.py", line 13, in __init__
File "Tkinter.pyc", line 1643, in __init__
_tkinter.TclError: Can't find a usable init.tcl in the following directories:
{C:/Users/rectangletangle/lib/tcl8.5} {C:/Users/rectangletangle/lib/tcl8.5} C:/Users/lib/tcl8.5 {C:/Users/rectangletangle/library} C:/Users/library C:/Users/tcl8.5.8/library C:/tcl8.5.8/library
This probably means that Tcl wasn't installed properly.
The error seems to be completely unrelated (If I do anything wrong with py2exe, this is the error I usually get). Everything, work's until I import and use PIL. Also, I included PIL in my setup file.
Edit 2
This is a snippet showing how I implement the image.
self.image = ImageTk.PhotoImage(Image.open("image.png"))
self.imageLabel = Tkinter.Label(self, image=self.image)
self.imageLabel.grid(column=0, columnspan=4, row=0)
how do you display the image?
maybe PIL uses tcl/tk to img.show() and that's why you should include tcl in your py2exe setup script.
Just in case someone happens upon this problem, I was able to fix this error message by adding the init.tcl to my "data_files" section in my setup.py file:
setup(data_files=['D:\\Python27\\tcl\\tcl8.5\\init.tcl',
windows=["script":'myfile.py')])

Categories