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.


    canvas.draw_image() function does not work

    Pythonista
    image canvas function
    3
    15
    1330
    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.
    • Trüff
      Trüff last edited by

      Hey there,
      I have a problem with the following code:

      import canvas as cv
      
      # creates canvas
      w = 200
      h = 500
      cv.set_size(w, h)
      
      # size of the image
      img_w = 190
      img_h = 490
      
      cv.begin_updates()
      
      # draws a white rectangle as the canvas background
      cv.set_fill_color(1, 1, 1)
      cv.fill_rect(0, 0, w, h)
      
      # draws an image onto the canvas
      cv.draw_image("image.png", 0, 0, img_w, img_h)
      
      cv.end_updates()
      

      Whenever is use the canvas.draw_image() function, my Pythonista crashes. This seems to be not because of the image that I‘m trying to draw, since I‘ve tried out other images and even an empty string as the 1. argument too. It didn‘t even give me an error, it just closed the program.

      Am I just not using the function correctly or is there something else I need to add?

      I would be really happy if someone could help me out.

      cvp 2 Replies Last reply Reply Quote 0
      • ccc
        ccc last edited by

        https://omz-software.com/pythonista/docs/ios/canvas.html#canvas.draw_image
        You need a "named image" not an image file name.
        The first two functions of https://forum.omz-software.com/topic/2332/can-t-subclass-scene-rect-in-1-6-beta/3 might be useful.

        cvp Trüff 2 Replies Last reply Reply Quote 0
        • cvp
          cvp @ccc last edited by cvp

          @ccc said

          You need a "named image"

          What is that? For me, in Ui.Image.named(name), name is a file name

          And the crash does not have any explanation in faultlog, only "abort"????

          1 Reply Last reply Reply Quote 0
          • cvp
            cvp @Trüff last edited by cvp

            @Trüff perhaps a canvas bug, try this (with, of course, your own file name)

            img = ui.Image.named('kitty.JPG')
            clipboard.set_image(img)
            #cv.draw_image("kitty.JPG", 0, 0, img_w, img_h)
            cv.draw_clipboard(0, 0, img_w, img_h)
            
            1 Reply Last reply Reply Quote 0
            • ccc
              ccc last edited by ccc

              https://omz-software.com/pythonista/docs/ios/ui.html#ui.Image.named
              --> cv.draw_image(ui.Image.named("image.png"), 0, 0, img_w, img_h)

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

                @ccc that does not work, draw_image wants a string as first argument

                cv.draw_image(ui.Image.named("kitty.JPG"), 0, 0, img_w, img_h)
                TypeError: argument 1 must be str, not _ui.Image
                
                1 Reply Last reply Reply Quote 1
                • cvp
                  cvp @Trüff last edited by

                  @Trüff could you tell me if this solves your problem, thanks

                  img = ui.Image.named('image.png')
                  clipboard.set_image(img)
                  cv.draw_clipboard(0, 0, img_w, img_h)
                  
                  Trüff 1 Reply Last reply Reply Quote 0
                  • Trüff
                    Trüff @cvp last edited by Trüff

                    @cvp Hm… Well, it does draw the image onto the canvas. But the image I want to draw is transparent and apparently it just sets a white background for images that are saved into the clipboard. So when I draw the clipboard with the image inside of it, the picture is not transparent anymore.

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

                      Keep the alpha low on the background color like:

                      • https://forum.omz-software.com/topic/3926/transparent-scene-background-using-objc_utils
                      cvp 1 Reply Last reply Reply Quote 0
                      • Trüff
                        Trüff @ccc last edited by

                        @ccc Ok thank you. I‘m going to read the documentary on these modules first.

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

                          @ccc said

                          Keep the alpha low on the background color

                          If the image he wants to draw is transparent, its background color has already a low alpha.

                          1 Reply Last reply Reply Quote 0
                          • cvp
                            cvp @Trüff last edited by

                            @Trüff I don't know if you really need a canvas, else, please try this

                            import ui
                            v = ui.View()
                            v.background_color = 'yellow'
                            iv = ui.ImageView()
                            iv.background_color = 'red'
                            iv.image = ui.Image.named('image.png')
                            wi,hi = iv.image.size
                            iv.frame = (10,10,150,150*hi/wi)
                            v.add_subview(iv)
                            v.present('fullscreen')
                            

                            Trüff 1 Reply Last reply Reply Quote 0
                            • Trüff
                              Trüff @cvp last edited by

                              @cvp This also doesn‘t work with transparent pictures for me. However, I tried out your previous idea of saving the image into the clipboard and drawing the clipboard but I used the class Image from the PIL module instead of the one from Pythonista‘s ui module and that worked. Thank you!

                              cvp 2 Replies Last reply Reply Quote 0
                              • cvp
                                cvp @Trüff last edited by

                                @Trüff saiD

                                This also doesn‘t work with transparent pictures for me

                                Weird, for me it works, the image of my example has a transparent background, here in red

                                1 Reply Last reply Reply Quote 0
                                • cvp
                                  cvp @Trüff last edited by

                                  @Trüff said

                                  but I used the class Image from the PIL module instead of the one from Pythonista‘s ui module and that worked

                                  👍

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