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

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

Related

Is there a way to capture an image of the screen in python with ctypes?

I want to have python save an image file of the whole screen as a variable with ctypes so that I could access the screen in a program and do something with it (like put it on a pygame window). I can't use any other libraries unless they are included with python (no installing or pip). Does anyone know how to do this?
Edit: I'm using windows 10.
PIL.ImageGrab is from PILLOW (a python image library fork which you can install with pip). You can give a bounding box or capture the entire screen.
Update: OP now mentions he can't use external libraries.
Then you could virtually hit printscreen and read the clipboard. The code of PILLOW is open-source feel free to use it.
Remember that you can always call a command from within python:
>>> import os
>>> os.system("pip install pillow")
Or download the zip of the library and import it in your code.

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

Python PIL import Mac compatiblity

I wonder now for a long time, can I redirect python imports.
So on my Machine I did:
pip install pil
and then I can do:
from PIL import Image
That works great but the problem is many programs just want Image, they want:
import Image
Now my question is can I "redirect" the above statement so that I can use import Image?
If that does not work, how could I make it work?
Basically you can use any of the methods mentioned here.
On my linux installation, PIL already uses one of those - there's a directory called PILcompat which has files like Image.py, and a file called PILcompat.pth which is used to tell python to add this directory to its module search path.
This way when
import Image
is executed, python finds PILcompat/Image.py which executes its contents -
from PIL.Image import *
I'm guessing either the .pth file or the directory are missing on your machine.

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.

SDL/Pygame failing to load PNG images with cx_Freeze

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"

Categories