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.


    Display image from photos.pick_image?

    Pythonista
    3
    10
    7045
    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.
    • jerry
      jerry last edited by

      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.

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

        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.

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

          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.)

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

            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.

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

              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).

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

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

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

                  This post is deleted!
                  1 Reply Last reply Reply Quote 0
                  • JonB
                    JonB last edited by

                    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.

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

                      Yes, I have the beta.

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

                        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.'
                        
                        1 Reply Last reply Reply Quote 0
                        • First post
                          Last post
                        Powered by NodeBB Forums | Contributors