SDL/Pygame failing to load PNG images with cx_Freeze - python

I'm running Python 3.1 on Windows and I'm trying to distribute my Pygame script as an executable via cx_Freeze. Right now it seems to be working except that the exe build can't load any of my images:
Cannot load image: C:\path\to\build\exe.win32-3.1\resources\image.png
File is not a Windows BMP file
Googling has revealed that this happens when the SDL imaging library doesn't get included correctly. However, SDL_image.dll and libpng12-0.dll are both put by cx_Freeze into my build directory, so it seems to me like everything should be fine. Why wouldn't it be able to load PNG images?
EDIT: I "solved" this problem by porting my script to Python 2.6 and using py2exe instead since it had some functionality anyway that I needed.

I encountered the same issue many times, but I found out how to deal with it.
The problem
It seems that there is a conflict between two possible dependencies. The file jpeg.dll is included from the JRE (on Windows, something like C:\Program Files\Java\jre6\bin\), but it is the wrong one. It should be included from the Pygame directory, located within your Python installation, at C:\Python31\lib\site-packages\pygame\. Don't know why cx_Freeze prefers the one from the JRE, though…
How to fix it?
It is quite easy. Just copy the correct file (the one from Pygame) to the directory in which you execute the cx_Freeze script. When you will start it, the script will look in the current directory first, and will find the correct jpeg.dll. Your executable should be able to import PNG images now.

Test by inserting some python code to display one message indicating that the libraries have loaded and another message to indicate that their loading resulted in an error.
try:
import SDL_image
print "Loaded SDL_image"
except:
print "Failed to import SDL_image"
try:
import libpng
print "Loaded libpng"
except:
print "Failed to import libpng"

Related

Create a python 3 exe with pyinstaller with Pmw module

Based on this Previos Post I'm trying to figure out how to make an exe out of my python files. The main issue seems to be that Pmw and its modules do not seem to import correctly though pyinstaller. The main error says:
FileNotFoundError: [WinError 3] The system cannot find the path specified: 'C:\User\Name\AppData\Local\Temp\_MEI61522\Pmw
Looking at the Pmw init it seems what it's doing is looking for files in the directory which have Pmw_### and looking in there which has lib and then PmwLoader.suffix and loadther then imports all the dependencies
So now it seems pyinstaller isn't understanding this file dependency system created by Pmw and when I tried to follow the previous post instruction I ran into other errors that made the code unable to compile.
If anyone has any insight on this, that would be fantastic
PMW has a script for bundling (almost) everything into one file which I found in Lib\site-packages\Pmw\Pmw_\bin\bundlepmw.py
My version wasn't entirely Python 3 compliant so I had to make a few edits to the file before adding it to my sources.
Ugly but it worked for me.
Welp, I gave up on pyinstaller and downgraded to python 3.4 and used cx_freeze and created created a single Pmw.py file from some code which I found from python-pmw-and-cx-freeze

package a pyqt desktop application its resource file with pyinstaller

I have created a desktop gui application which i want to package. I was directed to use pyinstaller to package it so i did. I have a mainwindow.py file where i import a resource file (converted to python code) as well as other imports going on in there. However when i run the output file,it gives me an error. I suspected it was due to my resource file, from the error so i tried pyinstaller on one of the modules and it worked perfectly. however i keep getting an error with the MainWindow.py file. I have the image of the error here for your consideration. How do i go around this error?
I have managed to solve the problem. After reading this answer, I tried pyrcc4 with the '-py3' flag and it worked. I think without the py3 flag,the resource file is converted into a python 2.x code. And I was trying to interpret py2 code with py3 interpreter. That's my understanding of the problem tho. But in case you have a similar problem,try using the -'py3' flag for python3 interpreter.Good Luck

PyAudio issues with AudioPort PYD

I'm trying to put PyAudio on Windows (yes, I know. It is the worst trying to do such) and I've hit a wall. I'm using Python 3.4 64 bit on Windows 10. Knowing the issues with PyAudio's Windows support I downloaded the Windows binary from http://www.lfd.uci.edu/~gohlke/pythonlibs/#pyaudio. More specifically - PyAudio‑0.2.8‑cp35‑none‑win_amd64.whl. After extracting the file I copied the contents into site-packages and just to try copied the entire folder into site-packages (with contents) and changed the folder name to PyAudio.
The contents are:
pyaudio.py
_portaudio.cp35-win_amd64.pyd
PyAudio-0.2.8.dist-info(folder){
DESCRIPTION.rst
METADATA
metadata.json
pbr.json
RECORD
top_level
WHEEL
}
However, when I try to run PyAudio I get the following import error "No module named '_portaudio'" when it attempts to import as shown below.
try:
import _portaudio as pa
except ImportError as e:
print(e)
print("Please build and install the PortAudio Python " +
"bindings first.")
sys.exit(-1)
So, to attempt to fix the error I renamed the pyd to _portaudio and went into the pyd to change the name to _portaudio before the PYInit__portaudio call as well. However, in doing that it attempts to read it as a 32 bit version and gives me the error "DLL load failed: %1 is not a valid Win32 application". Unfortunately, I can't copy and paste _portaudio.cp35-win_amd64 as an import in pyaudio.py because the syntax attempts to resolve the - as a statement. From this point I can't think of anything else to get it working. Any tips would be awesome, thanks!
EDIT:
I intentionally put the statement (yes, I know. It is the worst trying to do such) in the post because I know that a linux environment would be a better choice but it wasn't an option. I just didn't want to get comments or answers suggesting I run my project on Ubuntu so please don't remove it. :)

cx_freeze over Python Wand (imagemagick)

Wand needs the ImageMagick library to perform. When I do as they explain here my code works just fine on my computer.
However, when I freeze it with cx_freeze it misses the extra library. I don't know how to add that library to the zip. I know how to add .dll files into the folder, but I can't figure out which .dlls Wand needs. When I tried putting all the .dlls in the folder it ran but it couldn't convert images.
Update:
So I included CORE_RL_wand_*.dll and CORE_RL_magick_*.dll in the folder. Running it I gives me an error:
can't start because CORE_RL_bzlib_.dll is missing
I added that one and 20 others as it needed them. After that the app starts, but when I try to open and resize and covert an image, I get:
wand\resource.py line 223, wand.exception.MissingDelegateError: NoDecodeDelegateForThisImageFormat 'pathblah.jpg' # error/constitute.c/ReadImage/552
I don't know how to get rid of this.
You need to include CORE_RL_wand_*.dll and CORE_RL_magick_*.dll to the zip.

X11 Tkinter + PIL + py2app = IOError cannot identify image file

I have a problem with a python program (python 2.7.3, X11 Tkinter, py2app 0.6.4, MacOS X 10.7.4) that I'm trying to export to py2app. The problem only started occurring in the standalone py2app-ified app version of the program. When I run the python source file from which the app was created, the problem does not exist, so I feel it must have something to do with the py2app export.
The problem: When I start the GUI, the first time I try to load a valid image file, the image fails to load, and I get the following error from the PIL Image module:
File "Image.pyc", line 1980, in open
IOError: cannot identify image file
When I then (without closing the GUI or anything) try to open the exact same file, it loads perfectly, no errors or problems. This happens every time, with any image file I try - the first attempt to load fails, subsequent attempts succeed. I should add that after that first error, no image files ever fail to load - even if they are different from the first one.
A few notes:
- The image file is a sequence, and is very large (around 300 MB), so to speed up the loading process, I use a mmap. I have tried removing the mmap step, and handing a regular file object directly to ImagePIL.open it directly, and the problem is unaffected.
- I also tried seeking to the beginning of the file before giving it to ImagePIL.open, but that had no effect.
- The py2app setup file is pretty vanilla - it just includes a few config files and an icon.
Here is the relevant part of the offending image load function:
import Image as ImagePIL
import mmap as m
...
...
def loadImage(self):
errorLog.debug("Attempting to open image \""+self.filenameVar.get()+"\"")
try:
if self.fileMap is not None:
self.fileMap.close()
imageFile = open(self.filenameVar.get(), 'r')
self.fileMap = m.mmap(imageFile.fileno(), 0, prot=m.PROT_READ)
# self.fileMap.seek(0)
self.imageSeries = ImagePIL.open(self.fileMap)
imageFile.close()
except(IOError):
errorLog.exception("Failed to open image \""+self.filenameVar.get()+"\"")
return
I'm pretty stumped - any ideas? Thanks in advance!
Edit: I should add that Tkinter, PIL, and py2app were installed using MacPorts 2.1.2, in the off chance that helps.
It seems that py2app does not include PIL's image plugins into the application bundle even though one of the py2app recipes tries to ensure that they are included.
One thing you could try is to build with "python setup.py py2app --packages=PIL" and then use "import PIL.Image as ImagePIL" to use it.
I don't understand yet why the PIL recipe doesn't work, it might be something in the way MacPorts builds python packages (I don't use MacPorts myself).
The problem is the result of inconsistency between Pillow version 3.0.0 and py2app.
I suggest two solution to avoid PIL (Pillow)
Use opencv instead of PIL.
uninstall the current version of Pillow and install a previous one like 1.7.8

Categories