Problems with PIL and py2exe - python

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')])

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

Pyinstaller builds file but chokes on certain .dlls

When I run my py program it works the way I intended it to. If I am on a Linux box and build an executable using Pyinstaller, it builds without issue and executes without issue. I have scoured the Pyinstaller docs, git, etc. none of the posted fixes helped
I am still very new at python and feel like it might be a simple fix and might be over thinking the issue
Why can I no build a functional .exe on a windows based system using pyinstaller?
Windows 10 system
Pyinstaller version 3.2
Python version 3.5.2
This is a GUI program using appJar which is also up to date.
The file does build, but errors "Could not execute script"
EDIT
Not sure if this is best to edit in line like this but...
So studying the output and making adjustments, the issue seems to be appJar.py. For some reason it is missing assets, I am looking into it. The trouble is that I am still not used to looking at this kind of output and am not sure where to start.
C:\Users\_User_>C:\temp\fileCreatorGUI\fileCreatorGUI.exe
Traceback (most recent call last):
File "F:\Users\_User_\python_working\fileCreatorGUI.py", line 73, in <module>
app = gui()
File "C:\Users\_User_\AppData\Local\Programs\Python\Python35\lib\site-packages\appJar\appjar.py", line 509, in __init__
self.topLevel.wm_iconbitmap(self.appJarIcon)
File "C:\Users\_User_\AppData\Local\Programs\Python\Python35\lib\tkinter\__init__.py", line 1716, in wm_iconbitmap
return self.tk.call('wm', 'iconbitmap', self._w, bitmap)
_tkinter.TclError: bitmap "C:\temp\fileCreatorGUI\appJar\resources\icons\favicon.ico" not defined
Failed to execute script fileCreatorGUI
Edit 2
See answer below, but I was barking up the wrong tree on this one,
The Pyinstaller output chokes on the .dll's:
api-ms-win-core-console-l1-1-0.dll
api-ms-win-core-datetime-l1-1-0.dll
(There are like ~40 of these)
I added those .dll's to the python path, I declared them in the bianaries in the .spec file.
here is a truncated log:
2414 WARNING: Can not get binary dependencies for file: C:\Windows\system32\api-
ms-win-crt-stdio-l1-1-0.dll
Traceback (most recent call last):
File "C:\Users\_USER_NAME\AppData\Local\Programs\Python\Python35-32\lib\site-pa
ckages\PyInstaller\depend\bindepend.py", line 695, in getImports
return _getImports_pe(pth)
File "C:\Users\_USER_NAME\AppData\Local\Programs\Python\Python35-32\lib\site-pa
ckages\PyInstaller\depend\bindepend.py", line 122, in _getImports_pe
dll, _ = sym.forwarder.split('.')
TypeError: a bytes-like object is required, not 'str'
2423 WARNING: Can not get binary dependencies for file: C:\Windows\system32\api-
ms-win-crt-heap-l1-1-0.dll
I tried the fix listed here:
https://github.com/pyinstaller/pyinstaller/pull/1981
but it did not seem to make a difference.
Someone recommended adding the sys.path.insert() route but it did not make a difference either way
I also tried this in a VM with windows 7, clean install, no change. My next step is to try to use Wine in Debian, but I don't really want to go that route. Any help would be appreciated. Thank you
Turns out this was an appJar/packaging issue, the pyinstaller was not looking in the correct directory for the assets. per the dev of appJar, I commented out two lines of code in the appJar.py, lines 508-509:
if self.platform == self.WINDOWS:
self.topLevel.wm_iconbitmap(self.appJarIcon)
More on the specifics here: https://github.com/jarvisteach/appJar/issues/84
I probably can fix this by using the --path argument with pyinstaller but for the moment, the issue is fully resolved

Pygame breaks outside of Pydev.

Me and a team of friends created this game which I am now trying to run in linux,
We developed it using python 2.7 and Pygame in windows using Aptana studio and the code fully work when running through there.
When downloading it to linux It wouldn't load saying that it could not find files. I then tried running it via CMD in windows and there is the same error.
the error so far is
Traceback (most recent call last):
File "/home/user/Desktop/Raspberroids/mainmenu.py", line 144, in <module>
showMenu()
File "/home/user/Desktop/Raspberroids/mainmenu.py", line 107, in showMenu
menu.init(['Start','About','Quit'], surface)
File "/home/user/Desktop/Raspberroids/mainmenu.py", line 52, in init
self.create_strukture()
File "/home/user/Desktop/Raspberroids/mainmenu.py", line 73, in create_strukture
self.font = pygame.font.Font(self.font_path, self.fontsize)
IOError: unable to read font filename
And the source is at:
https://github.com/ryanteck/RasPiThon/tree/master/Raspberroids/Source%20Code
Happens on both 2.7 and 2.6
Can anyone help?
Your font path data/coders_crux/coders_crux.ttf is relative.
When you start your game from another directory than your source directory, pygame can't find the font.
A simple fix is to add the following lines to the top of your script (mainmenu.py):
import os
os.chdir(os.path.dirname(os.path.realpath(__file__)))
os.path.realpath(\__file__) will get the path to your script, and with os.chdir and os.path.dirname you change the current working directory to the directory of your script.
This way, the relative paths you use will work.
PyDev sets working directory and PYTHONPATH variable for your programs. It can also set console encoding to something different than OS defaults.
Add a print self.font_path statement before creating your Font object and see if path is OK. If it's relative path, you could also use os.path.abspath (see os.path docs for details) to get better understanding of what's going on.

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

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.

Can't create games with pygame

I make a game with python 2.5 and pygame.
but,I can't complete make.
because this errors occured.
C:\Python26\TypeType\src\dist\Main.exe:8: RuntimeWarning: use font: MemoryLoadLibrary failed loading pygame\font.pyd
Traceback (most recent call last):
File "Main.py", line 8, in <module>
File "pygame\__init__.pyo", line 70, in __getattr__
NotImplementedError: font module not available
Traceback (most recent call last):
File "Main.py", line 8, in <module>
File "pygame\sysfont.pyo", line 253, in SysFont
RuntimeError: default font not found 'freesansbold.ttf'
Perhaps I think that the reason is because it used an object called sysfont. (Because I had the programs that did not use sysfont on an execute file and was able to start with the PC which was not installed Python in)
What's wrong .
sorry I'm begginer.
EDIT
I can find Python2.5\Lib\site-packages\pygame\freesansbold.ttf
but same error occured..
Where may I copy freesansbold.ttf
A simple solution would be to simply load the font directly instead of using sysfont. Just use the pygame.font.Font class and directly load a ttf file. This will also make it easier to use py2exe, and you can choose exactly the font you want.
It looks like you don't have the freesansbold.ttf file on your computer in an accessible fonts folder. This font should have come with Pygame in the lib directory.
Check the installation folder for your copy of Pygame, and if it's there, modify your Python installation's font path to include that directory. If not, you'll need to find a copy or download a newer version of Pygame.
You probably found a solution but i want to give one for other.
Create you game with pygame. Then, unzip the librairy file.
Go to
PythonX.X\Lib\site-packages\pygame and copy freesansbold.ttf
Back to your library file that you unzipped and go to
library\pygame
Put freesansbold.ttf in and zip library.
Now, it's work ! :)

Categories