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.


    Rotate image in ui.ImageView

    Pythonista
    3
    7
    3804
    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.
    • daveM
      daveM last edited by daveM

      Is it possible to rotate an image in an ImageView view? In the docs it appears there is a rotate attached to Image, but I cannot get the preloaded arrow to react to any amount of coercion.

      ImageView.image.rotate 
      

      doesn’t exist, as doesn’t,

      ImageView.rotate. 
      

      Should I just be using an Image not attached to a view? (That definitely doesn’t sound right).

      Any pointers would be appreciated.

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

        @daveM rotate is a method of PIL Image, not of ui.Image

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

          You could also set the imageview transform to ui.Transform.rotation(90), depending on what you are trying to do.

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

            @daveM and also this

            import ui
            from objc_util import *
            from math import cos,sin,pi
            
            class MyClass(ui.View):
            	def __init__(self, *args, **kwargs):
            				super().__init__(*args, **kwargs)
            				b = ui.ImageView()
            				b.frame = (100,100,200,200)
            				ui_image = ui.Image.named('test:Peppers')	
            				b.content_mode = ui.CONTENT_SCALE_ASPECT_FILL
            				b.image = ui_image
            				o = ObjCInstance(b)
            				a = -pi/6
            				rot = CGAffineTransform(cos(a),-sin(a),sin(a),cos(a),0,0)
            				o.transform = rot
            
            				self.add_subview(b)
            
            if __name__ == '__main__':
                w, h = 400,400
                f = (0, 0, w, h)
                mc = MyClass(frame=f, bg_color='white')
                mc.present('sheet')
            

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

              As usual, @JonB is right 😀

              				a = -pi/6
              				b.transform = ui.Transform.rotation(a)
              

              replaces

              				o = ObjCInstance(b)
              				a = -pi/6
              				rot = CGAffineTransform(cos(a),-sin(a),sin(a),cos(a),0,0)
              				o.transform = rot
              
              1 Reply Last reply Reply Quote 0
              • cvp
                cvp last edited by cvp

                With PIL

                import io
                import ui
                from PIL import Image
                .
                .
                .
                .
                				b = ui.ImageView()
                				b.frame = (100,100,200,200)
                				b.border_width = 1
                				pil_image = Image.open('test:Peppers')
                				pil_image = pil_image.rotate(30,expand=True)
                				def pil2ui(imgIn):
                					with io.BytesIO() as bIO:
                						imgIn.save(bIO, 'PNG')
                						imgOut = ui.Image.from_data(bIO.getvalue())
                					del bIO
                					return imgOut
                				ui_image = pil2ui(pil_image)
                				b.content_mode = ui.CONTENT_SCALE_ASPECT_FILL
                				b.image = ui_image
                
                1 Reply Last reply Reply Quote 0
                • daveM
                  daveM last edited by

                  I’m looking at a compass style object using the attitude sensor.

                  I’ll try those now. Thanks guys

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