- Specific folder (this time
Desktop \ new) Is stored in
Randomly acquire image files with either JPG or PNG extension
- Resize the image to the proper size
- Display on screen
I'd like to realize that with PySimpleGUI, but I'm having trouble solving the following error.
We would appreciate it if you could lend us your help.
Basically, most of them are JPG images, but I want to display images with both extensions.
Since tkinter has a limited display extension, it uses ImageTk from the Pillow library.
Corresponding source code
Exception occurred: TclError couldn't recognize image data
#! python3.7 import glob import random import tkinter as tk import PySimpleGUI as sg from PIL import Image, ImageTk dir_name = r'C: \ Users \ USER \ Desktop \ new' img_types = (".jpg", ".png") #Function that takes one image at random def random_pick (): global dir_name global img_types while True: img_list = glob.glob (dir_name +'\\ *') pickfile = random.choice (img_list) if pickfile.lower (). endswith (img_types): return pickfile # Find the magnification to resize the image def set_magnification (iw, ih): rew = iw reh = ih n = 1 while rew>600 and reh>600: rew = iw/n reh = ih/n n + = 0.5 return n def get_img_data (f): "" "Generate image data using PIL" "" print ("open file:", f) img = Image.open (f) magf = set_magnification (img.width, img.height) img_resize = img.resize ((int (img.width/magf), int (img.height/magf))) return ImageTk.PhotoImage (img_resize) def main (): sg.ChangeLookAndFeel ('BlueMono') pfile = random_pick () image_elem = sg.Image (data = get_img_data (pfile)) filename_display_elem = sg.Text (pfile, size = (80, 3)) layout = [[filename_display_elem], [image_elem], [sg.OK ()]] window = sg.Window ('display images', layout, return_keyboard_events = True, location = (0, 0), use_default_focus = False) #event loop while True: event, value = window.read () #
Answer # 1
The actual error occurs in the following places.
image_elem = sg.Image (data = get_img_data (pfile))
The value taken by the argument data is the bytes type data.
: param data: Raw or Base64 representation of the image to put on button. Choose either filename or data
: type data: Union [bytes, str]
In the get_img_data function
return ImageTk.PhotoImage (img_resize)
However, in the article of the reference URL, first = True, so
Try to return the data in bytes.
- error in python image reading iobytesio ()
- python - i want to display a vector on the image
- python - error in image binarization using cv2adaptivethreshold function
- python - about mnist image display
- about image display of python
- python - about image display and position of main ()
- python - image display from the variable to which the file name is assigned
- error display on python discord bot
- c ++ - image switching display method
- python - i got an import error even though i did pip install
- [python3] i want to display the numerical list from excel vertically (line feed display) on the tkinter window
- i want to display python data mining 0 without erasing it!
- python - i want to create and save a graph, but i get an error
- i want to resolve a syntax error in a python while statement
- error when reading excel in python
- how to save the image ocr result file in python
- python - i get a key error
- python - watchdog is installed, but i get the error modulenotfounderror: no module named'watchdog'
- python - i want to put the image file path in a variable and open it using that variable