Skip to content
Advertisement

how can i show the front cover image of an audio file

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:

import audio_metadata
metadata=audio_metadata.load('Barood_Dil.mp3')
print(metadata.pictures)

Output:

[<ID3v2Picture({
    'data': '50.48 KiB',
    'description': 'FRONT_COVER',
    'height': 600,
    'mime_type': 'image/jpeg',
    'type': <ID3PictureType.OTHER_FILE_ICON>,
    'width': 600,
})>]

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

import audio_metadata
from tkinter import *
from PIL import ImageTk,Image
from io import BytesIO

#get all metadata of song.mp3
metadata=audio_metadata.load('song.mp3')

# get the picture data for the first picture in metadata pictures
artwork = metadata.pictures[0].data

# open stream (similar to file) in binary mode
stream = BytesIO(artwork)

root = Tk()
# display artwork on tkinter
canvas = Canvas(root, width = 512, height = 512)
canvas.pack()
img = ImageTk.PhotoImage(Image.open(stream))
canvas.create_image(0, 0, anchor=NW, image=img)
root.mainloop()
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement