Python Forum

Full Version: looking for scripts that do simple image display
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
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).
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()
where do i get skimage from? pypi?

it imports imshow and never uses it. was this taken from a larger script?
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
where is that tutorial? i'm always interested in learning things.
This is one of the links I used here.
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()
(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.
There is a minimal example using PIL here. This user has many snippets, including many tkinter examples.
(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.
Pages: 1 2