How to get artwork into Tkinter Image using eyed3? - python

Similar to this question:
How to get detail (Title,Artist) from .mp3 files in python using eyed3
I'd like to get music artwork from a .m4a file (similar to `.mp3') into Tkinter Image for displaying on a label.
For some strange reason all the answers in the link use:
import eyed3
but I have to use
import eyeD3
to install I had to use:
sudo apt install python-eyed3
sudo apt install eyed3
I'm running Ubuntu 16.04.6 LTS until 2021 at the latest which means I'm using Python 2.7.12. I understand the syntax and naming conventions may have changed in eyeD3 or eyed3 Python 3.5 versions which is another option for Ubuntu 16.04.6 LTS. I'm also using Linux Kernel 4.14.188 LTS but I doubt that matters.
Note: I tried ffmpeg calls from Python this morning to convert .m4a file's artwork into a .jpg file but that was "complicated" and I was hoping eyed3 or eyeD3 would be simpler.

Use file.tag.images and iterate it,use i.image_data to get the bytes of the image.
For example:
import eyed3, io
from PIL import Image, ImageTk
import tkinter as tk
root = tk.Tk()
file = eyed3.load(r"music_path")
# if there contains many images
for i in file.tag.images:
img = ImageTk.PhotoImage(Image.open(io.BytesIO(i.image_data)))
tk.Label(root, image=img).pack()
# or if there only one image here:
# img = ImageTk.PhotoImage(Image.open(io.BytesIO(file.tag.images[0].image_data)))
# tk.Label(root, image=img).pack()
root.mainloop()
Works fine on my PC.

Related

QRCode generated in python

I wrote some code to generate qrcode in python but at run time it shows this error:
Import "qrcode" could not be resolved Pylance(reportMissingImports)
I am using the qrcode library but I am having some problems.
Installation is pretty straight forward via pip install. Run the following command to install python-qrcode and pillow.
pip install qrcode[pil]
Once you are done, continue installing OpenCV-Python with the following command:
pip install opencv-python
If you intend to detect multiple QR codes in a single image, make sure that the opencv-python version is at least 4.3.0. Older versions do not come with the multi detection functionalities.
Add the following import declaration at the top of your Python file.
import qrcode
from PIL import Image
For basic usage, you can simply run the make() function to generate a QR Code based on your input text:
img = qrcode.make('Your input text')
You can utilize the QRCode class, which comes with a lot more controls and properties.
qr = qrcode.QRCode(
version=1,
error_correction=qrcode.constants.ERROR_CORRECT_H,
box_size=10,
border=4,
)
The next step is to call the add_data() function. Pass in the input text of your choice. Continue by appending the following code to generate a QR code with a white background and black fill.
qr.add_data('https://medium.com/#ngwaifoong92')
qr.make(fit=True)
img = qr.make_image(fill_color="black", back_color="white").convert('RGB')
You can save it in as an image file as follow:
img.save("sample.png")
Finally you can read this paper.

Is there a way to capture an image of the screen in python with ctypes?

I want to have python save an image file of the whole screen as a variable with ctypes so that I could access the screen in a program and do something with it (like put it on a pygame window). I can't use any other libraries unless they are included with python (no installing or pip). Does anyone know how to do this?
Edit: I'm using windows 10.
PIL.ImageGrab is from PILLOW (a python image library fork which you can install with pip). You can give a bounding box or capture the entire screen.
Update: OP now mentions he can't use external libraries.
Then you could virtually hit printscreen and read the clipboard. The code of PILLOW is open-source feel free to use it.
Remember that you can always call a command from within python:
>>> import os
>>> os.system("pip install pillow")
Or download the zip of the library and import it in your code.

Package a GUI for distribution - ghostscript..?

I have a GUI created that I'd like to ship out to people I work with. The GUI works perfectly for me, but when I packaged it using pyinstaller and --onefile, it didn't work for anyone else. I am aware no one else has python installed on their machines, but I was told that wouldn't be a problem. However, my program involves ghostscript.
In short, my program has 3 buttons. Button 1 allows the user to select a pdf. Button 2 will convert this pdf into images. Button 3 will read each image and say whether it is a colour page or a black only page, when done it'll delete the temporary image files.
the imports i am using are
from tkinter import *
import os, time
from PyPDF2 import PdfFileReader, PdfFileWriter
from tempfile import NamedTemporaryFile
from tkinter.filedialog import askopenfilename, askdirectory
import tkinter.scrolledtext as tkst
from PythonMagick import Image as IMG
from PIL import Image
I think I'd somehow need to have the user install the same version of ghostscript as me when they open the application. Does anyone have any advice on how I can sort this out to have people use it?
EDIT: When I say 'it didn't work for anyone else', i mean the application opened, the user could select a file, but the button which runs the ghostscript stuff didn't work. Meaning ghostscript needed to be installed. After installed it worked.
I would suggest manual installation of GhostScript and adding its path to the OS environment.
import os
from pathlib import Path
this_path = Path(sys.argv[0]).parent
os.environ['MAGICK_HOME'] = ';'.join([str(this_path / 'image_magic'),
str(this_path / 'GhostScript' / 'bin')
])
Here, I install Image Magic + GhostScript in the root with the Python files.

I am not able to import an image into python using the import image way

Before you ask, I did try putting an image in it, and it didn't work like look at this.
import Image
Image = Image.open('clerky.jpeg')
Image.show()
and this code above comes up with this error below.
import Image
ImportError: No module named 'Image'
If you're using Pillow, the correct usage is
from PIL import Image
To install Pillow on Windows, start the Command Prompt application (or hit WinR and type cmd, then hit Enter). Type
pip install Pillow
hit Enter, and everything should install automatically.
I think you mean from PIL import Image
from PIL import Image
PIL (Python Image Library) has to be installed on your system, AKA "pil" and "Pillow" from time to time. effbot.org/imagingbook/pil-index.htm

ImageGrab Python on OS X

I want to use a imageGrab in my application.
My laptop is a macbook with OSX.
When I use Pillow I got this error:
ImportError: ImageGrab is Windows only
Code:
import ImageGrab
im = PIL.ImageGrab.grab()
but in Pillow documentation says:
The current version works on OS X and Windows only.
Take a snapshot of the screen.
The pixels inside the bounding box are returned as an “RGB” image on Windows or “RGBA” on OS X.
If the bounding box is omitted, the entire screen is copied.
http://pillow.readthedocs.org/en/latest/reference/ImageGrab.html
When I use pyscreenshot I got this error:
IOError: cannot identify image file '/var/folders/wk/b1c839t15xvbz923wtfdsfw80000gn/T/pyscreenshot_imagemagick_Gsb0Pw.png'
Code:
import pyscreenshot as ImageGrab
im=ImageGrab.grab()
According to the commit history, OSX support was only added on 1st Aug 2015. However, the latest Pillow release (2.9.0) was made on 1st July 2015. So it would appear that the online documentation is not kept in sync with the current release.
You could compile a pre-release version from github to get the required functionality, but it would probably be much simpler to just copy the relevant ImageGrab code directly:
import os, tempfile, subprocess
from PIL import Image
def grab(bbox=None):
f, file = tempfile.mkstemp('.png')
os.close(f)
subprocess.call(['screencapture', '-x', file])
im = Image.open(file)
im.load()
os.unlink(file)
if bbox:
im = im.crop(bbox)
return im
The next Pillow release, 3.0.0, is due out on Thursday (1st Oct 2015) and ImageGrab will support both OS X and Windows.
The linked documentation is the latest, and is generated from the latest master branch.
The 2.9.0 docs says it's Windows only.
OS X support was added in Pillow 3.3.0.

Categories