Just taking a quick look at your code, you are missing a few calculations.
Just like you are swapping height and width, you need to swap center_x and center_y. (self.center[0]. self.center[1])
You may also need to adjust the pos.
I suspect you want your RotateImage to be a Widget, not a FloatLayout.
I’d suggest deriving the RotatedImage from an Image widget and rotating the canvas.
--
You received this message because you are subscribed to the Google Groups "Kivy users support" group.
To unsubscribe from this group and stop receiving emails from it, send an email to kivy-users+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/CAG2bR8x0z2sOeNBV-7i9v7D4NSOKr%3DBVd9iGQ%2B8s-XshSD1jyA%40mail.gmail.com.
Giving it a try myself, I think the code below does what you are looking for. Note you will need to change the name of the image file in the kv code.
import itertools
from kivy.app import App
from kivy.clock import Clock
from kivy.lang import Builder
from kivy.properties import NumericProperty, ListProperty, BooleanProperty
from kivy.uix.image import Image
kv = """
<RotatedImage>:
keep_ratio: True
allow_stretch: True
transposed: (root.angle == 90) or (root.angle == 270)
canvas.before:
PushMatrix
Rotate:
angle: root.angle
axis: 0, 0, 1
origin: root.center
canvas.after:
PopMatrix
BoxLayout:
Label:
text: 'Image Rotate Test'
RotatedImage:
id: ri
source: 'ACESxp-30230 crop.jpg' # CHANGE TO YOUR IMAGE FILE
Label:
text: 'Image Rotate Test'
"""
class RotatedImage(Image):
angle = NumericProperty(0.0)
transposed = BooleanProperty(False)
def set_image(self, path, angle=0.0):
self.source = path
self.angle = angle
class KvTestApp(App):
angles = itertools.cycle([0, 90, 270])
def build(self):
return Builder.load_string(kv)
def on_start(self, *args):
Clock.schedule_interval(self.rotate, 2)
def rotate(self, *args):
self.root.ids.ri.angle = next(self.angles)
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/61ace8d9.1c69fb81.6baa6.8842SMTPIN_ADDED_MISSING%40gmr-mx.google.com.
A small edit to make things more ‘pythonic’
transposed: root.angle in (90, 270)
From: Elliot Garbus
Sent: Sunday, December 5, 2021 10:22 AM
To: kivy-...@googlegroups.com
Subject: RE: [kivy-users] Custom image class to transparently rotate animage
Giving it a try myself, I think the code below does what you are looking for. Note you will need to change the name of the image file in the kv code.
import itertools
from kivy.app import App
from kivy.clock import Clock
from kivy.lang import Builder
from kivy.properties import NumericProperty, ListProperty, BooleanProperty
from kivy.uix.image import Image
kv =
"""
<RotatedImage>:
keep_ratio: True
allow_stretch: True
transposed: (root.angle == 90) or (root.angle == 270)
canvas.before:
PushMatrix
Rotate:
angle: root.angle
axis: 0, 0, 1
origin: root.center
canvas.after:
PopMatrix
BoxLayout:
Label:
text: 'Image Rotate Test'
RotatedImage:
id: ri
source: 'ACESxp-30230 crop.jpg' # CHANGE TO YOUR IMAGE FILE
Label:
text: 'Image Rotate Test'
"""
class RotatedImage(Image):
angle = NumericProperty(0.0)
transposed = BooleanProperty(False)
def set_image(self, path, angle=0.0):
self.source = path
self.angle = angle
class KvTestApp(App):
angles = itertools.cycle([0, 90, 270])
def build(self):
return Builder.load_string(kv)
def on_start(self, *args):
Clock.schedule_interval(self.rotate, 2)
def rotate(self, *args):
self.root.ids.ri.angle = next(self.angles)
if __name__ == '__main__':
app = KvTestApp()
app.run()
Sent: Sunday, December 5, 2021 9:29 AM
To: kivy-...@googlegroups.com
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/61ace8d9.1c69fb81.6baa6.8842SMTPIN_ADDED_MISSING%40gmr-mx.google.com.
I don’t understand what you are trying to achieve. Happy to help if you can help me understand.
You can of course set allow_stretch: False
The Image property norm_image_size provides the size of the scaled image.
The image property texture size provides the size of the original image.
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/6f1916f3-df45-49fa-bbe4-53bc3f073027n%40googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/61ace8d9.1c69fb81.6baa6.8842SMTPIN_ADDED_MISSING%40gmr-mx.google.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/61aeb410.1c69fb81.c12fd.4e41SMTPIN_ADDED_MISSING%40gmr-mx.google.com.