Obligatory slideshow first project - need help fadeout and in of image

51 views
Skip to first unread message

Rick Bonafied

unread,
Nov 24, 2019, 12:34:43 PM11/24/19
to pyglet-users
Hi,

This is not only my first pyglet attempt but also my first python program.  And yes, my first project is a slideshow.  I have a raspberry pi that will drive an older monitor that will display family photos.  I'm mostly done and really happy with how it behaves.   There are no transition affects between images.  Unfortunately, I don't really understand GL or texture use or blending or any of that.  I'm hoping someone can provide me a snippet of code that will do the trick.

Basically I created a subclass of Window called ImageWindow.  I load the image into a single sprite and after every 10 scheduled seconds or so, the new image is loaded into that same sprite and displayed.  This works great when progressing through images without any transition affect.  I've tried to schedule a "fade out" method that would fire off before the next image.  The "fade out" method just reduced the sprite opacity by a small increment until it got to zero.  Then the schedule was removed.  This did some of what I want but I don't think it was very efficient or very reliable.  And then the fade-in of the next image had me stumped.  I tried to do the reverse of the fade-out but combining that with the time it took to load the next image from a network share made the fade-out last too long before the fade in.

Is there a cleaner/proper way to do this?

Thank you!
R

gimmick2509 .

unread,
Nov 24, 2019, 5:09:58 PM11/24/19
to pyglet...@googlegroups.com
Hello and welcome to the pyglet community! You can create the second Sprite while the image is shown for 10 seconds or so and load the next image here. Use fade-out effect for the first Sprite, delete it when opacity reaches 0 and then activate fade-in effect for the remaining Sprite. Hope it helps!

--
You received this message because you are subscribed to the Google Groups "pyglet-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pyglet-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/pyglet-users/f560aab8-93c3-4a07-a4e9-67e19958f351%40googlegroups.com.

Rick Bonafied

unread,
Nov 29, 2019, 2:50:36 PM11/29/19
to pyglet-users
Thank you, gimmick2509!

I think the problem I have with that approach is timing.  So I have a schedule that runs every second.  After 10 seconds (or whatever the user wishes), that's when I load the next image.  This allows me to show a countdown timer in the corner of the screen with the number of seconds remaining before the next image is shown.

My newbie thinking is I would need to set up another "temporary" schedule/clock interval of say like point one ( .1) seconds which would reduce the opacity.  However, I found that approach to rather choppy depending on the size of the next image and reading it from disk.  I also feel like have 2 more temporary schedules is not clean (1 to fade out, then 1 to fade in)  Is this possible to do through some sprite animation?

Also, how can I load the second image during the 'countdown' time without delaying said countdown?  I've found that if I do a heavy load task, all pyglet processing stops until that task finishes.  In real terms it seems like if i'm counting down from 10-9-8 and i do a load that takes 2 seconds, the countdown pauses at 8 for 2 seconds and resumes at .  It'd be nice if I could load the image into a sprite in the "background" without affecting the countdown.

I'm sure there is a clean/elegant way to do it, I'm just too new at this to know what it is.
To unsubscribe from this group and stop receiving emails from it, send an email to pyglet...@googlegroups.com.

Greg Ewing

unread,
Nov 29, 2019, 5:33:02 PM11/29/19
to pyglet...@googlegroups.com
On 30/11/19 8:50 am, Rick Bonafied wrote:
> It'd be nice if I could load the image into a sprite in
> the "background" without affecting the countdown.

What are you using to load the image? You'll have to do it in
another thread using something that releases the GIL during
the load.

--
Greg

Rick Bonafied

unread,
Nov 29, 2019, 8:59:49 PM11/29/19
to pyglet-users
Because of the memory limitations on a raspberry pi, I discovered that if I use Pillow to read the image and scale it to the screen dimensions instead of the photo untouched dimensions it would work really well.  Since doing that, I haven't run out of memory putting a large image into a sprite. 

So basically I do something like this:
  1. A schedule fires off and calls the image load method
  2. Image load =
    1. tmp_pilimage = PIL.Image.open(path_to_image)
    2. If the orientation of the image needs to be adjusted, do so then
    3. tmp_pilimage.thumbnail((target_width, target_height),PIL.Image.LANCZOS)
  3. curpic_image = pyglet.image.load(path_to_image)
  4. curpic_sprite = pyglet.sprite.Sprite(curpic_image)
What does release the GIL mean?

Greg Ewing

unread,
Nov 29, 2019, 11:59:45 PM11/29/19
to pyglet...@googlegroups.com
On 30/11/19 2:59 pm, Rick Bonafied wrote:

> What does release the GIL mean?

GIL = Global Interpreter Lock. It's a lock that the Python
interpreter uses to protect its data structures, and it only
allows one thread in a process to execute Python code at a
time.

A well-written extension module will release the GIL while
it's doing something time-consuming that doesn't touch
Python objects, to give other threads a chance to run
Python code. I don't know whether Pillow does this, but
quite likely it does. Try doing the image loading in
another thread and see if it will run concurrently with
your timer.

--
Greg

Rick Bonafied

unread,
Nov 30, 2019, 2:54:58 PM11/30/19
to pyglet-users
Hi Greg,

I don't know enough Python yet to understand how to put something on another thread but I'll continue to tinker and experiment.

Regards,
Rick
Reply all
Reply to author
Forward
0 new messages