Forum Archive

Rotate image in ui.ImageView

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

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

JonB

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

cvp

@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')

cvp

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
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
daveM

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

I’ll try those now. Thanks guys