Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Display jpg images in python

Tags:

python

tkinter

I am creating a simple tool to add album cover images to mp3 files in python. So far I am just working on sending a request to amazon with artist and album title, and get the resulting list, as well as finding the actual images for each result. What I want to do is to display a simple frame with a button/link for each image, and a skip/cancel button.

I have done some googling, but I can't find examples that I can use as a base.

  1. I want to display the images directly from the web. Ie. using urllib to open and read the bytes into memory, rather than go via a file on disk
  2. I want to display the images as buttons preferably

All examples seems to focus on working on files on disk, rather with just a buffer. The TK documentation in the python standard library doesn't seem to cover the basic Button widget. This seems like an easy task, I have just not had any luck in finding the proper documentation yet.

like image 769
Staale Avatar asked Nov 05 '22 19:11

Staale


1 Answers

you can modify this using urllib.urlopen(). But I don't know (as I haven't tested it) if you can make this step without saving the (image) file locally. But IMHO urlopen returns a file handle that is usable in tk.PhotoImage().

For jpg files in PhotoImage you need PIL:

from PIL import Image, ImageTk
image = Image.open("test.jpg")
photo = ImageTk.PhotoImage(image)
like image 142
tuergeist Avatar answered Nov 13 '22 17:11

tuergeist