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.


    How do I convert a ui.Image into a scene.SpriteNode?

    Pythonista
    2
    2
    2168
    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.
    • ccc
      ccc last edited by ccc

      import scene
      
      def make_oval_sprite(width=100, height=100, fg_color='blue', bg_color='grey'):
          with scene.ui.ImageContext(width, height) as ctx:
              scene.ui.set_color(bg_color)
              scene.ui.Path.rounded_rect(0, 0, width, height, height / 10).fill()
              scene.ui.set_color(fg_color)
              scene.ui.Path.oval(0, 0, width, height).fill()
              return ctx.get_image()
      
      class MyScene(scene.Scene):
          def setup(self):  # scene.SpriteNode() wants an image name, not an image.
              scene.SpriteNode(make_oval_sprite(), parent=self, position=(200,200))
              
      scene.run(MyScene())
      # make_oval_sprite().show()
      
      1 Reply Last reply Reply Quote 0
      • zipit
        zipit last edited by

        You have to cast the ui.Image into a scene.Texture. Something like that:

        return scene.Texture(ctx.get_image())

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