I am trying to have a QLabel() display a pixmap JPG image from a file (which can't be in a resource file since it is downloaded from the web), but I am having problems loading it. The code is fairly simple:
label = QLabel()
label.setPixmap(QPixmap("image.jpg"))
It works with PNG files, but it doesn't work with JPG files. I have Googled quite a bit and found that I needed to place the "imageformats" folder in the same folder where my script is located. However, even after doing this (and yes, qjpeg4.dll and others are there), it still doesn't work. I've also tried doing
path = r"C:\Users\Deusdies\Documents\Work\myapp\imageformats"
app.addLibraryPath(path)
but that didn't help either.
Again, it loads PNGs just fine, but it won't load JPGs. I've also noticed even before that it won't load ICO either, but I thought of it as an unrelated issue - however it doesn't seem that way now.
It is worth noting that the application is not converted to an .exe at this point - it is ran through python.exe interpreter via PowerShell.
My development environment is Windows 7 x64, PySide 1.1.0
How can I solve this problem?
I solved the problem. First, path should look like this:
path = r"C:\Users\Deusdies\Documents\Work\myapp"
(so without the "imageformats" part)
And second, I was an idiot. I created an instance of the QDialog() class before doing the addLibraryPath()
Related
TLDR; I have a python script that uses autopygui to click on a file and open it, but it does not work with lnk files located in the same path.
I have a little script using pyautogui to screenshot a desktop icon and save it as a png image on the desktop and doubleclick it to open that image. This works perfect.
However..
I tried implementing the same script on an existing Firefox.lnk shortcut and it fails with the following error:
locateOnScreen Error
Things I have tried:
Running VS code as admin.
locateOnCenterScreen and locateOnScreen.
I have tried others, but I'm blanking because I have tried everything I can think of.
It should open my Firefox.lnk shortcut up, it does not. It only works on png file.
It's because the locateOnScreen(...) function only accepts paths to image files, and .lnk files are not image files. If you manually took a screenshot of the Firefox icon on your desktop, saved it as a PNG file, and passed that to your script, it would open fine.
If you want to launch a program using a .lnk file from Python, you don't need pyautogui at all. Consider using os.startfile(...) instead.
You should put the path of png file in locateCenterOnScreen(...) function instead.
Please also make sure you minimize all active windows so that pyautogui could locate your Firefox short icon.
import pyautogui
pyautogui.hotkey('winleft','d')
icon_location = pyautogui.locateCenterOnScreen('.../FireFox_icon_screenshot.png', confidence=0.9)
pyautogui.moveTo(icon_location)
pyautogui.click(clicks=2)
I started playing around with Pygame today, and while following a guide that instructed me to change the game icon I ran into an issue with the line:
img = pygame.image.load("icon.bmp")
My code works fine without this line, but with it the program instantly crashes.
I also noticed that before the crash the icon starts as "icon.bmp", but then changes into the default one. I have a block of code afterwards that stops the program from exiting automatically after running the code, and the window remains on screen when I comment the problematic line.
My guess is maybe a problem with the file path. The image is in the same folder as the script. I am not sure how to make a relative file path, but earlier when I used an absolute path and the issue persisted.
I can't find anyone else with the same problem, and I am getting frustrated at getting stuck on such a simple issue, so in my desperation, I've come here for help. I am sorry for the newbie question, I've never written on stackoverflow.com before.
The file path has to be relative to the current working directory. The working directory is possibly different to the directory of the python file.
It is not enough to put the files in the same directory or sub directory. You also need to set the working directory.
The name and path of the file can be get by __file__. The current working directory can be retrieved with os.getcwd() and can be changed with os.chdir(path):
import os
os.chdir(os.path.dirname(os.path.abspath(__file__)))
When I run: pyinstaller game.py it works fine. I copy and paste my resources folder into dist and the game runs perfectly.
However, there's so many other files in the dist folder that it would be confusing for people to find the file to click on, so I wanted to use --onefile.
I tried to do so and pasted the resource folder into dist and tried running the game, the console window appeared for a second then disappeared almost immediately. (I'm assuming there's an dependency I'm missing, but how can that be? it works fine without --onefile)
Does --onefile not work with my layout for importing images and fonts or something?
Images:
if getattr(sys, 'frozen', False):
CURRENT_DIRECTORY = sys._MEIPASS
else:
CURRENT_DIRECTORY = os.path.dirname(os.path.abspath(__file__))
bg = (pygame.image.load(os.path.join(CURRENT_DIRECTORY, r"Resources\backgroundlvl0.png")),
pygame.image.load(os.path.join(CURRENT_DIRECTORY, r"Resources\backgroundlvl1.png")),
pygame.image.load(os.path.join(CURRENT_DIRECTORY, r"Resources\backgroundlvl2.png")),
pygame.image.load(os.path.join(CURRENT_DIRECTORY, r"Resources\backgroundlvl3.png")))
Fonts:
font = pygame.font.Font(os.path.join(CURRENT_DIRECTORY, r"Resources\arial.ttf"), 36)
Well, it took ages but I believe I now have the answer to my question for anyone interested.
When running my game in my IDE, it would work fine and I would import relevant pictures like this:
bg = (pygame.image.load(os.path.join(CURRENT_DIRECTORY, r"Resources\backgroundlvl0.png")),
pygame.image.load(os.path.join(CURRENT_DIRECTORY, r"Resources\backgroundlvl1.png")),
pygame.image.load(os.path.join(CURRENT_DIRECTORY, r"Resources\backgroundlvl2.png")),
pygame.image.load(os.path.join(CURRENT_DIRECTORY, r"Resources\backgroundlvl3.png")))
For some reason, if I moved my resources folder and the game.py, to another directory, when i ran the game, I would get a nameError: name 'os' is not defined python. This is a mystery to me however, I also noticed that pyinstaller does not like this format of importing images and therefore, would not work for --onefile.
Therefore, I tried this alternative:
bg = (pygame.image.load(os.path.join('Resources', 'backgroundlvl0.png')),pygame.image.load(os.path.join('Resources', 'backgroundlvl1.png')),pygame.image.load(os.path.join('Resources', 'backgroundlvl2.png')),pygame.image.load(os.path.join('Resources', 'backgroundlvl3.png')) )
This for some strange reason, works in my IDE, however, when I right click the .py file and press open with -> python 3.8, it crashes. Regardless, it seemed to work for pyinstaller.
What to take away:
If you are using pyinstaller and you want one single file, use pyinstaller --onefile name.py. Ensure that the folder you use which has your dependencies is referred to in your name.py with os.path.join('foldername', 'file'). I had to do this specifically: pygame.image.load(os.path.join(....
For the past month I've been writing a desktop application for MacOS using Python. It requires opening files and saving compressed data to them. This application creates files using a my own made up extension, so the files are not usable for for other applications. I have almost everything figured out. However, I want to make it so that I can right click on a file with the extension and open it with my python application. I tried using sys.argv to get any arguments for the path of the file to open, but that doesn't work. I know there has to be a way. Preferably there's a builtin variable that is easy to use, but I haven't found anything that helps.
Any help would be useful.
Thanks.
I was using the turtle module with Python to make a project. In the project, an image is displayed, with this line:
screen.bgpic("image.png")
This worked at first, but then I copied the files (both the python file and image.png) onto a flash drive. I tested it on another computer, and it worked fine there, but when I saved it onto the actual computer that I needed it to be on, it didn't work.
I know it's not a problem with the actual turtle, since it works before that part. I also am pretty sure it isn't a file problem since the same file worked before. It says:
Couldn't recognize data in image file "image.png"
Older versions of tkinter don't support .png files.
You can convert GIF or JPEG or another image file type with an external tool.