GPIO button press image display


13 posts
by danieljay » Wed Feb 20, 2013 6:04 pm
Hey all, so i am trying to load an image file when a button is pressed connected to GPIO pins.

I followed the setup on adafruits site here: http://learn.adafruit.com/adafruits-ras ... gpio-setup


i then created my code below an its giving me an error that it cannot find a module named "Image". Very new to linux and python so all help appreciated.

Code: Select all
#!/usr/bin/env python
import RPi.GPIO as GPIO
import time
import os
import Image
im=Image.open("/home/pi/imagejpg/Home.jpg").convert("RGB")
#adjust for where your switch is connected
buttonPin = 23
GPIO.setmode(GPIO.BCM)
GPIO.setup(buttonPin,GPIO.IN)

while True:
     if ( GPIO.input(23) == False ):
         im.show()
   
Posts: 49
Joined: Sun Sep 16, 2012 5:01 pm
by davef21370 » Wed Feb 20, 2013 6:09 pm
Try...
Code: Select all
from PIL import Image


Dave.
Please feel free to tap into my abundant lack of knowledge.
User avatar
Posts: 523
Joined: Fri Sep 21, 2012 4:13 pm
Location: Up North
by danieljay » Wed Feb 20, 2013 6:22 pm
it said no module named "PIL".

I am very new and not sure if I need to install libraries(?) or other dependencies.
Posts: 49
Joined: Sun Sep 16, 2012 5:01 pm
by croston » Wed Feb 20, 2013 6:31 pm
To install PIL:
Code: Select all
$ sudo apt-get install python-imaging

Apologies if that's not quite right - I am working from memory as I am not at a computer at the moment!
User avatar
Posts: 464
Joined: Sat Nov 26, 2011 12:33 pm
Location: Blackpool
by danieljay » Wed Feb 20, 2013 6:40 pm
before I attempt, should a simple import Image work with my current setup?
Posts: 49
Joined: Sun Sep 16, 2012 5:01 pm
by davef21370 » Wed Feb 20, 2013 6:51 pm
Go with croston's post and you should be good.

Dave.
Please feel free to tap into my abundant lack of knowledge.
User avatar
Posts: 523
Joined: Fri Sep 21, 2012 4:13 pm
Location: Up North
by danieljay » Wed Feb 20, 2013 7:09 pm
thank you all for being so helpful. the error is gone, but a new error popped up:


Code: Select all
SyntaxError: Non-ASCII character '\xc2' in file buttonimage.py on line 13, but no encoding declared; see http://www.python.org/peps/pep-0263.html for details



tried from PIL import Image and just import Image but same error.
Posts: 49
Joined: Sun Sep 16, 2012 5:01 pm
by croston » Wed Feb 20, 2013 7:13 pm
Is buttonimage.py your program? If so, delete and rewrite line 13 - you have done a copy & paste of a strange whitespace character. I don't think the error is anything to do with PIL.
User avatar
Posts: 464
Joined: Sat Nov 26, 2011 12:33 pm
Location: Blackpool
by danieljay » Wed Feb 20, 2013 8:19 pm
Great! that solvesd that error. but now when my code is executed in terminal, i.e.

sudo python buttonimage.py, it seems to hang without any results.

i tested a simple sketch to load an image and made it executable with chmod +x like so:


Code: Select all
import Image
im = Image.open("/home/pi/imagejpg/Home.jpg").convert("RGB")
im.show()
Posts: 49
Joined: Sun Sep 16, 2012 5:01 pm
by danieljay » Wed Feb 20, 2013 10:22 pm
So I I added the feh image viewer since i read img.show is reliant on xv. im stilll not getting an image shown though




Code: Select all
import Image
img=Image.open("/home/pi/Home.jpg")
img.show(command='feh')
Posts: 49
Joined: Sun Sep 16, 2012 5:01 pm
by texy » Thu Feb 21, 2013 7:51 am
Hi,
I would make sure your button is working as expected before worrying about the image. Take a look at the Mapi magazine, issue 2.
http://issuu.com/themagpi/docs/the_magpi_issue_2_final

Texy
"2.8inch TFT LCD + Touch screen" add-on boards for sale here :
http://www.raspberrypi.org/phpBB3/viewtopic.php?f=93&t=65566
50p goes to the Foundation ;-)
Forum Moderator
Forum Moderator
Posts: 2454
Joined: Sat Mar 03, 2012 10:59 am
Location: Berkshire, England
by danieljay » Thu Feb 21, 2013 7:58 am
I would make sure your button is working as expected before worrying about the image.
.

The order in which to work on the two different parts of my code is irrelevant.
Posts: 49
Joined: Sun Sep 16, 2012 5:01 pm
by texy » Thu Feb 21, 2013 9:05 am
Hi,
it may well be irrelevant, but thats the order I would do things if I didn't know what I was doing ;)
You can very easily remove any reference to checking the input pin from your code to make sure the image is loaded without any pre-condition.
So as I said, making sure the button hardware is correctly working and the software is configured to match that hardware is more difficult if you have little experience of doing it.
Texy
"2.8inch TFT LCD + Touch screen" add-on boards for sale here :
http://www.raspberrypi.org/phpBB3/viewtopic.php?f=93&t=65566
50p goes to the Foundation ;-)
Forum Moderator
Forum Moderator
Posts: 2454
Joined: Sat Mar 03, 2012 10:59 am
Location: Berkshire, England