I have an image file , as C:/44637landscapes-2007.jpg
I want to load this file using QPixmap using PySide
I tried as following .
pixmap = QPixmap('C:/44637landscapes-2007.jpg')
But documentation says like QPixmap(':/xxxxxx.jpeg') . What does ':' means ?
How do I load an image at 'C:\' ?
EDIT : The problem was with trying to load "JPEG" . It was able to load "PNG" , with no issues. So what else , I need to do to load "JPEG" ?
Thanks
Jijoy
Qt does something a little weird with images. If you are using only pngs you will never have a problem. The handling of other image formats is handled a little differently.
If you take a look in your Qt directory (where all the binaries and source is) you will find a directory called: plugins\imageformats
In that directory there are a ton of dlls (or .so) one for each image format that Qt supports. In your case I presume the one that interests you is qjpeg4.dll
Now, when you run your application and you try to use a jpeg, Qt will try to load this plugin, but if it can't find it, then your image won't show up.
We had a similar problem with ico files. It worked in our development environment because the Qt folder was in our PATH, and therefore it magically found this plugin. However, when we were releasing our software we would distribute only those dlls that we needed, and therefore it didn't work.
After much investigation this is what we did:
Our application is located in a bin/ directory. This is where the exe, and all Qt dlls are put. QtCore4.dll and QtGui4.dll etc...
Under the bin directory we create a subdirectory called 'imageformats'
Put qjpeg4.dll and any other image plugins you might need.
Change the code as follows:
QApplication _App(argc, argv);
// Find the path of your bin directory - whereever your exe resides.
QString _Path = _PathToBin + QString(QDir::separator()) + "imageformats");
// This line is providing a path to Qt telling it to look here for plugins
QApplication::addLibraryPath(_Path);
// Initialize the main form
return _App.exec();
This should make sure that Qt is aware of the jpeg handling plugin, and load your image in the UI.
You can construct an instance of QPixMap by providing the path name of the file. What is the error you get when you try your code ? Change your code to something like:
pixmap = QPixMap(r'C:\filename.jpeg')
The documentation what you are referring to is the way you would load a Qt resource. One can convert an image that is needed by the application to a Qt resource so as to load it in a platform independent manner. For steps to do so refer to The Qt Resource System
documentation.
If you're trying to build your Python code to an executable this covers the issue for .ico files but the process is the same for JPEG:
PyQt/PySide - icon display
If you're having problems when running from your installed interpreter then your path variable probably isn't set correctly, your qt.conf (in the base python dir) has been deleted/changed or you don't have qjpeg4.dll where its supposed to be (<python dir>\Lib\site-packages\PySide\plugins\imageformats).
My qt.conf looks like:
[Paths]
Prefix = C:/Python27/lib/site-packages/PySide
Binaries = .
Plugins = plugins
Translation = translation
C:/Python27/lib/site-packages/PySide is part of my system PATH variable and ahead of any PyQt (or other Qt) directories.
Related
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')
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.
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
For some reason on my system (Windows XP 32-bit, Python 2.6) PyQt is able to display gifs perfectly when run in the python interpreter, but when I run it through py2exe, they no longer display.
I've tried everything I've googled: copying the gif DLLs from PyQt into an imageformats/ folder, setting up a qt.conf (as another stackoverflow thread suggested), done a setLibraryPaths to where the imageformat DLLs were, copied the setup file from http://wiki.wxpython.org/py2exe-python26 .
Nothing seems to work -- what on earth could I be doing wrong?
I'm not sure how you even managed to compile PyQt with py2exe; I had no success, and switched to pyinstaller.
Py2exe wasn't cooperating well PyQt and refused to compile depending on what widgets were featured.
I'd recommend switching to pyinstaller for compiling PyQt; see if it allows what you want in this case
from distutils.core import setup
import py2exe
DATA=[('imageformats',['C:\\Python26/Lib/site-packages/PyQt4/plugins/imageformats/qjpeg4.dll',
'C:\\Python26/Lib/site-packages/PyQt4/plugins/imageformats/qgif4.dll',
'C:\\Python26/Lib/site-packages/PyQt4/plugins/imageformats/qico4.dll',
'C:\\Python26/Lib/site-packages/PyQt4/plugins/imageformats/qmng4.dll',
'C:\\Python26/Lib/site-packages/PyQt4/plugins/imageformats/qsvg4.dll',
'C:\\Python26/Lib/site-packages/PyQt4/plugins/imageformats/qtiff4.dll'
])]
setup(windows=[{"script":"your_python_script.py"}],
data_files = DATA,
options={"py2exe":{
"includes":["sip", "PyQt4.QtNetwork", "PyQt4.QtWebKit", "PyQt4.QtSvg" ],
"bundle_files":3,
"compressed":True,
"xref":True}},
zipfile=None)
Just in case anyone is in this situation, I found the solution. These is what you need to do when compiling with py2exe so your image files are shown:
All the image files (gif, png, jpg) need to be copied to the dist folder
Qt dll files need to be copied from Qt installation folder to
dist\imageformats
For the dll files you need to set this on your setup.py file:
windows = [{
"script":"yourPythonScript.py",
"icon_resources": [(1, "nameOfIcoFile.ico")],
"dest_base":"nameOfExeFile"
}],
data_files = [
('imageformats',
[r'C:\Python27\Lib\site-packages\PyQt4\plugins\imageformats\qico4.dll',
r'C:\Python27\Lib\site-packages\PyQt4\plugins\imageformats\qgif4.dll'
])],
)
What is the best way to pack up an IronPython application for deployment? After scouring the web the best thing I've come up with (and what I'm currently doing) is using clr.CompileModules() to glue together my entire project's .py files into one .dll, and then having a single run.py do this to run the dll:
import clr
clr.AddReference('compiledapp.dll')
import app
This is still suboptimal, though, because it means I have to
distribute 3 files (the .dll, the .xaml, and the run.py launcher)
install IronPython on the host machine
Plus, this feels so... hacky, after the wonderful integration IronPython already has with Visual Studio 2010. I'm completely mystified as to why there is no integrated build system for IPy apps, seeing as it all boils down to IL anyway.
Ideally, I want to be able to have a single .exe with the .xaml merged inside somehow (I read that C# apps compile XAML to BAML and merge them in the executable), and without requiring IronPython to be installed to run. Is this at least halfway possible? (I suppose it's ok if the exe needs some extra .DLLs with it or something. The important part is that it's in .exe form.)
Some edits to clarify: I have tried pyc.py, but it seems to not acknowledge the fact that my project is not just app.py. The size of the exe it produces suggests that it is just 'compiling' app.py without including any of the other files into the exe. So, how do I tell it to compile every file in my project?
To help visualize this, here is a screenshot of my project's solution explorer window.
Edit II: It seems that unfortunately the only way is to use pyc.py and pass every single file to it as a parameter. There are two questions I have for this approach:
How do I possibly process a command line that big? There's a maximum of 256 characters in a command.
How does pyc.py know to preserve the package/folder structure? As shown in my project screenshot above, how will my compiled program know to access modules that are in subfolders, such as accessing DT\Device? Is the hierarchy somehow 'preserved' in the dll?
Edit III: Since passing 70 filenames to pyc.py through the command line will be unwieldy, and in the interest of solving the problem of building IPy projects more elegantly, I've decided to augment pyc.py.
I've added code that reads in a .pyproj file through the /pyproj: parameter, parses the XML, and grabs the list of py files used in the project from there. This has been working pretty well; however, the executable produced seems to be unable to access the python subpackages (subfolders) that are part of my project. My version of pyc.py with my .pyproj reading support patch can be found here: http://pastebin.com/FgXbZY29
When this new pyc.py is run on my project, this is the output:
c:\Projects\GenScheme\GenScheme>"c:\Program Files (x86)\IronPython 2.7\ipy.exe"
pyc.py /pyproj:GenScheme.pyproj /out:App /main:app.py /target:exe
Input Files:
c:\Projects\GenScheme\GenScheme\__init__.py
c:\Projects\GenScheme\GenScheme\Agent.py
c:\Projects\GenScheme\GenScheme\AIDisplay.py
c:\Projects\GenScheme\GenScheme\app.py
c:\Projects\GenScheme\GenScheme\BaseDevice.py
c:\Projects\GenScheme\GenScheme\BaseManager.py
c:\Projects\GenScheme\GenScheme\BaseSubSystem.py
c:\Projects\GenScheme\GenScheme\ControlSchemes.py
c:\Projects\GenScheme\GenScheme\Cu64\__init__.py
c:\Projects\GenScheme\GenScheme\Cu64\agent.py
c:\Projects\GenScheme\GenScheme\Cu64\aidisplays.py
c:\Projects\GenScheme\GenScheme\Cu64\devmapper.py
c:\Projects\GenScheme\GenScheme\Cu64\timedprocess.py
c:\Projects\GenScheme\GenScheme\Cu64\ui.py
c:\Projects\GenScheme\GenScheme\decorators.py
c:\Projects\GenScheme\GenScheme\DeviceMapper.py
c:\Projects\GenScheme\GenScheme\DT\__init__.py
c:\Projects\GenScheme\GenScheme\DT\Device.py
c:\Projects\GenScheme\GenScheme\DT\Manager.py
c:\Projects\GenScheme\GenScheme\DT\SubSystem.py
c:\Projects\GenScheme\GenScheme\excepts.py
c:\Projects\GenScheme\GenScheme\FindName.py
c:\Projects\GenScheme\GenScheme\GenScheme.py
c:\Projects\GenScheme\GenScheme\PMX\__init__.py
c:\Projects\GenScheme\GenScheme\PMX\Device.py
c:\Projects\GenScheme\GenScheme\PMX\Manager.py
c:\Projects\GenScheme\GenScheme\PMX\SubSystem.py
c:\Projects\GenScheme\GenScheme\pyevent.py
c:\Projects\GenScheme\GenScheme\Scheme.py
c:\Projects\GenScheme\GenScheme\Simulated\__init__.py
c:\Projects\GenScheme\GenScheme\Simulated\Device.py
c:\Projects\GenScheme\GenScheme\Simulated\SubSystem.py
c:\Projects\GenScheme\GenScheme\speech.py
c:\Projects\GenScheme\GenScheme\stdoutWriter.py
c:\Projects\GenScheme\GenScheme\Step.py
c:\Projects\GenScheme\GenScheme\TimedProcess.py
c:\Projects\GenScheme\GenScheme\UI.py
c:\Projects\GenScheme\GenScheme\VirtualSubSystem.py
c:\Projects\GenScheme\GenScheme\Waddle.py
Output:
App
Target:
ConsoleApplication
Platform:
ILOnly
Machine:
I386
Compiling...
Saved to App
So it correctly read in the list of files in the .pyproj... Great! But running the exe gives me this:
Unhandled Exception: IronPython.Runtime.Exceptions.ImportException:
No module named Cu64.ui
So even though Cu64\ui.py is obviously included in compilation, the exe, when run, can't find it. This is what I was afraid of in point #2 in the previous edit. How do I preserve the package hierarchy of my project? Perhaps compiling each package seperately may be needed?
I'll extend the bounty for this question. Ultimately my hope is that we can get a working pyc.py that reads in pyproj files and produces working exes in one step. Then maybe it could even be submitted to IronPython's codeplex to be included in the next release... ;]
Use pyc.py to produce app.exe and don't forget to include app.dll and IronPython libraries.
As for XAML - I've created project just for .xaml files that I compile in VS and then use them from IronPython. For example:
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/CompiledStyle;component/Style.xaml" />
</ResourceDictionary.MergedDictionaries>
It "boils down to IL", but it isn't compatible with the IL that C# code produces, so it can't be directly compiled to a standalone .exe file.
You'll need to use pyc.py to compile your code to a stub EXE with the DLL that CompileModules creates.
Then distribute those files with IronPython.dll, IronPython.Modules.dll, Microsoft.Dynamic.dll, Microsoft.Scripting.Debugging.dll, Microsoft.Scripting.dll, and of course the XAML file.
To compile other files, just add them as arguments:
ipy.exe pyc.py /main:app.py /target:winexe another.py another2.py additional.py
I posted a Python script which can take an IronPython file, figure out its dependencies and compile the lot into a standalone binary at Ironpython 2.6 .py -> .exe. Hope you find it useful. It ought to work for WPF too as it bundles WPF support.
To create a set of assemblies for your IronPython application so that you can distribute it you can either use pyc.py or SharpDevelop.
To compile using pyc.py:
ipy.exe pyc.py /main:Program.py Form.py File1.py File2.py ... /target:winexe
Given the amount of files in your project you could try using SharpDevelop instead of maintaining a long command line for pyc.py. You will need to create a new IronPython project in SharpDevelop and import your files into the project. You will probably need to import the files one at a time since SharpDevelop lacks a way to import multiple files unless they are in a subfolder.
You can then use SharpDevelop to compile your application into an executable and a dll. All the other required files, such as IronPython.dll, Microsoft.Scripting.dll, will be in the bin/debug or bin/release folder. SharpDevelop uses clr.CompileModules and a custom MSBuild task behind the scenes to generate the binaries.
Any IronPython packages defined in your project should be usable from your application after compilation.
Packaging up the XAML can be done by embedding the xaml as a resource. Then using code similar to the following:
import clr
clr.AddReference('PresentationFramework')
clr.AddReference('System')
from System.IO import FileMode, FileStream, Path
from System.Reflection import Assembly
from System.Windows import Application
from System.Windows.Markup import XamlReader
executingAssemblyFileName = Assembly.GetEntryAssembly().Location
directory = Path.GetDirectoryName(executingAssemblyFileName)
xamlFileName = Path.Combine(directory, "Window1.xaml")
stream = FileStream(xamlFileName, FileMode.Open)
window = XamlReader.Load(stream)
app = Application()
app.Run(window)
SharpDevelop 3.2 does not embed resource files correctly so you will need to use SharpDevelop 4.
If you are using IronPython 2.7 you can use the new clr.LoadComponent method that takes an object and either a XAML filename or stream and wires up that object to the XAML.
Whilst the C# compiler can compile your XAML into a BAML resource doing the same with IronPython has a few problems. If you do not link the XAML to a class via the x:Class attribute then it is possible to compile the XAML into a BAML resource and have that embedded into your assembly. However you will not get any autogenerated code so you will need to create that code yourself. Another problem is that this will not work out of the box with SharpDevelop. You will need to edit the SharpDevelop.Build.Python.targets file and change the from Python to C#. Trying to use the x:Class attribute will not work since the BAML reader cannot access any associated IronPython class. This is because the generated IL in the compiled IronPython application is very different to that in a C# or VB.NET assembly.
I installed Visual Studio 2015 with PTVS (ironpython 2.7). I created a very simple WPF project and wasn't able to compile an exe. I always got the exception "ImportError: No module named wpf".
import clr
clr.AddReferenceToFileAndPath("c:\\path\\to\\IronPython.Wpf.dll")
clr.AddReferenceToFileAndPath('c:\\path\\to\\PresentationCore.dll')
clr.AddReferenceToFileAndPath('c:\\path\\to\\PresentationFramework.dll')
clr.AddReferenceToFileAndPath('c:\\path\\to\\WindowsBase.dll')
from System.Windows import Application, Window
import wpf
class MyWindow(Window):
def __init__(self):
wpf.LoadComponent(self, 'RegExTester.xaml')
def OnSearch(self, sender, e):
self.tbOut.Text = "hello world"
if __name__ == '__main__':
Application().Run(MyWindow())
The fault I got was because the clr clause must be before the import wpf. Steps to compile it:
install pip for CPython 2.7 (not ironpython!)
install ipy2asm
python -m pip install ironpycompiler
compile the application like
ipy2asm compile -t winexe -e -s program.py