Building an Image Viewer in Python

in #python6 years ago

Screenshot from 2018-05-28 12-30-28.png

You may recall I was building an image viewer, and began with getting a list of files in modified date order.

Background

The reason for this project is I build a lot of content using purchased stock photography, and keeping track of those photographs, in particular, the most recently purchased can be tricky due to the variety of naming conventions of these files.

When I am looking for a fresh photograph in the shared dropbox folder, it helps me to be able to visually flick through them, and ideally I don't want to have a bulky app like lightroom etc on all the devices I might use.

Yeah, there are other tools out there, but this was an opportunity to develop more Python muscles :)

Core requirements

Top priority

  1. Most recent first
  2. View the image
  3. Quickly and easily skip to the next image

Priority 2: Significant value but non-essential

  • Skip back and forth
  • Open/select in directory
  • Copy filename to clipboard

Priority 3: Nice to have

  • Thumbnails?
  • Basic image processing? (eg. 90-degree rotation, crop)
  • Labelling/annotation?

Coding up the core requirements in Python

We can satisfy the core requirements in a rough and dirty way very simply:

import glob
from PIL import Image

files = glob.glob("/home/chrisg/Pictures/*.jpg")
files += glob.glob("/home/chrisg/Pictures/*.png")
files += glob.glob("/home/chrisg/Pictures/*.gif")
files.sort(key=os.path.getmtime, reverse=True)

for file in files:
    print(file)
    picture = Image.open(file).show()

We simply use our list of files and iterate, showing the image using the default image viewer.

This has an issue that each opens in a unique window and you can't skip through, you can only close them.

So we need a basic GUI.

Python Gui

We have a few options for GUI work in Python, but I am going with tkinter as that is standard.

import tkinter
from PIL import ImageTk

These modules will give us tk support both as a base and with the Python image library format images.

Next, we need a window to display our pictures in.

window = tkinter.Tk()

Next, we load the image file ready to display.

To actually display the picture, we set it up in a label, which I call image_widget.

window.title(file)
    picture = Image.open(file)
    tk_picture = ImageTk.PhotoImage(picture)
    image_widget = tkinter.Label(window, image=tk_picture)
    image_widget.place(x=0, y=0, width=picture_width, height=picture_height)

Interaction

Our basic interaction will be skipping to the next picture, so we can use click and any press of the keyboard for this.

def event_action(event):
    print(repr(event))
    event.widget.quit()


def clicked(event):
    event_action(event)


def key_press(event):
    event_action(event)


window.bind("<Button>", clicked)
window.bind("<Key>", key_press)

We "bind" the click and key events to a callback that simply closes the current widget. Nothing fancy.

You can see in your console the key/click that triggered the function, so later we can interpret the events.

Remember, TK is event-driven, so control of the flow sits with the GUI until you take an action, after which the control passes back to our for loop.

window.mainloop()

Full code

Get the full code in this Gist


Sort:  

I suggest if you can add some photo editing tools would be great and attract more people like crop, fade, white, and some mini tools.

You have captured my most used image features with the step and rotate feature

wah ... i have never used python, now i often use photoshop for some edits, because i am learning to edit more professional ... maybe i will try your way after one lesson i mastered.
Thanks for that code @makerhacks

You got a 10.66% upvote from @upme thanks to @makerhacks! Send at least 3 SBD or 3 STEEM to get upvote for next round. Delegate STEEM POWER and start earning 100% daily payouts ( no commission ).

This post has received votes totaling more than $50.00 from the following pay for vote services:

smartsteem upvote in the amount of $87.07 STU, $114.33 USD.
minnowbooster upvote in the amount of $58.31 STU, $82.11 USD.
upme upvote in the amount of $27.97 STU, $36.73 USD.
danzy upvote in the amount of $0.58 STU, $0.81 USD.

For a total calculated value of $174 STU, $234 USD before curation, with a calculated curation of $30 USD.

This information is being presented in the interest of transparency on our platform and is by no means a judgement as to the quality of this post.

I am a python developer and i appreciate your work and spreading awareness and trying to make people aware about python. Good Man!

By the way i am a traveler so you may like my travel blog too. Travel Series (Part #3) St. Lucia Iceland, A Romantic Getaway

I took a look but it’s a bit strange you didn’t use your own photos?

And it’s not Iceland?

wow very nice creative @makerhacks

Coin Marketplace

STEEM 0.20
TRX 0.16
JST 0.030
BTC 66070.34
ETH 2691.62
USDT 1.00
SBD 2.88