Skip to content

Instantly share code, notes, and snippets.

@tshirtman
Created October 24, 2020 12:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tshirtman/8638d2095da025d07d3e0d48c91af921 to your computer and use it in GitHub Desktop.
Save tshirtman/8638d2095da025d07d3e0d48c91af921 to your computer and use it in GitHub Desktop.
'''Demonstrate showing a cropped image using get_region
Drop any image supported by kivy on the window and play with the sliders
'''
from kivy.app import App
from kivy.lang import Builder
from kivy.core.window import Window
from kivy import properties as P
KV = '''
BoxLayout:
orientation: 'vertical'
Label:
text: 'drop an image file on the window and play with the sliders'
size_hint_y: None
height: self.texture_size[1]
font_size: dp(25)
text_size: self.width, None
BoxLayout:
Image:
id: full_image
source: app.source
Image:
crop_rect: crop_left.value, crop_bottom.value, crop_width.value, crop_height.value
crop_rect:
texture:
full_image.texture.get_region(
(full_image.texture_size[0] * crop_left.value),
(full_image.texture_size[1] * crop_bottom.value),
(full_image.texture_size[0] * (crop_left.value + crop_width.value)),
(full_image.texture_size[1] * (crop_bottom.value + crop_height.value)),
) if full_image.texture else None
GridLayout:
cols: 2
Label:
text: 'crop left'
Slider:
id: crop_left
min: 0
max: 1
value: 1
Label:
text: 'crop bottom'
Slider:
id: crop_bottom
min: 0
max: 1
value: 1
Label:
text: 'crop width'
Slider:
id: crop_width
min: .0001
max: 1
value: 1
Label:
text: 'crop height'
Slider:
id: crop_height
min: .0001
max: 1
value: 1
''' # noqa
class Application(App):
source = P.StringProperty()
def build(self):
Window.bind(on_dropfile=self.update_source)
return Builder.load_string(KV)
def update_source(self, window, path):
try:
self.source = path.decode('utf8')
except Exception as exception:
print(exception)
self.source = ''
if __name__ == "__main__":
Application().run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment