Forum Archive

Scene-script crashes

brumm

This code is showing a choosen picture for approx. 5 seconds until it crashed. What's wrong?

import sys
import photos
from scene import *

class Test(Scene):
    def draw(self):
        image(load_pil_image(img), 10, 10, 800, 600)    

pics = photos.get_count()
if (pics == 0):
    print 'Sorry no access or no pictures.'
    sys.exit()
img = photos.pick_image()
if not image:
    print 'No picture was selected. Good bye!'
    sys.exit()
run(Test())
omz

Don't load the same image over and over again in the draw method, you'll run out of memory this way.

This should work:

import photos
import sys
from scene import *

class Test(Scene):
    def __init__(self, img=None):
        self.img = img

    def draw(self):
        image(self.img, 10, 10, 800, 600)    

pics = photos.get_count()
if (pics == 0):
    print 'Sorry no access or no pictures.'
    sys.exit()
img = photos.pick_image()
if not image:
    print 'No picture was selected. Good bye!'
    sys.exit()
s = Test(image)
run(s)
brumm

Thank you very much.

brumm

This line

image(self.img, 10, 10, 800, 600)

ends in a TypeError: coercing to Unicode: need string or buffer, instance found

Then I tried this code

self.img = load_pil_image(img)

and I got a white rectangle

and with

image(load_pil_image(self.img), 10, 10, 800, 600)

it crashed again.

ccc

You can make a few changes to OMZ's code above:

1. Change two instances of 'image' to 'img'.

2. Add this setup() method to your Scene:

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

The trick is that photos.pick_image() must be called before scene.Scene.setup() and scene.load_pil_image(self.img) must be called in or after scene.Scene.setup().

This works for me:

import photos, scene

class MyScene(scene.Scene):
    def __init__(self):
        self.img = photos.pick_image()
        if self.img:
            scene.run(self)
        else:
            print('No picture was selected. Good bye!')

    def setup(self):
        self.layer = scene.Layer(self.bounds)
        self.layer.image = scene.load_pil_image(self.img)
        self.add_layer(self.layer)

    def draw(self):
        self.root_layer.update(self.dt)
        self.root_layer.draw()

if photos.get_count():
    MyScene()
else:
    print('Sorry no access or no pictures.')
brumm

Thank you very much. Now I can start my next project.