-2

I'm trying to get the image from the song album to display in the window with the song title and artist but it just doesn't do anything. I've tried replacing the "imageLabel" with

"imageLabel = tkinter.Label(window,image=tkinter.PhotoImage(file="CurrentSong.jpg"))" but it still doesn't work.

import requests
import time
import tkinter

token = ''
endpoint = "https://api.spotify.com/v1/me/player/currently-playing"
spotifyHeaders = {'Authorization':'Bearer ' + token}
requestAmount = 1
window = tkinter.Tk(className="|CurrentSong Spotify Song|")
window.geometry('400x400')
canvas = tkinter.Canvas(window,height=1000,width=1000)
canvas.pack()
songLabel = tkinter.Label(window,bg='grey')
songLabel.pack()


def GrabSpotifyCurSong(curSongJson):
    return curSongJson['item']['name']
def GrabSpotifyCurArtist(curSongJson):
    return curSongJson['item']['artists'][0]['name']
def GrabCurrentSongImage(curSongJson):
    return curSongJson['item']['album']['images'][0]['url']
    
def displaySongs():
    while True:
        try:
            curSong = requests.get(endpoint, headers=spotifyHeaders)
            curSongJson = curSong.json()
            break
        except:
            print("Please start listening to a song")
            time.sleep(2)
    with open('CurrentSong.png','wb+') as SongImage:
        response = requests.get(GrabCurrentSongImage(curSongJson))
        SongImage.write(response.content)
    currentSong = GrabSpotifyCurSong(curSongJson)
    currentArtist = GrabSpotifyCurArtist(curSongJson)
    img = tkinter.PhotoImage(file="CurrentSong.png")
    imageLabel = tkinter.Label(window,image=img)
    # songLabel['text'] = f'{currentArtist} - {currentSong}'
    # songLabel.place(height=400,width=400)
    print(f'{currentArtist} - {currentSong}')
    window.after(2500,displaySongs)

displaySongs()
window.mainloop()
Gravity
  • 23
  • 7
  • 1
    What does "_it still doesn't work._" mean – Delrius Euphoria Jun 05 '21 at 03:04
  • @CoolCloud So "imageLabel = tkinter.Label(window,image=tkinter.PhotoImage(file="CurrentSong.jpg"))" gives me an error that says: "couldn't recognize data in image file "CurrentSong.jpg"" and the other one which is in the code just doesn't do anything – Gravity Jun 05 '21 at 03:06
  • You can load the image data directly: `response = requests.get(GrabCurrentSongImage(curSongJson))`, `image = ImageTk.PhotoImage(data=response.content)`. – acw1668 Jun 05 '21 at 03:37

1 Answers1

1

Images with tkinter has to be PhotoImage instances, here it is just a string of location of the image and tkinter does not understand that. Furthermore, tkinter.PhotoImage does not recognize JPEG format, so you have to convert it to PNG or use PIL.ImageTk.PhotoImage to use JPEG.

  • For JPEG and other formats too:

    First pip install Pillow and then:

    import tkinter
    from PIL import Image, ImageTk
    
    ....
    img = ImageTk.PhotoImage(Image.open("CurrentSong.jpg"))
    imageLabel = tkinter.Label(window,image=img)
    

    Adding further here, you can also use ImageTk.PhotoImage(file="CurrentSong.jpg") but that will remove the flexibility that you could get if you want to, say, resize or do some filters to your image. If not, then use that.

  • For GIF, PGM, PPM, and PNG:

    img = tkinter.PhotoImage(file="CurrentSong.png")
    imageLabel = tkinter.Label(window,image=img)
    

Also note that if these are inside function you have to keep reference to the object to avoid it being collected by the gc after the function finishes running.

Delrius Euphoria
  • 14,910
  • 3
  • 15
  • 46
  • I copied what you put and I ran it(also removed the other labels just so they wouldn't go over the image just in case) and the same thing is happening where no image gets put into the window. – Gravity Jun 05 '21 at 03:15
  • @Gravity Other labels won't go over the image, and also do you get any error? – Delrius Euphoria Jun 05 '21 at 03:16
  • no error, I will turn the image into a png and do it the way I was originally, and see if that works – Gravity Jun 05 '21 at 03:18
  • and now I am getting the same error as before "couldn't recognize data in image file "CurrentSong.png"" – Gravity Jun 05 '21 at 03:20
  • @Gravity I don't think it will, because there is some mistake in your implementation. Edit your question with the updated code. – Delrius Euphoria Jun 05 '21 at 03:21
  • @Gravity To convert an image to PNG, you have to use a converting tool, not just change its filename from .jpeg to .png – Delrius Euphoria Jun 05 '21 at 03:21
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/233345/discussion-between-gravity-and-cool-cloud). – Gravity Jun 05 '21 at 03:22