Created
March 22, 2014 16:09
-
-
Save sirpercival/9709563 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
from kivy.uix.scatter import Scatter | |
from kivy.uix.boxlayout import BoxLayout | |
from kivy.uix.stencilview import StencilView | |
from kivy.uix.slider import Slider | |
from kivy.graphics.texture import Texture | |
from kivy.lang import Builder | |
from kivy.uix.widget import Widget | |
from kivy.properties import ObjectProperty, NumericProperty | |
from numpy import nanmin, nanmax | |
Builder.load_string(''' | |
#:kivy 1.8.0 | |
<ImagePane>: | |
orientation: 'vertical' | |
StencilView: | |
size_hint: 1., 0.7 | |
BoxLayout: | |
size: root.size | |
pos: root.pos | |
padding: 20 | |
orientation: 'horizontal' | |
ScatterLayout: | |
id: the_scatter | |
do_rotation: False | |
do_translation: self.scale != 1 | |
Widget: | |
id: the_image | |
canvas: | |
Rectangle: | |
size: self.size | |
pos: self.pos | |
texture: root.iregion | |
GridLayout: | |
size_hint: 1., 0.3 | |
cols: 2 | |
Label: | |
text: 'Stretch mode:' | |
size_hint_y: | |
Label: | |
text: 'Stretch factor:' | |
Spinner: | |
id: smode | |
text: 'linear' | |
values: 'linear', 'logarithmic', 'gamma', 'arcsinh', 'square root', 'histogram equalization' | |
on_text: root.update_slider() | |
Slider: | |
id: sfactor | |
value_normalized: 0.5 | |
disabled: True | |
BoxLayout: | |
orientation: 'horizontal' | |
Button: | |
text: 'Reset Zoom' | |
on_press: root.reset_zoom() | |
Button: | |
text: 'Submit' | |
on_press: root.submit_changes() | |
BoxLayout: | |
orientation: 'horizontal' | |
Label: | |
text: 'Min' | |
TextInput: | |
text: '%f6'%root.tmin | |
multiline: False | |
Label: | |
text: 'Max' | |
TextInput: | |
text: '%f6'%root.tmax | |
multiline: False | |
''') | |
class ImagePane(BoxLayout): | |
data = ObjectProperty(None) | |
itexture = ObjectProperty(Texture.create(size = (2048, 2048))) | |
iregion = ObjectProperty(None) | |
tmin = NumericProperty(0.) | |
tmax = NumericProperty(0.) | |
def load_data(self, dataobj): | |
self.data = dataobj | |
print self.data.threshold | |
self.tmin = float(self.data.threshold[0]) | |
self.tmax = float(self.data.threshold[1]) | |
self.iregion = self.itexture.get_region(0, 0, self.data.dimensions[0], \ | |
self.data.dimensions[1]) | |
aspect = float(self.data.dimensions[0]) / float(self.data.dimensions[1]) | |
self.ids.the_image.size_hint = (1./aspect, 1.) if aspect > 1 else (1., aspect) | |
self.update_display() | |
def update_display(self): | |
idata = ''.join(map(chr,self.data.scaled)) | |
self.itexture.blit_buffer(idata, colorfmt='luminance', bufferfmt='ubyte', \ | |
size = self.data.dimensions) | |
self.itexture.ask_update(None) | |
def update_slider(self): | |
mode = self.ids.smode.text | |
factor = self.ids.sfactor | |
if mode in ['linear', 'histogram equalization', 'square root']: | |
factor.disabled = True | |
elif mode == 'gamma': | |
factor.disabled = False | |
factor.range = (-1, 1) | |
factor.step = 0.05 | |
factor.value = 0 | |
elif mode == 'logarithmic': | |
factor.disabled = False | |
factor.range = (1, 10) | |
factor.step = 0.25 | |
elif mode == 'arcsinh': | |
factor.disabled = False | |
factor.range = (1, 6) | |
factor.step = 0.1 | |
def reset_zoom(self): | |
scatr = self.ids.the_scatter | |
scatr.pos = 0, 0 | |
scatr.scale = 1 | |
def submit_changes(self): | |
factor = self.ids.sfactor.value | |
if self.ids.smode.text == 'gamma': | |
factor = 10.**factor | |
info = {'min':self.tmin, 'max':self.tmax, \ | |
'mode':self.ids.smode.text, 'factor':factor} | |
self.data.change_parameters(info) | |
self.update_display() | |
if __name__ == '__main__': | |
from kivy.base import runTouchApp | |
from fitsimage import ScalableImage | |
from numpy import array | |
tmp = array([ x / 8 + 1 for x in range(1024)]) | |
data = tmp + tmp.reshape(-1,1) | |
im = ScalableImage(data, scalemode='gamma', factor=0.3) | |
wid = ImagePane() | |
wid.load_data(im) | |
runTouchApp(wid) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment