cx_freeze over Python Wand (imagemagick) - python

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.

Related

Converting a Python project to DLL or decreasing the size and imports

I have a python project for OCR MRZ detection with 2 modules 1 is for ID which uses EasyOcr,pythorch and other one is for Passport documents which uses Pytesseract and tensorflow.
I need to prepare this project for deployment I have tried some methods but none of them was practible for deployment process.
I have tried pyinstaller with couple of configurations with --onefile
option the setup is great but it takes too long to unpack the exe
when executed.
I have then tried --onedir option the delay was gone but now
installation package was too complicated and size was too
large(1.8GB).
I have tried to "compile" python code by using Cython but even with a
helloworld.py sample app I couldn't manage to make this one work I
got couple of errors during gcc compiling the last error I got due to
msvcp package which i have installed but still got the error.
And as last I have used Nuitka to get a dll-like file to import this
in C# and use it like a package, I have successfully created a test
.pyd file from a helloworld.py but i couldn't import it in C# as i
planned.
What I need is to prepare this project as a simpler and low-sized application which is hard to reverse-engineered for source codes ready for deployment. For passport OCR I can switch development to C# but for ID I couldn't find any alternative OCR library to get the MRZ information so at least I need to use ID OCR module from my Python project.
Any help would be appreciated,
Thanks

Python - how to USE additional installed package data

I have a PyQt application which needs to show some images. I saw in:
How can I include package_data without a MANIFEST.in file?
A way to include the image files in the package installation.
It works fine in the aspect that /usr/local/lib/python2.7/dist-packages/mypacakge/images/ now contains all the files. Unfortunately images are still not being shown by the app.
My current code addresses the images files as relative paths (i.e: images/foo.jpeg) which I guess is wrong. But I have no idea what is the right way to address them (unless I want to use the absolute /usr/local/lib/python2.7/dist-packages/mypacakge/images/foo.jpeg which I believe I don't). This seems like a common enough issue, yet I can't find any resources/answers on how to do that.
Module pkg_resources has support to locate a file contained in a package:
import pkg_resources
path = pkg_resources.resource_filename('<package name>', 'images/foo.jpeg')

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

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

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