omz:forum

    • Register
    • Login
    • Search
    • Recent
    • Popular

    Welcome!

    This is the community forum for my apps Pythonista and Editorial.

    For individual support questions, you can also send an email. If you have a very short question or just want to say hello — I'm @olemoritz on Twitter.


    Scene-script crashes

    Pythonista
    3
    6
    2497
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • brumm
      brumm last edited by

      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())
      
      1 Reply Last reply Reply Quote 0
      • omz
        omz last edited by

        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)
        
        1 Reply Last reply Reply Quote 0
        • brumm
          brumm last edited by

          Thank you very much.

          1 Reply Last reply Reply Quote 0
          • brumm
            brumm last edited by

            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.

            1 Reply Last reply Reply Quote 0
            • ccc
              ccc last edited by

              You can make a few changes to OMZ's code above:<br />

              1. Change two instances of 'image' to 'img'.<br />
              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.')
              
              1 Reply Last reply Reply Quote 0
              • brumm
                brumm last edited by

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

                1 Reply Last reply Reply Quote 0
                • First post
                  Last post
                Powered by NodeBB Forums | Contributors