I am working on a music player program, i wanted to show the song default image while playing it, so how can I add and show it in a Tkinter window. this what i have tried:
JavaScript
x
4
1
import audio_metadata
2
metadata=audio_metadata.load('Barood_Dil.mp3')
3
print(metadata.pictures)
4
Output:
JavaScript
1
9
1
[<ID3v2Picture({
2
'data': '50.48 KiB',
3
'description': 'FRONT_COVER',
4
'height': 600,
5
'mime_type': 'image/jpeg',
6
'type': <ID3PictureType.OTHER_FILE_ICON>,
7
'width': 600,
8
})>]
9
this helped me to get all the information about its cover image but i wanted to show it in my Tkinter window.
Advertisement
Answer
The album artwork is returned in a list of dictionaries. So to access the first picture use: metadata.pictures[0]
and to access the byte data of that picture use: metadata.pictures[0].data
Try (Explanation in code comments):
JavaScript
1
22
22
1
import audio_metadata
2
from tkinter import *
3
from PIL import ImageTk,Image
4
from io import BytesIO
5
6
#get all metadata of song.mp3
7
metadata=audio_metadata.load('song.mp3')
8
9
# get the picture data for the first picture in metadata pictures
10
artwork = metadata.pictures[0].data
11
12
# open stream (similar to file) in binary mode
13
stream = BytesIO(artwork)
14
15
root = Tk()
16
# display artwork on tkinter
17
canvas = Canvas(root, width = 512, height = 512)
18
canvas.pack()
19
img = ImageTk.PhotoImage(Image.open(stream))
20
canvas.create_image(0, 0, anchor=NW, image=img)
21
root.mainloop()
22