PySide6: fail to load "jpg" format in QPixmap - pyqt

I'm trying to load "jpg" picture on the QLabel in PySide6. So I used this:
mypic = QPixmap()
mypic.load("./test.jpg")
self.ui.mylabel.setPixmap(QPixmap(mypic))
However, I found there is empty in mylabel. When I debug in this program, I found that my pic is null (even after it loads the image file).
So I wonder if the jpg is not automatically supported in PySide6.
As I checked the documention of PySide6, it illustrates that the default supporting image formats include "jpg".
enter image description here
As I print the supportedImageFormats using:
print(QtGui.QImageReader.supportedImageFormats())
It returns with this:
[PySide6.QtCore.QByteArray(b'bmp'), PySide6.QtCore.QByteArray(b'pbm'), PySide6.QtCore.QByteArray(b'pgm'), PySide6.QtCore.QByteArray(b'png'), PySide6.QtCore.QByteArray(b'ppm'), PySide6.QtCore.QByteArray(b'xbm'), PySide6.QtCore.QByteArray(b'xpm')]
I found it does not include the "jpg" format by default!
Then I searched a lot of solutions, I found that this can solve my problem: add one line of code to introduce the "imageformats" plugins in PySide6 folder.
app.addLibraryPath(os.path.join(os.path.dirname(QtCore.__file__), "plugins"))
Here I printed the os.path.dirname(QtCore.__file__), and found it was my PySide6 folder.
I checked the plugins folder, and found the inner imageformats folder, and found these dlls:
enter image description here
It seems that the jpg format is supported here. So when I add the plugin path, it can solve my problem. But it still confuses me that why I should add the plugin path (The official documentation implies that this format is supported by default!).
I wonder if there is a more convincing solution because adding one line of code in my program every time seems clumsy. Or if I left out something. I sincerely want to get your help. Thank you!

Related

Pycharm path issues

Im testing the very first line of a text extraction program in Python with PyCharm. With only a couple lines of simple code, the editor is raising errors on nearly every word
Ive done some googling about path configuration and tried reading the official documentation as well. I think is has something to do with Working Directory? But I can't find a clear definition for the term and dont want to sabotage myself by ignorantly messing with path variables
import os
test = os.listdir(
/Users/.../Library/Application Support/Steam/steamapps/common/Europa Universalis IV/history/countries
)
print(test)
The errors are: UNUSED IMPORT STATEMENT and UNRESOLVED REFERENCE (on every term in the path and os)
What am I doing wrong? I imported and used a different module this afternoon with 0 issues on a separate file. Ive changed none of the interpreter or config settings between the last file and this one.
In pycharm, you need to go to File -> Settings -> Under Project(current project name) -> Project Structure and "Add Content Root" of all the directories that you import your files from.
And make sure you check the option for "add content roots to PYTHONPATH"(File-> Settings-> Python Console from the Console dropdown) as following:

How to build python class structure for matplotlib to export ot .exe with cx_freeze?

I built a code to generate and play random musical notes. It is working great within python, but I would like to make it into an .exe stand alone program, so people without python can use it. I show an image below of the output. It creates a matplotlib figure with a 'TkAgg' backend. There are 5 buttons and an user entry box that all work.
I used cx_freeze to try to package it, and I worked through all of the errors. I also got some of the examples to work. I can see the the build folder is getting the 4 Images and many .wav files I need to draw the musical staff and play the notes. One error showed that the .exe tried to run my code, because it couldn't find the .wav files). I changed how I specified where they were for the .exe. But now when I run the .exe nothing happens.
Unfortunately my code is a monstrosity. It's messy, and somewhat long (750 lines if you count white space). The .py file I am trying to write to the .exe is Interval_Trainer_v1_1.py. It can be found here.
Because it works in python, but not in the .exe, I thought it might have to do with my ignorance of how to use classes in conjunction with plotting well. Basically I call the class, and then initialize a bunch of things so I can refer to them later. That allows me to delete notes I've plotted before, old answers, etc.
How can I practice building up 'TkAgg' backended figures that will execute properly after cf_freeze? I feel like I need to start with some basic ideas and build up to my application, which is fairly complex.
One note, I do use pygame for the sounds.
Here is my setup file:
from cx_Freeze import setup, Executable
import os
os.environ['TCL_LIBRARY']=r'C:\Users\Bart\Anaconda3\tcl\tcl8.6'
os.environ['TK_LIBRARY']=r'C:\Users\Bart\Anaconda3\tcl\tk8.6'
import sys
base = None
if sys.platform == 'win32':
base = 'Win32GUI'
additional_mods = ['numpy.core._methods', 'numpy.lib.format',"matplotlib.backends.backend_tkagg", 'matplotlib.pyplot', 'matplotlib.image', 'matplotlib.widgets']
setup(
name = "Interval Trainer",
version = "1.0.0",
author = "Bart",
author_email = "bcubrich#gmail.com",
options = {"build_exe": {'includes': additional_mods,"packages":["pygame","tkinter",'random'],
"include_files": [
'Images/F cleff 8vb.png', 'Images/F cleff.png',
'Images/G cleff 8vb.png', 'Images/G cleff.png',
'Pitches/A#1.wav', 'Pitches/A#2.wav', 'Pitches/A#3.wav',
'Pitches/A#4.wav', 'Pitches/A#5.wav', 'Pitches/A1.wav',
'Pitches/A2.wav', 'Pitches/A3.wav', 'Pitches/A4.wav',
'Pitches/A5.wav', 'Pitches/Ab1.wav', 'Pitches/Ab2.wav',
'Pitches/Ab3.wav', 'Pitches/Ab4.wav', 'Pitches/B#2.wav',
'Pitches/B#3.wav', 'Pitches/B#4.wav', 'Pitches/B1.wav',
'Pitches/B2.wav', 'Pitches/B3.wav', 'Pitches/B4.wav',
'Pitches/B5.wav', 'Pitches/Bb1.wav', 'Pitches/Bb2.wav',
'Pitches/Bb3.wav', 'Pitches/Bb4.wav', 'Pitches/C#2.wav',
'Pitches/C#3.wav', 'Pitches/C#4.wav', 'Pitches/C#5.wav',
'Pitches/C2.wav', 'Pitches/C3.wav', 'Pitches/C4.wav',
'Pitches/C5.wav', 'Pitches/C6.wav', 'Pitches/D#2.wav',
'Pitches/D#3.wav', 'Pitches/D#4.wav', 'Pitches/D#5.wav',
'Pitches/D2.wav', 'Pitches/D3.wav', 'Pitches/D4.wav',
'Pitches/D5.wav', 'Pitches/Db1.wav', 'Pitches/Db2.wav',
'Pitches/Db3.wav', 'Pitches/Db4.wav', 'Pitches/E#2.wav',
'Pitches/E#3.wav', 'Pitches/E#4.wav', 'Pitches/E1.wav',
'Pitches/E2.wav', 'Pitches/E3.wav', 'Pitches/E4.wav',
'Pitches/E5.wav', 'Pitches/Eb2.wav', 'Pitches/Eb3.wav',
'Pitches/Eb4.wav', 'Pitches/F#1.wav', 'Pitches/F#2.wav',
'Pitches/F#3.wav', 'Pitches/F#4.wav', 'Pitches/F#5.wav',
'Pitches/F1.wav', 'Pitches/F2.wav', 'Pitches/F3.wav',
'Pitches/F4.wav', 'Pitches/F5.wav', 'Pitches/G#1.wav',
'Pitches/G#2.wav', 'Pitches/G#3.wav', 'Pitches/G#4.wav',
'Pitches/G#5.wav', 'Pitches/G1.wav', 'Pitches/G2.wav',
'Pitches/G3.wav', 'Pitches/G4.wav', 'Pitches/G5.wav',
'Pitches/Gb1.wav', 'Pitches/Gb2.wav', 'Pitches/Gb3.wav',
'Pitches/Gb4.wav']}},
executables = [Executable("Interval_trainer_v1_1.py", base=base)],
)
Output Image
Any help is appreciate.
See the matplotlib user interfaces examples embedding_in_tk and embedding_in_tk2 to practice building up TkAgg backended figures.

Why is bodymovin exporting PNG's and not SVG's?

I'm trying to use lottie to animate an SVG animation created in After Effects. I use the bodymovin extension to export the JSON data file. But, I also noticed the export includes some PNG images. I'm also getting console errors that said PNG's can not be found.
Why is it exporting PNGs as I'm using SVG (an AI file) in AE. Below is my code, and the error.
index.ts
import * as lottie from 'lottie-web';
import * as header from './assets/header.json';
import './css/base.sss';
var animation = lottie.loadAnimation({
container: document.getElementById('header'),
animationData: header,
renderer: 'svg/canvas/html',
autoplay: true
});
But I'm getting the following errors that the images can't be found. Why is bodymovin exporting/looking for pngs? I require SVG's.
Chrome console errors (sorry can't embed until 10 rep)
I found the problem. I have to convert paths to shapes in AI as noted here.
In Adobe After effect, you can use Create Shapes from vector layer
Another way I found was to search the exported .json file for the .png extension and replace the found extensions with .svg. Making sure to add the .svg files to the same directory.
I used this bodymovin render option:
I did my search in the .json file in Dreamweaver like so:
I hope this helps someone!!!

opencv fisherface recognizer

I'm trying to load model files for a FisherFaceRecognizer. The initial problem is that the program was written for an older OpenCV version and it seems some interfaces were changed.
Info about my project:
programming language: Python 3.5
OpenCV Version: 3.3.0
These are the two lines were I had a problem with:
model = cv2.face.createFisherFaceRecognizer()
model.load('foo_model.xml')
In the OpenCV documentation I found out that there is a new way to call the create functions and it seems to work. But I could not find the right call for the load function. I have tried to use the read function of the recognizer, but that results in an error.
model = cv2.face.FisherFaceRecognizer_create()
model.read('foo_model.xml')
The error message I've got when I try to use read():
File can't be opened for reading! in function read
Does somebody can help me with loading the model files? Thank you :)
The problem is with the xml file format. if you open the XML file you will not find "my_object" tag. I will not go to the details of this but, every time I face this problem, it works when I modify the xml file as follows.
<?xml version="1.0"?>
<opencv_storage>
<my_object> //add this
.........
.........
.........
</my_object> //and this
</opencv_storage>
The problem seems to be that the xml format in which the models are saved had been changed. This seems to be a known issue. I am using OpenCV 3.3.0 and want to load a model from an older OpenCV version which results in the mentioned error from the read-function. In the OpenCV Q&A forum a solution was suggested to me, but in my case it did not work. Nonetheless I will drop the link to my post at OpenCV Q&A here. Maybe someone else with the same problem can benefit from it.

Can't get to exif data .JPG image

I'm trying to read the exif data from a .JPG image. I've tried differents solutions found here and there (PIL, piexif, exifread...) and none of them worked for this set of images. It worked for other images taken from another camera but not for this one, all these different methods returning empty dictionaries. It seems that there is no exif data but (I apologies for my newbyness) when I RIGHT-click + properties (I use windows), I do see what is exif data to me : date of creation, etc...
Here is one image :
image.JPG
If another of the thousands of anonymous heroes could help me on this one, I would be very grateful...
Alright so I found a solution which I share now.
The problem is that the libraries that open metadata are not taking all possible configurations for the image file and therefore, they can handle some and some others they cannot. I finally made it using exiftool, an executable that I dowloaded on my windows on this link :
https://sno.phy.queensu.ca/~phil/exiftool/
Then I paste the executable in a folder and I add exiftool.py in that folder, that I got from :
https://github.com/smarnach/pyexiftool/find/master
Then, using this small piece of code (for example):
import exiftool
with exiftool.ExifTool("exiftool.exe") as et:
metadata = et.get_metadata_batch(files)
for d in metadata:
print("{:20.20} {:20.20}".format(d["SourceFile"],
d["File:FileCreateDate"]))
Of course, this is just to show that you indeed can access the metadata, then you can do whatever you want with that. Here is the documentation of the library exiftool : http://smarnach.github.io/pyexiftool/
Cheers, JM

Resources