serverius
Posts: 9
Joined: Tue Jan 09, 2018 9:17 pm

Code for displaying an image (raspberry pi 3 & Python)

Tue Jan 09, 2018 9:39 pm

Hello Everybody,

I have a question that i hope somebody can help me with.

At work (i work as a designer and IT Manager for a company who designs and installs indoor playgrounds) we are trying work out an idea we have regarding a new play item to use in our playgrounds.

The idea was to create a kind of interactive traffic circuit combined with images that we are going to project using beamers, sound and some IO stuff for infrared etc.

Now the IO part is working but i have a problem with the code i should use regarding the images.
When i use the terminal with feh i have no problem to display the images fullscreen, but this does not seem to work very well from within python (or i am doing something wrong, kind of a python novice to be honest, but working on it :-) )

From the terminal, i use the following command :

Code: Select all

feh --hide-pointer -x -q -B black -g 1280x800 /path/to/image/pic.jpg
This works perfect from within the terminal but not from within python.

Could somebody help me out, what i need it that the image is displayed fullscreen (1280x800) for a numer of seconds and at the same time it should also play the sound. Now for the sound part, i did not go into that part deeper so maybe this will be easy but i am completely stuck, so i hope somebody could help me out with some code i can put into the rest of my code.

And any tips regarding the sound code would also help me out immensely. the idea is that it keeps playing/repeating a specific mp3 file for a specific number of seconds.

Hope somebody could help me out,

Thanks a lot for any input.

Best Regards,
serverius

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

Re: Code for displaying an image (raspberry pi 3 & Python)

Wed Jan 10, 2018 6:45 pm

Check out PyGame, it does all that stuff & loads of online examples.

Dave.
Apple say... Monkey do !!

User avatar
OutoftheBOTS
Posts: 674
Joined: Tue Aug 01, 2017 10:06 am

Re: Code for displaying an image (raspberry pi 3 & Python)

Fri Jan 12, 2018 11:35 am

The PIL (Python Imaging Library) library is the main python graphic library see http://effbot.org/imagingbook/

serverius
Posts: 9
Joined: Tue Jan 09, 2018 9:17 pm

Re: Code for displaying an image (raspberry pi 3 & Python)

Fri Jan 12, 2018 2:57 pm

davef21370 wrote:
Wed Jan 10, 2018 6:45 pm
Check out PyGame, it does all that stuff & loads of online examples.

Dave.
Hi,

Thanks for the info.

I had a look at the pygame but could not get that to work like i needed.

Code: Select all

import pygame #voor beeld en geluid
from time import sleep
from pygame.locals import *
pygame.init()
WIDTH = 1280
HEIGHT = 800
windowSurface = pygame.display.set_mode((WIDTH, HEIGHT), 0, 0)
img = pygame.image.load("/home/pi/images/pic06.jpg")
windowSurface.blit(img, (0, 0))
pygame.display.flip()

# sound code        
pygame.mixer.init()
pygame.mixer.music.load("/home/pi/sound/dog.mp3")
pygame.mixer.music.play()
while pygame.mixer.music.get_busy() == True:
    continue
I used this code, but the problem i encountered was that there was a delay between the time the screen appears and the actual displaying of the image.

I would need it to display a certain image fullscreen at 1280x800 without borders and for a certain amount of seconds.

I found another solution for the images with the following code :

Code: Select all

import RPi.GPIO as GPIO
from time import sleep
import os
os.system ('feh --hide-pointer -x -q -B black -g 1280x800 "/home/pi/images/pic01.jpg" &')
The only problem i have is that the images do not dissapear, i use a total of 12 images in the whole code and when it is finished i need to press escape 12 times because this just stacks the image screens on top of each other.

Any ideas on how to close the images with the feh command ?

And if anybody knows what pygame code i need to use to get the same results as with the feh command, that would really help me out.

Thanks,
Last edited by serverius on Fri Jan 12, 2018 3:00 pm, edited 1 time in total.

serverius
Posts: 9
Joined: Tue Jan 09, 2018 9:17 pm

Re: Code for displaying an image (raspberry pi 3 & Python)

Fri Jan 12, 2018 2:58 pm

OutoftheBOTS wrote:
Fri Jan 12, 2018 11:35 am
The PIL (Python Imaging Library) library is the main python graphic library see http://effbot.org/imagingbook/
Thanks for the advice, i'll have a look at that.

Thanks,

User avatar
elParaguayo
Posts: 1941
Joined: Wed May 16, 2012 12:46 pm
Location: London, UK

Re: Code for displaying an image (raspberry pi 3 & Python)

Fri Jan 12, 2018 7:16 pm

You could use the subprocess module rather than os.system.

As an example:

Code: Select all

import subprocess

#Display the image
image = subprocess.Popen(["feh", "--hide-pointer", "-x", "-q", "-B", "black", "-g", "1280x800", "/home/pi/images/pic01.jpg"])

# Do other stuff here...

# You can now close the image by doing
image.kill()
RPi Information Screen: plugin based system for displaying weather, travel information, football scores etc.

serverius
Posts: 9
Joined: Tue Jan 09, 2018 9:17 pm

Re: Code for displaying an image (raspberry pi 3 & Python)

Fri Jan 12, 2018 10:09 pm

You have saved my life!!!!!

Works perfect, thanks ever so much
elParaguayo wrote:
Fri Jan 12, 2018 7:16 pm
You could use the subprocess module rather than os.system.

As an example:

Code: Select all

import subprocess

#Display the image
image = subprocess.Popen(["feh", "--hide-pointer", "-x", "-q", "-B", "black", "-g", "1280x800", "/home/pi/images/pic01.jpg"])

# Do other stuff here...

# You can now close the image by doing
image.kill()

User avatar
elParaguayo
Posts: 1941
Joined: Wed May 16, 2012 12:46 pm
Location: London, UK

Re: Code for displaying an image (raspberry pi 3 & Python)

Sat Jan 13, 2018 7:22 am

Well, I wouldn't go that far but I'm glad I could help!
RPi Information Screen: plugin based system for displaying weather, travel information, football scores etc.

Jamesm42
Posts: 24
Joined: Mon Sep 24, 2018 3:36 am

Re: Code for displaying an image (raspberry pi 3 & Python)

Thu Sep 27, 2018 2:44 am

I tried this method of using the subprocess.Popen.....and I get no image on the screen. I installed FEH and I tried to install PIL, but PIL gave me a message saying it could not find a version that satisfies the requirement PIL(from versions:) No matching distribution found for PIL

What went wrong?

Return to “Python”