Tkinter couldn't recognize data in image file - python

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.

Related

Python Script Failing to Open Firefox.lnk file on Desktop Path

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)

How can i connect open cv file (for playing video) when i click push button in an UI file?

I tried importing cv2. But When I import it in my python file with ui, it overwrite the code, and it is the first one to run, which supposedly, it should be the other python file. It should just be run when only i click the push button. Kindly help me. :< Beginner here.
Or do you have other ideas on how to play external video like mp4 which is also saved in the same folder.
I'm using qtdesigner, python.

How to use python PIL ImageFont.truetype()?

I'm trying to load a font through the ImageFont.truetype() command in the PIL python package.
I've looked at the documentation here: https://pillow.readthedocs.io/en/stable/reference/ImageFont.html?highlight=imagefont but can't figure out what I'm doing wrong.
I've downloaded the font I want, put it in the same directory as my python script (which is also my working directory). My code is:
from pillow import ImageFont
font = ImageFont.truetype('Roboto-Bold.ttf', size=10)
I get this error:
File "C:\Users\myusername\AppData\Local\anaconda3\lib\site-packages\PIL\ImageFont.py", line 161, in __init__
font, size, index, encoding, layout_engine=layout_engine
OSError: cannot open resource
I've tried other .ttf files, adding my working directory to sys.path, and using the full path to .ttf file. All those attempts yielded the same error. Any ideas what I've done wrong?
Okay. It looks like I had a piece of code that ran ImageFont.truetype('Roboto-Bold.ttf', size=10) too many times so the operating system eventually stopped opening the font file correctly.
Don't be dumb like me and read in fonts in a recursive function.
Related reading: https://github.com/python-pillow/Pillow/issues/3730

Python Zelle graphics - image error

Using a mac, I am trying to set the background of my program to an image that i downloaded, I called the image "ChristmasBackground.jpg" and saved it to my documents. Using Zelle graphics, this is what I wrote:
XmasBack = Image(Point(600,100),"ChristmasBackground.jpg")
XmasBack.draw(win)
but the program is giving me an error saying:
couldn't open "ChristmasBackground.jpg": no such file or directory
just put it in the same folder of your script or write the file path

QLabel() won't load a pixmap if it is a JPG image

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()

Categories

Resources