Python Forum
looking for scripts that do simple image display
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
looking for scripts that do simple image display
#1
i would like to have a minimal script to display an image. very minimal would support just one image file format although supporting two formats would be nice. it needs to start at the command line. it can either open a new window for the image or put the image in the root window. an option to do either would be nice. a timer to take down the image in a specified time would be very nice. support for a key input like Q or ESC or just any key or mouse click to have exit would be nice. Tkinter or PyQT support would be great but any would be fine. this is for learning by example so fancy elaborate image applications would be of much less help. i hope it unpacks the image file into raw pixels and displays those. Python3, only (does this need to be said these days ... i was debugging an issue with a script from Ubuntu just this afternoon that still uses 2.7 so i wonder).
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#2
Not sure if this is what you want.

#! /usr/bin/env python3

# Do the imports

from skimage.io import imread, imshow
import tkinter as tk
from tkinter import filedialog
from os import sys

# Start tkinter
root = tk.Tk()
root['padx'] = 20
root.geometry('800x600+250+250')

root.columnconfigure(0, weight=1)
root.rowconfigure(0, weight=1)

# Function for getting and displaying image
def get_image():
    file = filedialog.askopenfilename(title='image file')
    img = tk.PhotoImage(file=file)
    img.back = img
    label = tk.Label(container, image=img)
    label['relief'] = 'ridge'
    label.grid(column=0, row=0, sticky='new', padx=2)
    col = 0
    row = 0

    # Use imread to get raw image data
    imag = imread(file)

    # Clear any data then insert new data into a listbox
    box.delete(0, tk.END)
    for im in imag:
        box.insert(tk.END, im)

# Container frame
container = tk.Frame(root)
container.grid(column=0, row=0, sticky='news')
container.grid_columnconfigure(0, weight=3, uniform='win')
container.grid_columnconfigure(1, weight=3, uniform='win')

# Button to get the image
btn = tk.Button(container, text='Get Image', command=get_image)
btn.grid(column=1, row=0, sticky='new', pady=10, padx=2)

# Frame for listbox containing raw image data
frame = tk.Frame(container)
frame.grid(column=0, row=1, columnspan=2, sticky='news')

# Scrollbars
scrollbar = tk.Scrollbar(frame, orient='vertical')
scrollbar2 = tk.Scrollbar(frame, orient='horizontal')
box = tk.Listbox(frame, width=123, height=24)
box['yscrollcommand'] = scrollbar.set
box['xscrollcommand'] = scrollbar2.set
scrollbar.configure(command=box.yview)
scrollbar2.configure(command=box.xview)

scrollbar.grid(column=1, row=0, sticky='news')
scrollbar2.grid(column=0, row=1, sticky='news')
box.grid(column=0, row=0, sticky='news')

# Bind the q and esc keys to exit when pressed
root.bind('<q>', sys.exit)
root.bind('<Escape>', sys.exit)

# Automatic close after 5 minutes
root.after(50000, sys.exit)
root.mainloop()
I welcome all feedback.
The only dumb question, is one that doesn't get asked.

My Scripts
CookBook - Shmup - PyQt5 Music Player


Reply
#3
where do i get skimage from? pypi?

it imports imshow and never uses it. was this taken from a larger script?
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#4
Quote:where do i get skimage from? pypi?
I did pip install, buts been renamed to scikit-image

Quote:it imports imshow and never uses it
I forgot to remove it.

Quote:was this taken from a larger script?
I looked up a tutorial and used bits and pieces
I welcome all feedback.
The only dumb question, is one that doesn't get asked.

My Scripts
CookBook - Shmup - PyQt5 Music Player


Reply
#5
where is that tutorial? i'm always interested in learning things.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#6
This is one of the links I used here.
I welcome all feedback.
The only dumb question, is one that doesn't get asked.

My Scripts
CookBook - Shmup - PyQt5 Music Player


Reply
#7
Rewrote the code a little.

#! /usr/bin/env python3

from skimage.io import imread
import tkinter as tk
from tkinter import filedialog
from os import sys


root = tk.Tk()
root.geometry('1020x600+250+250')
root['padx'] = 5
root['pady'] = 5
root['padx'] = 20
root.columnconfigure(0, weight=1)
root.rowconfigure(0, weight=1)

def get_image():
    file = filedialog.askopenfilename(title='Image File')
    img = tk.PhotoImage(file=file)
    img.back = img

    img_label = tk.Label(emptyframe)
    img_label['image'] = img
    img_label.grid(column=0, row=0, sticky='news')

    col = 0
    row = 0

    imag = imread(file)
    listbox.delete(0, tk.END)

    for data in imag:
        listbox.insert(tk.END, data)


container = tk.Frame(root)
container.grid(column=0, row=0, sticky='news')
container.grid_columnconfigure(0, weight=1)
container.grid_columnconfigure(1, weight=3)
container.grid_rowconfigure(1, weight=3)

border = tk.Frame(container)
border['highlightcolor'] = 'lightgray'
border['highlightbackground'] = 'lightgray'
border['highlightthickness'] = 1
border.grid(column=0, row=0, sticky='news', padx=2, pady=2)

label = tk.Label(border, text='Choose an Image')
label.grid(column=0, row=0, sticky='news')

btnborder = tk.Frame(container)
btnborder['highlightcolor'] = 'lightgray'
btnborder['highlightbackground'] = 'lightgray'
btnborder['highlightthickness'] = 1
btnborder.grid(column=1, row=0, sticky='news', padx=2, pady=2)
btnborder.grid_columnconfigure(0, weight=3)

btn = tk.Button(btnborder, text='Get Image', command=get_image)
btn.grid(column=0, row=0, sticky='n')

emptyframe = tk.Frame(container)
emptyframe['highlightthickness'] = 1
emptyframe['highlightcolor'] = 'lightgray'
emptyframe['highlightbackground'] = 'lightgray'
emptyframe.grid(column=0, row=1, sticky='news', padx=2)
emptyframe.grid_rowconfigure(1, weight=3)
emptyframe.grid_columnconfigure(0, weight=1)


spacer = tk.Frame(container, bg='white', border=1)
spacer['highlightbackground'] = 'lightgray'
spacer['highlightcolor'] = 'lightgray'
spacer['highlightthickness'] = 1
spacer.grid(column=1, row=1, sticky='news')
spacer.grid_rowconfigure(0, weight=3)
spacer.grid_columnconfigure(0, weight=3)

boxframe = tk.Frame(spacer, bg='white', border=2)
boxframe['highlightcolor'] = 'white'
boxframe['highlightbackground'] = 'white'
boxframe['highlightthickness'] = 2
boxframe.grid(column=0, row=0, sticky='news', padx=2, pady=2)
boxframe.grid_rowconfigure(0, weight=3)

horizontalbar = tk.Scrollbar(spacer, orient='horizontal')
horizontalbar.grid(column=0, row=1, sticky='ew')

verticalbar = tk.Scrollbar(spacer, orient='vertical')
verticalbar.grid(column=1, row=0, sticky='ns')

listbox = tk.Listbox(boxframe, relief='flat')
listbox['highlightthickness'] = 0
listbox.pack(expand=True, fill='both')
listbox['xscrollcommand'] = horizontalbar.set
listbox['yscrollcommand'] = verticalbar.set
horizontalbar.configure(command=listbox.xview)
verticalbar.configure(command=listbox.yview)

root.mainloop()
I welcome all feedback.
The only dumb question, is one that doesn't get asked.

My Scripts
CookBook - Shmup - PyQt5 Music Player


Reply
#8
(Sep-06-2021, 02:09 AM)menator01 Wrote: This is one of the links I used here.

interesting page, but it lost me using the term "feature" for pixel. it lost me further by saying a 650 x 450 image has 297,000 features. how does it get that? 650 * 450 is 292500. ah, later, it uses 660 x 450 and now that number makes sense. but i've lost trust in the writing accuracy of this author. i still do not know where the term "feature" comes from. maybe this is why the title made no sense to me.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#9
There is a minimal example using PIL here. This user has many snippets, including many tkinter examples.
Reply
#10
(Sep-08-2021, 09:29 AM)Gribouillis Wrote: There is a minimal example using PIL here. This user has many snippets, including many tkinter examples.

at least it is more minimal. the Tkinter part is a plus.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [Tkinter] Display Selected Image from Directory and Resize it EchoLi 0 2,486 Oct-02-2019, 06:54 PM
Last Post: EchoLi
  Refresh image in label after every 1s using simple function jenkins43 1 3,736 Jul-28-2019, 02:49 PM
Last Post: Larz60+
  Simple Button click on image file to create action? jpezz 4 3,403 Mar-27-2019, 10:08 PM
Last Post: jpezz
  Display and update the label text which display the serial value jenkins43 5 5,317 Feb-04-2019, 04:36 AM
Last Post: Larz60+
  Display more than one button in GUI to display MPU6000 Sensor readings barry76 4 1,869 Jan-05-2019, 01:48 PM
Last Post: wuf
  Display image in tkinter dan789 9 23,326 Dec-09-2018, 08:17 PM
Last Post: dan789
  Tkinter Image Display Weird Tearing Upon Transforming Emerest 6 4,403 Sep-01-2018, 11:46 PM
Last Post: Emerest

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020