Forum Archive

Display image from photos.pick_image?

jerry

I’m playing around with displaying a photo from the photo library in a very simple script; however, I’m doing something wrong: where the image should be, it displays white in the shape that the image ought to be:

from scene import *
import photos

class MyScene(Scene):   
    def __init__(self, mapimage):
        mapimage2 = mapimage.convert('RGBA')
        self.mapimage = load_pil_image(mapimage2)

    def draw(self):
        # This will be called for every frame
        background(1, 1, .5)
        fill(1, 0, 0)

        image(self.mapimage, 0, 0)

        # Draw a red circle for every finger that touches the screen:
        for touch in self.touches.values():
            ellipse(touch.location.x - 50, touch.location.y - 50, 100, 100)

mapimage = photos.pick_image(show_albums=True)
if mapimage:
    scene = MyScene(mapimage)
    run(scene, frame_interval=1)
else:
    print 'Canceled or invalid image.'

Touching the screen drags one or more red circles around, but the image doesn’t appear.

mmontague

This code didn't work at all for me until I added a call to Scene's init() function, e.g. using super():

def __init__(self, mapimage):
        mapimage2 = mapimage.convert('RGBA')
        self.mapimage = load_pil_image(mapimage2)
        super(MyScene,self).__init__() 

Now I see the image I picked in the lower left of the screen.

jerry

Are you sure you didn’t make any other changes to get it to work? I verified before adding the super() call that I was able to drag red dots across the screen; and after adding super() I still see only a white rectangle in the lower-left where the image ought to be. (Dragging red dots continues to work.)

mmontague

Yeah that is all I changed.
Could it be an issue with the image itself?
What if, just for debug, you do a mapimage.show() to put it on the console, right after you get it from photos.pick_image. To see what it looks like there.

jerry

Yes, mapimage.show() displays it in the console. I made this change to test it:

if mapimage:
    scene = MyScene(mapimage)
    run(scene, frame_interval=1)
    mapimage2 = mapimage.convert('RGBA')
    mapimage.show()
    mapimage2.show()
else:
    print 'Canceled or invalid image.'

I tried this on all of the images I'd been using for testing, and in each case, I saw the image twice in the console after hitting the "x" in the upper right of the scene. But it still displays just a white rectangle (surrounded by yellow) in the scene (which I can move red dots around on).

mmontague

I really don't know then.
Have you checked that the built-in images work, like
image('Sun_1', 0, 0)

JonB

@mmontague said:

super(MyScene,self).init()

mmontague, are you using the beta, or 1.5? how about the @jerry? are you using ipad or iphone?

On the current beta, 32 bit on ipad, I also verified the code works with the addition of the supr init call -- without it, the code does not run at all . You might want to not name your scene instance scene, that is just asking for trouble, especially if you are using 1.5 where globals do not get cleared.

JonB

turns out it is hard to search the forum for function names with underscores... but this was asked/answered once before:
https://forum.omz-software.com/topic/1590/scene-load_pil_image

bottom line, load_pil_image from setup, not init, and you should be good. montague is likely using the beta, which does not seem to have that limitation anymore.

mmontague

Yes, I have the beta.

jerry

Thank you! That was the issue. For future reference, here is the code that works:

from scene import *
import photos

class MyScene(Scene):
    def __init__(self, mapimage):
        self.mapimage = mapimage.convert('RGBA')
        super(MyScene, self).__init__()

    def setup(self):
        self.mapimage = load_pil_image(self.mapimage)

    def draw(self):
        background(1, 1, .5)
        image(self.mapimage, 0, 0)

        # Draw a red circle for every finger that touches the screen:
        fill(1, 0, 0)
        for touch in self.touches.values():
            ellipse(touch.location.x - 50, touch.location.y - 50, 100, 100)

mapimage = photos.pick_image(show_albums=True)
if mapimage:
    scene = MyScene(mapimage)
    run(scene, frame_interval=1)
else:
    print 'Canceled or invalid image.'