mrteach
Posts: 181
Joined: Sun May 26, 2013 6:49 am

displaying a .jpg image on screen

Fri Aug 29, 2014 5:18 pm

Alright so I've searched and tried many different programs to try to just display a simple image on my pi. (jpg).

And nothing is really working. I forgot what program I used but the closest I got was displaying an image but it wasn't scaled to the screen.

I found a photobooth program and it displayed the image perfectly using pygame. I tried to disassemble the code, but it didn't work for some reason... (no image showed up).

I'm almost leaning towards giving up and just converting my images to a video and using omxplayer...

So anyone can help? I'm trying to just display a jpg on my screen that is using rca

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

Re: displaying an image on screen

Fri Aug 29, 2014 5:27 pm

What was wrong with the pygame code? I'm a fan of pygame so would be happy to help but other people may also chip in with some other/easier solutions.
RPi Information Screen: plugin based system for displaying weather, travel information, football scores etc.

mrteach
Posts: 181
Joined: Sun May 26, 2013 6:49 am

Re: displaying an image on screen

Fri Aug 29, 2014 6:21 pm

it just didn't display any image...
let me try again (I deleted the code that I was working on)
Looking back at the photobooth code I'm thinking perhaps I didn't loop it? do I need to use try: or while True:?
I just tried this and it just freezes (ctrl+c doesn't quit the program)

Code: Select all

import time
import pygame

w = 700 
h = 500
transform_x = 600 #648 #how wide to scale the jpg when replaying
transfrom_y = 486 #how high to scale the jpg when replaying
offset_x = 20 #how far off to left corner to display photos
offset_y = 10 #how far off to left corner to display photos

try:
	pygame.init()
	screen = pygame.display.set_mode((w,h),pygame.FULLSCREEN) 
	pygame.display.set_caption('Photo Booth Pics')
	pygame.mouse.set_visible(False) #hide the mouse cursor	
	filename = image.jpg
	img=pygame.image.load(filename) 
	img = pygame.transform.scale(img,(transform_x,transfrom_y))
	screen.blit(img,(offset_x,offset_y))
	pygame.display.flip() # update the display
	time.sleep(3) # pause
finally:
	pygame.quit()
EDIT:

I removed the sleep and it gave me this error
pygame.error: Unable to open a console terminal

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

Re: displaying an image on screen

Fri Aug 29, 2014 6:27 pm

you've got a lot going on inside a try block. get rid of the try for now so we can catch all errors and debug.

This line "filename = image.jpg" is going to give you an error so you'll fall out of the try block at that point. I guess you mean:

Code: Select all

filename = "image.jpg"
I'm also wary of pygame.FULLSCREEN - try it without this option first.
Last edited by elParaguayo on Fri Aug 29, 2014 6:32 pm, edited 1 time in total.
RPi Information Screen: plugin based system for displaying weather, travel information, football scores etc.

User avatar
DougieLawson
Posts: 39613
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: displaying an image on screen

Fri Aug 29, 2014 6:29 pm

mrteach wrote:And nothing is really working. I forgot what program I used but the closest I got was displaying an image but it wasn't scaled to the screen.
That was probably fbi.

Your best bet for a full screen display is something like PiPresents or Minimal Kiosk Browser

http://www.raspberrypi.org/forums/viewt ... 38&t=29397
http://www.raspberrypi.org/forums/viewt ... 66&t=40860
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

Criticising any questions is banned on this forum.

Any DMs sent on Twitter will be answered next month.
All fake doctors are on my foes list.

mrteach
Posts: 181
Joined: Sun May 26, 2013 6:49 am

Re: displaying an image on screen

Fri Aug 29, 2014 6:40 pm

i tried fbi before and it didn't work. surprisingly I just tried it again and it worked =D

it is really jumpy though (I guess because of refreshing) any possible way to fix this?

I may want to use pygame for something else though... so let me try removing fullscreen

also how do i exit fbi...

EDIT: pkill seemed to work =)

mrteach
Posts: 181
Joined: Sun May 26, 2013 6:49 am

Re: displaying an image on screen

Fri Aug 29, 2014 6:46 pm

Unable to open a console terminal

still got that after removing fullscreen

-------------------------

Pipresents looks cool too though.

mrteach
Posts: 181
Joined: Sun May 26, 2013 6:49 am

Re: displaying an image on screen

Fri Aug 29, 2014 7:14 pm

alright it seems like not using sudo was giving me that error. I got it to display an image now... but only when I ctrl+C does the image popup for a second

it seems like image.load is freezing until I ctrl+c and then it process through (with the image...)

I tried this and if left alone it would just hang.. when I use ctrl+c it will continue and load the picture..

Code: Select all

screen = pygame.display.set_mode((700,500))
pygame.mouse.set_visible(False) #hide the mouse cursor	
filename = "image.jpg"
print filename
img=pygame.image.load(filename) 
img = pygame.transform.scale(img,(transform_x,transfrom_y))
screen.blit(img,(offset_x,offset_y))
pygame.display.flip() # update the display
time.sleep(replay_delay) # pause
pygame.quit()

mrteach
Posts: 181
Joined: Sun May 26, 2013 6:49 am

Re: displaying an image on screen

Fri Aug 29, 2014 7:26 pm

okay so I re-edited based on the photobooth script I found and got this

Code: Select all

import time
import pygame, sys, traceback
from signal import alarm, signal, SIGALRM, SIGKILL

w = 700 
h = 500
transform_x = 600 #648 #how wide to scale the jpg when replaying
transfrom_y = 486 #how high to scale the jpg when replaying
offset_x = 20 #how far off to left corner to display photos
offset_y = 10 #how far off to left corner to display photos
replay_delay = 1 # how much to wait in-between showing pics on-screen after taking
replay_cycles = 2 # how many times to show each photo on-screen after taking

class Alarm(Exception):
        pass
def alarm_handler(signum, frame):
	raise Alarm
signal(SIGALRM, alarm_handler)
alarm(3)
try:
	pygame.init()
	screen = pygame.display.set_mode((w,h),pygame.FULLSCREEN)
	alarm(0)
except Alarm:
	raise KeyboardInterrupt
pygame.mouse.set_visible(False) #hide the mouse cursor	
filename = "image.jpg"
print filename
img=pygame.image.load(filename) 
img = pygame.transform.scale(img,(transform_x,transfrom_y))
screen.blit(img,(offset_x,offset_y))
pygame.display.flip() # update the display
time.sleep(replay_delay) # pause
pygame.quit()
I guess I had to add the alarm stuff... or it wouldn't work... properly

also it takes a while for a photo to load (5 seconds or so)... I thought it was beign my image was large so I changed it but the same problem.

mrteach
Posts: 181
Joined: Sun May 26, 2013 6:49 am

Re: displaying an image on screen

Fri Aug 29, 2014 7:44 pm

changed one of the alarms to (1) --- no sure what this does... but it seemed to have made the load time to about 3 seconds instead of 5.

Any reasons why pygame images load slow? =)

oh and now fbi doesnt work...

gordon77
Posts: 5133
Joined: Sun Aug 05, 2012 3:12 pm

Re: displaying an image on screen

Fri Aug 29, 2014 7:51 pm

Have you noticed your program says transfrom_y?

mrteach
Posts: 181
Joined: Sun May 26, 2013 6:49 am

Re: displaying an image on screen

Fri Aug 29, 2014 7:56 pm

it didn't really make a difference (as the variable was misnamed too), so no I didn't notice (fixed it though just for aesthetics).

mrteach
Posts: 181
Joined: Sun May 26, 2013 6:49 am

Re: displaying an image on screen

Fri Aug 29, 2014 8:48 pm

so to summarize so far. it works, but has a loading delay.. (i guess pygame takes 3 or so seconds to load a 600x600 photo?)

I'm not sure what the alarm aspects do in the script, but they seem to be needed in order for it to process correctly (any way to get rid of this, as it could be the loading delay.

if alarm aspects were removed the program would hang around filename = "image.jpg". when ctrl+c is used the program would continue (e.g. print filename and the rest of the code).

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

Re: displaying an image on screen

Fri Aug 29, 2014 9:24 pm

I've seen that alarm code used before by some people when pygame won't load. I've never had to use it before though.

It might be worth putting an event loop in the script as not having this can sometimes cause the script to hang.
RPi Information Screen: plugin based system for displaying weather, travel information, football scores etc.

mrteach
Posts: 181
Joined: Sun May 26, 2013 6:49 am

Re: displaying an image on screen

Sat Aug 30, 2014 2:05 am

elParaguayo wrote:I've seen that alarm code used before by some people when pygame won't load. I've never had to use it before though.

It might be worth putting an event loop in the script as not having this can sometimes cause the script to hang.
can you provide me with an example? =)

Sleep Mode zZ
Posts: 319
Joined: Sun Aug 19, 2012 5:56 am
Location: Finland

Re: displaying an image on screen

Sat Aug 30, 2014 3:05 am

mrteach wrote:so to summarize so far. it works, but has a loading delay.. (i guess pygame takes 3 or so seconds to load a 600x600 photo?)
If you tried only to load one picture, the "loading delay" will have more to do with the time that Python and Pygame takes loading and starting up than with the time loading and displaying an image takes. Also, the start up time of Python and Pygame will be faster if they are already in the computer's memory - the first run is slower than the succeeding runs. That difference should not be attributed to any code changes made between the first and second run.

Sleep Mode zZ
Posts: 319
Joined: Sun Aug 19, 2012 5:56 am
Location: Finland

Re: displaying an image on screen

Sat Aug 30, 2014 3:36 am

A simple example with an event loop (no exception handling when loading the pictures):

Code: Select all

import pygame, sys

pygame.init()
screen = pygame.display.set_mode((700,500))
images = []
images.append(pygame.image.load("picture01.jpg"))
images.append(pygame.image.load("picture02.jpg"))
show = 0
clock = pygame.time.Clock()
while True:
	for event in pygame.event.get():
		if event.type == pygame.QUIT:
			pygame.quit()
			sys.exit()
		if event.type == pygame.KEYDOWN:
			if event.key == pygame.K_ESCAPE:
				pygame.quit()
				sys.exit()
			if event.key == pygame.K_SPACE:
				show = (show + 1) % len(images)
			
	screen.blit(images[show], (0 , 0))
	clock.tick(30)
	pygame.display.update()
Space key gives the next image, esc key quits. I only tried this with my desktop computer and the switching of images is practically instantaneous.

Sleep Mode zZ
Posts: 319
Joined: Sun Aug 19, 2012 5:56 am
Location: Finland

Re: displaying an image on screen

Sat Aug 30, 2014 4:06 am

Although the previous version did not show any significant cpu use on my desktop computer, it does blit the image and update the whole screen 30 times every second. So here I moved the the blitting and updating to be done only when the image is switched to the next one:

Code: Select all

import pygame, sys

pygame.init()
screen = pygame.display.set_mode((700,500))
images = []
images.append(pygame.image.load("picture01.jpg"))
images.append(pygame.image.load("picture02.jpg"))
show = 0
clock = pygame.time.Clock()
screen.blit(images[show], (0 , 0))
pygame.display.update()
while True:
	for event in pygame.event.get():
		if event.type == pygame.QUIT:
			pygame.quit()
			sys.exit()
		if event.type == pygame.KEYDOWN:
			if event.key == pygame.K_ESCAPE:
				pygame.quit()
				sys.exit()
			if event.key == pygame.K_SPACE:
				show = (show + 1) % len(images)
				screen.blit(images[show], (0 , 0))
				pygame.display.update()
			
	clock.tick(30)

mrteach
Posts: 181
Joined: Sun May 26, 2013 6:49 am

Re: displaying an image on screen

Sat Aug 30, 2014 10:53 pm

oh I just want to load it one by one... unless I can hide the pygame if omxplayer is playing. (or omxplayer goes ontop of pygame already.

Sleep Mode zZ
Posts: 319
Joined: Sun Aug 19, 2012 5:56 am
Location: Finland

Re: displaying an image on screen

Sun Aug 31, 2014 12:32 am

Then you can simplify it:

Code: Select all

import pygame, sys
pygame.init()
screen = pygame.display.set_mode((700,500))
pygame.mouse.set_visible(False)
image = pygame.image.load("picture01.jpg")
image = pygame.transform.scale(image, (700,500))
screen.blit(image, (0 , 0))
clock = pygame.time.Clock()
pygame.display.update()
while True:
	for event in pygame.event.get():
		if event.type == pygame.QUIT:
			pygame.quit()
			sys.exit()
		if event.type == pygame.KEYDOWN:
			if event.key == pygame.K_ESCAPE:
				pygame.quit()
				sys.exit()	
	clock.tick(30)
Or with a fixed time without an event loop:

Code: Select all

import pygame, time
pygame.init()
screen = pygame.display.set_mode((700,500))
pygame.mouse.set_visible(False)
image = pygame.image.load("picture01.jpg")
image = pygame.transform.scale(image, (700,500))
screen.blit(image, (0 , 0))
pygame.display.update()
time.sleep(10)
pygame.quit()
Both work without errors on my desktop computer (within X windows). Of course, loading the Python interpreter and loading and initializing Pygame will take a few seconds, especially on the Pi.

Edit: I unnecessarily created a Clock object in the second example although it was not used for anything. Removed it now.
Last edited by Sleep Mode zZ on Sun Aug 31, 2014 3:36 am, edited 1 time in total.

mrteach
Posts: 181
Joined: Sun May 26, 2013 6:49 am

Re: displaying a .jpg image on screen

Sun Aug 31, 2014 3:25 am

nice it works. and that lag time i guess it is just loading... cause it happens with your script too.

Thanks.

mrteach
Posts: 181
Joined: Sun May 26, 2013 6:49 am

Re: displaying a .jpg image on screen

Sun Aug 31, 2014 3:31 am

okay one thing I don't understand is why the event thing doesn't work...

I got event is not defined error

Code: Select all

  File "test.py", line 62, in <module>
    if event.type == pygame.KEYDOWN:
NameError: name 'event' is not defined

Code: Select all

while True:
	for event in pygame.event.get():
		if event.type == pygame.QUIT:
			screen.fill([0, 0, 0])
			pygame.display.update()
			pygame.quit()
			sys.exit()
			 
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_ESCAPE:
			       screen.fill([0, 0, 0])
				    pygame.display.update()
				    pygame.quit()
				    sys.exit()   
    clock.tick(30)
Last edited by mrteach on Mon Sep 01, 2014 5:13 pm, edited 1 time in total.

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

Re: displaying a .jpg image on screen

Sun Aug 31, 2014 7:00 am

Check your indenting. It looks odd in your post. Make sure you're not mixing tabs and spaces. I won't be back in front of a computer until tonight so won't be able to help more before then but suspect other people will.
RPi Information Screen: plugin based system for displaying weather, travel information, football scores etc.

Sleep Mode zZ
Posts: 319
Joined: Sun Aug 19, 2012 5:56 am
Location: Finland

Re: displaying a .jpg image on screen

Sun Aug 31, 2014 8:22 am

In addition to what elParaguayo said about indentation, it seems that when quitting the program, you are trying to fill the pygame screen with black. Maybe you expect that the black screen will stay on after pygame stops? Won't happen :) The black screen - that probably will not have time to appear even for one refresh cycle of your monitor (e.g. 1/60 of a second) - will disappears when the pygame process ends .Your display will show what ever was there before the pygame process started.

mrteach
Posts: 181
Joined: Sun May 26, 2013 6:49 am

Re: displaying a .jpg image on screen

Mon Sep 01, 2014 5:05 pm

Yeah I tried copy and pasting from my script to the post and it got a little messed up. Here is what I have in my script. I think the indentation is right.

I'm filling with black mainly because there seems to be a residue leftover that leaves a line of the old image. so blacking the screen gets rid of that before it quits.

Code: Select all

while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            screen.fill([0, 0, 0])
            pygame.display.update()
            pygame.quit()
            sys.exit()
		 
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_ESCAPE:
                screen.fill([0, 0, 0])
                pygame.display.update()
                pygame.quit()
                sys.exit()   
    clock.tick(30)

Return to “Python”