I don’t have time to try this right now – but a few things come to mind:
If you want to rotate an image, you can look in the example directory for kivy and find an example.
To Mirror an image, I would try using Pillow. https://note.nkmk.me/en/python-pillow-flip-mirror/
And then use in memory loading to load the mirrored texture. https://kivy.org/doc/master/api-kivy.core.image.html?highlight=image#in-memory-image-loading
--
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/c6548659-63bc-4811-9828-52dd59786d8an%40googlegroups.com.
Here is a version using PIL. The file management here is good for a POC, but you would want to do something more robust for a production app.
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.image import Image
from kivy.properties import BooleanProperty
from PIL import ImageOps
from PIL import Image as PImage
from pathlib import Path
kv = """
<MirrorImage>:
keep_ratio: True
allow_stretch: True
BoxLayout:
orientation: 'vertical'
Label:
size_hint_y: None
height: 48
text: 'Mirror Image Test'
MirrorImage:
id: mi
source:'ACESxp-30230 crop.jpg' # replace with your image
ToggleButton:
size_hint_y: None
height: 48
text: 'Mirror'
on_release: mi.mirror = False if self.state == 'normal' else True
"""
class MirrorImage(Image):
mirror = BooleanProperty(False)
def on_mirror(self, *args):
if self.mirror:
if not Path('mirror_' + self.source).exists():
# for production need more robust file naming and management
# sufficient for proof of concept
im = PImage.open(self.source)
image = ImageOps.mirror(im)
image.save('mirror_' + self.source)
self.source = 'mirror_'+ self.source
else:
self.source = self.source[7:] # remove 'mirror_'
class MirrorImageApp(App):
def build(self):
return Builder.load_string(kv)
MirrorImageApp().run()
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/61b2aeba.1c69fb81.e7604.e8efSMTPIN_ADDED_MISSING%40gmr-mx.google.com.
Here is a version using Robert’s approach (Very Clever!!!), in kv. This is based on a Widget, not an Image – so it does not maintain the ratio of the image.
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.widget import Widget
from kivy.properties import BooleanProperty, ListProperty, StringProperty
kv = """
<MirrorImage>:
view_size: self.size if not self.mirror else (-self.size[0], self.size[1])
view_pos: self.pos if not self.mirror else (self.pos[0] + self.size[0], self.pos[1])
canvas:
Color:
rgb: 1, 1, 1
Rectangle:
size: self.view_size
pos: self.view_pos
source: self.source
BoxLayout:
orientation: 'vertical'
Label:
size_hint_y: None
height: 48
text: 'Mirror Image Test'
MirrorImage:
id: mi
source:'ACESxp-30230 crop.jpg' # replace with your image
ToggleButton:
size_hint_y: None
height: 48
text: 'Mirror'
on_release: mi.mirror = False if self.state == 'normal' else True
"""
class MirrorImage(Widget):
mirror = BooleanProperty(False)
view_size = ListProperty([0,0])
view_pos = ListProperty([0,0])
source = StringProperty()
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/61b371b0.1c69fb81.b4415.11aaSMTPIN_ADDED_MISSING%40gmr-mx.google.com.
The need to create a temporary file bothered me – so I modified the code to use the io module to keep the mirror image in memory.
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.image import Image
from kivy.core.image import Image as CoreImage
from kivy.properties import BooleanProperty
from PIL import ImageOps
from PIL import Image as PImage
import io
from pathlib import Path
kv = """
<MirrorImage>:
keep_ratio: True
allow_stretch: True
BoxLayout:
orientation: 'vertical'
Label:
size_hint_y: None
height: 48
text: 'Mirror Image Test'
MirrorImage:
id: mi
source:'ACESxp-30230 crop.jpg' # replace with your image
ToggleButton:
size_hint_y: None
height: 48
text: 'Mirror'
on_release: mi.mirror = False if self.state == 'normal' else True
"""
class MirrorImage(Image):
mirror = BooleanProperty(False
)
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.mirror_image = None
self.previous_filename = None
self.extension = {'.jpg': {'format': 'jpeg', 'ext': 'jpg'},
'.png': {'format': 'png', 'ext': 'png'}}
# expand with required formats, see: https://pillow.readthedocs.io/en/stable/handbook/image-file-formats.html
def on_source(self, *args):
if not self.source or self.source == self.previous_filename: # if source is '' or same as previous
return
self.previous_filename = self.source # else create texture
with PImage.open(self.source) as im:
mirror_image = ImageOps.mirror(im)
output = io.BytesIO()
ext = Path(self.source).suffix
mirror_image.save(output, format=self.extension[ext]['format'])
output.seek(0)
self.mirror_image = CoreImage(output, ext=self.extension[ext]['ext'])
def on_mirror(self, *args):
if self.mirror:
self.texture = self.mirror_image.texture
else:
self.reload() # sets source to '' then back to name to fire source event.
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/3df563c7-5dbe-4cef-b348-896051b73e09n%40googlegroups.com.