9

I've got a prototype I need to show for an RFID project. I've got the RFID (Parallax USB) working, and the sample Python script (my first) is displaying the ID of the card just like it's supposed to... problem is, it's not a very compelling demo/prototype.

What I'd love to be able to do is launch my application on the pi and have a PNG display somehow. New window, full screen, etc. Then, when a card is detected by the Pi, it would swap the image being displayed (1.PNG for 2.PNG, etc).

What would be the most pragmatic, easiest, fastest way to have the screen display just an image file?

Here's my code that outputs the RFID:

#! /usr/bin/python
import serial
import time

ser = serial.Serial('/dev/ttyUSB0', 2400, timeout=1) # replace '/dev/ttyUSB0' with your port

while True:
    response = ser.read(12)
    if response <> "":
        print "raw: " + str(response)
        print "hex: " + str(response[-8:])
        print "dec: " + str(int(response[-8:], 16))
    time.sleep(1)

ser.close()
2
  • How will you be running your application? Will you be using a GUI or just from a command line shell? Commented Jun 4, 2014 at 20:46
  • I would probably answer, "any way I can". I'm open to really any solution. I AM using LXDE, so it's definitely an option. I haven't programmed in a while, so it's been a fun challenge, and I'm not jaded yet by "no I can't..." so I appreciate any and all options. I'm really in a sort of "super exploratory mode" and have been having a blast just trying all sorts of solutions in python.
    – Jeff
    Commented Jun 5, 2014 at 18:49

3 Answers 3

6

If you're using an X11 Desktop Environment such as LXDE, then you can accomplish this using the basic logic shown in this article.

Here's what I came up with to switch displaying two images waiting 30 seconds between each switch. You should be able to insert your logic for switching the images based on what you read from your RFID sensor.

displayImages.py

#!/usr/bin/python

# use a Tkinter label as a panel/frame with a background image
# note that Tkinter only reads gif and ppm images
# use the Python Image Library (PIL) for other image formats
# free from [url]http://www.pythonware.com/products/pil/index.htm[/url]
# give Tkinter a namespace to avoid conflicts with PIL
# (they both have a class named Image)

import Tkinter as tk
from PIL import Image, ImageTk
from Tkinter.ttk import Frame, Button, Style
import time

class Example():
    def __init__(self):
        self.root = tk.Tk()
        self.root.title('My Pictures')

        # pick an image file you have .bmp  .jpg  .gif.  .png
        # load the file and covert it to a Tkinter image object
        imageFile = "babyAce.jpg"
        self.image1 = ImageTk.PhotoImage(Image.open(imageFile))
        self.image2 = ImageTk.PhotoImage(Image.open("baby-marti.jpg"))

        # get the image size
        w = self.image1.width()
        h = self.image1.height()

        # position coordinates of root 'upper left corner'
        x = 0
        y = 0

        # make the root window the size of the image
        self.root.geometry("%dx%d+%d+%d" % (w, h, x, y))

        # root has no image argument, so use a label as a panel
        self.panel1 = tk.Label(self.root, image=self.image1)
        self.display = self.image1
        self.panel1.pack(side=tk.TOP, fill=tk.BOTH, expand=tk.YES)
        print "Display image1"
        self.root.after(30000, self.update_image)
        self.root.mainloop()

def update_image(self):
    if self.display == self.image1:
        self.panel1.configure(image=self.image2)
        print "Display image2"
        self.display = self.image2
    else:
        self.panel1.configure(image=self.image1)
        print "Display image1"
        self.display = self.image1
    self.root.after(30000, self.update_image)       # Set to call again in 30 seconds

def main():
    app = Example()

if __name__ == '__main__':
    main()

You should be able to modify this to wait 1000 ms and test your RFID status to determine which image to display.

3
  • This is awesome. I installed PIL, python-tk is already the latest version, and I renamed the image files to match the ones I copied over into the same directory as "window.py". When I run it under python with python window.py I get the error: ImportError: cannot import name ImageTk Digging through it now to see how to troubleshoot it.
    – Jeff
    Commented Jun 5, 2014 at 19:06
  • I updated the program to properly use tkinter using this article as a reference. Commented Jun 6, 2014 at 1:01
  • I'm not a python user so I passed on approving the edit, but someone suggested from ttk import should be from Tkinter.ttk import -- but that also strikes me as wrong, my guess is it should be from tk import, since that's the as for Tkinter.
    – goldilocks
    Commented Mar 27, 2018 at 13:43
7

Wand has a display module/method.

In the terminal

$ python -m wand.display wandtests/assets/mona-lisa.jpg

In a Python script

import wand
with Image(blob=file_data) as image:
    wand.display.display(IMAGE)
1
  • thank you, that is great... you can even change image on fly, so it is great for making simple animations aswell, or games, or whatever... Commented Jul 11, 2015 at 10:28
2

If you want to display an image from the command line, you can use the "fbi" console program with sudo apt-get install -y fbi

Not the answer you're looking for? Browse other questions tagged or ask your own question.