danieljay
Posts: 49
Joined: Sun Sep 16, 2012 5:01 pm
Contact: Website

GPIO button press image display

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()
    

User avatar
davef21370
Posts: 875
Joined: Fri Sep 21, 2012 4:13 pm
Location: Earth But Not Grounded

Re: GPIO button press image display

Wed Feb 20, 2013 6:09 pm

Try...

Code: Select all

from PIL import Image
Dave.
Apple say... Monkey do !!

danieljay
Posts: 49
Joined: Sun Sep 16, 2012 5:01 pm
Contact: Website

Re: GPIO button press image display

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.

User avatar
croston
Posts: 670
Joined: Sat Nov 26, 2011 12:33 pm
Location: Blackpool
Contact: Website

Re: GPIO button press image display

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!

danieljay
Posts: 49
Joined: Sun Sep 16, 2012 5:01 pm
Contact: Website

Re: GPIO button press image display

Wed Feb 20, 2013 6:40 pm

before I attempt, should a simple import Image work with my current setup?

User avatar
davef21370
Posts: 875
Joined: Fri Sep 21, 2012 4:13 pm
Location: Earth But Not Grounded

Re: GPIO button press image display

Wed Feb 20, 2013 6:51 pm

Go with croston's post and you should be good.

Dave.
Apple say... Monkey do !!

danieljay
Posts: 49
Joined: Sun Sep 16, 2012 5:01 pm
Contact: Website

Re: GPIO button press image display

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.

User avatar
croston
Posts: 670
Joined: Sat Nov 26, 2011 12:33 pm
Location: Blackpool
Contact: Website

Re: GPIO button press image display

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.

danieljay
Posts: 49
Joined: Sun Sep 16, 2012 5:01 pm
Contact: Website

Re: GPIO button press image display

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()

danieljay
Posts: 49
Joined: Sun Sep 16, 2012 5:01 pm
Contact: Website

Re: GPIO button press image display

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')

texy
Forum Moderator
Forum Moderator
Posts: 5003
Joined: Sat Mar 03, 2012 10:59 am
Location: Berkshire, England

Re: GPIO button press image display

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
Various male/female 40- and 26-way GPIO header for sale here ( IDEAL FOR YOUR PiZero ):
https://www.raspberrypi.org/forums/viewtopic.php?f=93&t=147682#p971555

danieljay
Posts: 49
Joined: Sun Sep 16, 2012 5:01 pm
Contact: Website

Re: GPIO button press image display

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.

texy
Forum Moderator
Forum Moderator
Posts: 5003
Joined: Sat Mar 03, 2012 10:59 am
Location: Berkshire, England

Re: GPIO button press image display

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
Various male/female 40- and 26-way GPIO header for sale here ( IDEAL FOR YOUR PiZero ):
https://www.raspberrypi.org/forums/viewtopic.php?f=93&t=147682#p971555

LAB
Posts: 3
Joined: Fri Jul 24, 2015 11:18 pm

Re: GPIO button press image display

Fri Jul 24, 2015 11:24 pm

Hello

I have two questions:
1) With your above code (Thank you) how would I make the image display full screen without a title bar at the top?
2) When a second button, connected to another GPIO pin, is pressed, how would I change the image to a second image?

I'm new to this stuff and your code was very helpful. Any ideas? LAB.

Return to “Python”

Who is online

Users browsing this forum: No registered users and 22 guests