Created
October 24, 2020 12:22
-
-
Save tshirtman/8638d2095da025d07d3e0d48c91af921 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'''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