Skip to content

Commit ff9a108

Browse files
author
yxdragon
committed
rename event to action
1 parent d07d5b6 commit ff9a108

27 files changed

+850
-88
lines changed

sciwx.docx

Whitespace-only changes.

sciwx/action/__init__.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
from .action import SciAction
2+
from .imgact import ImgAction
3+
from .tolact import Tool, DefaultTool

sciwx/action/action.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
class SciAction:
2+
name = 'SciAction'
3+
def __init__(self): pass
4+
def start(self, app):
5+
self.app = app
6+
print(self.name, 'started!')

sciwx/event/imevt.py renamed to sciwx/action/imgact.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
1-
from .event import SciEvent
1+
from .action import SciAction
22
from ..widgets import ParaDialog
33

4-
class ImgEvent(SciEvent):
5-
title = 'Image Event'
4+
class ImgAction(SciAction):
5+
title = 'Image Action'
66
note, para, view = [], None, None
77

8-
def __init__(self, app):
9-
self.app = app
10-
self.ips = app.get_img()
8+
def __init__(self): pass
119

1210
def show(self):
1311
dialog = ParaDialog(self.app.get_img_win(), self.title)
@@ -27,12 +25,14 @@ def cancel(self, ips):
2725
def run(self, ips, img, snap, para):
2826
print('I am running!!!')
2927

30-
def start(self, para=None, callback=None):
31-
print('Image Event Started!')
28+
def start(self, app, para=None, callback=None):
29+
print('Image Action Started!')
30+
self.app = app
31+
self.ips = app.get_img()
3232
if 'auto_snap' in self.note: self.ips.snapshot()
3333
if para!=None:
3434
self.run(self.ips, self.ips.img, self.ips.snap, para)
35-
elif self.view==None and self.__class__.show is ImgEvent.show:
35+
elif self.view==None and self.__class__.show is ImgAction.show:
3636
self.run(self.ips, self.ips.img, self.ips.snap, para)
3737
elif self.show():
3838
self.run(self.ips, self.ips.img, self.ips.snap, self.para)

sciwx/event/tool.py renamed to sciwx/action/tolact.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,20 @@
1-
from .event import SciEvent
1+
from .action import SciAction
22

3-
class Tool(SciEvent):
3+
class Tool(SciAction):
44
title = 'Base Tool'
55
default = None
66
def mouse_down(self, image, x, y, btn, **key): pass
77
def mouse_up(self, image, x, y, btn, **key): pass
88
def mouse_move(self, image, x, y, btn, **key): pass
99
def mouse_wheel(self, image, x, y, d, **key): pass
10-
def start(self): Tool.default = self
10+
def start(self, app):
11+
self.app = app
12+
Tool.default = self
13+
if not app is None: app.tool = self
1114

1215
class DefaultTool(Tool):
1316
title = 'Move And Scale'
14-
def __init__(self, app):
17+
def __init__(self):
1518
self.oldxy = None
1619

1720
def mouse_down(self, image, x, y, btn, **key):
@@ -31,4 +34,4 @@ def mouse_wheel(self, image, x, y, d, **key):
3134
if d>0: key['canvas'].zoomout(x, y, coord='data')
3235
if d<0: key['canvas'].zoomin(x, y, coord='data')
3336

34-
DefaultTool(None).start()
37+
DefaultTool().start(None)

sciwx/app/sciapp.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import time, threading
33

44
import wx.lib.agw.aui as aui
5-
from sciwx.widgets import MenuBar, ToolBar
5+
from sciwx.widgets import MenuBar, ToolBar, ChoiceBook
66
from sciwx.canvas import CanvasNoteBook
77
from sciwx.grid import GridNoteBook
88
from sciwx.text import MDNoteFrame, TextNoteFrame
@@ -23,8 +23,9 @@ def __init__( self, parent ):
2323
self.init_tool()
2424
self.init_canvas()
2525
self.init_table()
26-
#self.load_menu(data)
26+
self.init_widgets()
2727
self.init_text()
28+
self.init_status()
2829

2930
self.Layout()
3031
self.auimgr.Update()
@@ -34,8 +35,7 @@ def __init__( self, parent ):
3435
self.Bind(wx.EVT_CLOSE, self.on_close)
3536
self.Bind(aui.EVT_AUI_PANE_CLOSE, self.on_pan_close)
3637

37-
38-
def add_status(self):
38+
def init_status(self):
3939
self.stapanel = stapanel = wx.Panel( self, wx.ID_ANY, wx.DefaultPosition, wx.DefaultSize, wx.TAB_TRAVERSAL )
4040
sizersta = wx.BoxSizer( wx.HORIZONTAL )
4141
self.txt_info = wx.StaticText( stapanel, wx.ID_ANY, "ImagePy v0.2", wx.DefaultPosition, wx.DefaultSize, 0 )
@@ -57,6 +57,9 @@ def load_tool(self, data, default=None):
5757
if not default is None: self.toolbar.add_pop('P', default)
5858
self.toolbar.Layout()
5959

60+
def load_widget(self, data):
61+
self.widgets.load(data)
62+
6063
def init_menu(self):
6164
self.menubar = MenuBar(self)
6265

@@ -94,6 +97,11 @@ def init_table(self):
9497
.MaximizeButton( True ).Resizable().FloatingSize((800, 600)).BestSize(( 120,120 )). Caption('Tables') .
9598
BottomDockable( True ).TopDockable( False ).LeftDockable( True ).RightDockable( True ) )
9699

100+
def init_widgets(self):
101+
self.widgets = ChoiceBook(self)
102+
self.auimgr.AddPane( self.widgets, aui.AuiPaneInfo() .Right().Caption('Widgets') .PinButton( True )
103+
.Dock().Resizable().FloatingSize( wx.DefaultSize ).MinSize( wx.Size( 266,-1 ) ).Layer( 10 ) )
104+
97105
def init_text(self):
98106
self.mdframe = MDNoteFrame(self, 'Sci Document')
99107
self.txtframe = TextNoteFrame(self, 'Sci Text')

sciwx/canvas/__init__.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
from .canvas import Canvas
22
from .mcanvas import MCanvas
33
from .widget import CanvasFrame, CanvasNoteBook, CanvasNoteFrame
4-
from ..event import Tool, DefaultTool
54
from .image import Image

sciwx/canvas/canvas.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from .imutil import mix_img
44
from .mark import drawmark
55
from .image import Image
6-
from ..event import Tool, DefaultTool
6+
from ..action import Tool, DefaultTool
77
from time import time
88

99
class Canvas (wx.Panel):
@@ -207,6 +207,7 @@ def draw_image(self, dc, img, back, mode):
207207
mix_img(img, m, o, shp, self.outimg,
208208
self.outrgb, self.outint, self.image.rg, self.image.lut,
209209
self.image.log, cns=self.image.cn, mode=self.image.mode)
210+
210211
self.outbmp.CopyFromBuffer(memoryview(self.outrgb))
211212
dc.DrawBitmap(self.outbmp, *csbox[:2])
212213

sciwx/canvas/image.py

Lines changed: 72 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,47 @@
11
import numpy as np
22

33
default_lut = np.arange(256*3, dtype=np.uint8).reshape((3,-1)).T
4-
4+
5+
def get_updown(imgs, slices='all', chans='all', step=1):
6+
c = chans if isinstance(chans, int) else slice(None)
7+
if isinstance(slices, int): imgs = [imgs[slices]]
8+
if step<=1: step = int(1/step+0.5)
9+
else: step = int(min(imgs[0].shape[:2])/step+0.5)
10+
s = slice(None, None, max(step,1))
11+
s = (s,s,c)[:imgs[0].ndim]
12+
mins = [i[s].min(axis=(0,1)) for i in imgs]
13+
maxs = [i[s].max(axis=(0,1)) for i in imgs]
14+
mins = np.array(mins).reshape((len(mins),-1))
15+
maxs = np.array(maxs).reshape((len(maxs),-1))
16+
mins, maxs = mins.min(axis=0), maxs.max(axis=0)
17+
if np.iscomplexobj(mins):
18+
mins, maxs = np.zeros(mins.shape), np.abs(maxs)
19+
if chans!='all': return mins.min(), maxs.max()
20+
return [(i,j) for i,j in zip(mins, maxs)]
21+
22+
def lookup(img, cn, rgs, lut):
23+
if isinstance(cn, int): cn = [cn]
24+
img = img.reshape(img.shape[:2]+(-1,))
25+
buf = np.zeros(img.shape[:2]+(len(cn),), dtype=np.uint8)
26+
for i in range(len(cn)):
27+
rg = rgs[cn[i]]
28+
k = 255.0/(max(1e-10, rg[1]-rg[0]))
29+
bf = np.clip(img[:,:,cn[i]], rg[0], rg[1])
30+
np.subtract(bf, rg[0], out=bf, casting='unsafe')
31+
np.multiply(bf, k, out=buf[:,:,i], casting='unsafe')
32+
return buf if len(cn)==3 else lut[buf.reshape(img.shape[:2])]
33+
34+
def histogram(imgs, rg=(0,256), slices='all', chans='all', step=1):
35+
c = chans if isinstance(chans, int) else slice(None)
36+
if isinstance(slices, int): imgs = [imgs[slices]]
37+
if step<=1: step = int(1/step+0.5)
38+
else: step = int(min(imgs[0].shape[:2])/step+0.5)
39+
s = slice(None, None, max(step,1))
40+
s = (s,s,c)[:imgs[0].ndim]
41+
rg = np.linspace(rg[0], rg[1], 257)
42+
hist = [np.histogram(i[s], rg)[0] for i in imgs]
43+
return np.sum(hist, axis=0)
44+
545
class Image:
646
def __init__(self, img=None):
747
self.name = 'Image'
@@ -25,6 +65,10 @@ def img(self): return self.imgs[self.cur]
2565
def img(self, value):
2666
self.imgs[self.cur] = value
2767
self.cn = [0, (0,1,2)][self.channels==3]
68+
if self.dtype == np.uint8:
69+
self.rg = [(0, 255)] * self.channels
70+
else:
71+
self.rg = self.get_updown('all', 'all', step=512)
2872

2973
@property
3074
def channels(self):
@@ -49,6 +93,15 @@ def info(self):
4993
return '%sx%s S:%s/%s C:%s/%s'%(*self.shape,
5094
self.cur+1, self.slices, self.cn, self.channels)
5195

96+
@property
97+
def range(self):
98+
rg = np.array(self.rg).reshape((-1,2))
99+
return (rg[:,0].min(), rg[:,1].max())
100+
101+
@range.setter
102+
def range(self, value):
103+
self.rg = [value] * len(self.rg)
104+
52105
def update(self): self.dirty = True
53106

54107
def snapshot(self):
@@ -60,7 +113,24 @@ def swap(self):
60113
if self.snap is None:return
61114
buf = self.img.copy()
62115
self.img[:], self.snap[:] = self.snap, buf
63-
print('swap')
116+
117+
def histogram(self, rg=None, slices=None, chans=None, step=1):
118+
if slices is None: slices = self.cur
119+
if chans is None: chans = self.cn
120+
if rg is None: rg = self.range
121+
print(rg, slices, chans, step)
122+
return histogram(self.imgs, rg, slices, chans, step)
123+
124+
def get_updown(self, slices='all', chans='one', step=1):
125+
if slices is None: slices = self.cur
126+
if chans is None: chans = self.cn
127+
return get_updown(self.imgs, slices, chans, step)
128+
129+
def lookup(self, img=None):
130+
if img is None: img = self.img
131+
a = lookup(img, self.cn, self.rg, self.lut)
132+
print(a.shape)
133+
return a
64134

65135
if __name__ == '__main__':
66136
img = Image(np.zeros((5,5)))

sciwx/canvas/mcanvas.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ def __init__(self, parent=None, autofit=False):
3636
self.Layout()
3737
self.sli_page.Bind(wx.EVT_SCROLL, self.on_scroll)
3838
self.sli_chan.Bind(wx.EVT_SCROLL, self.on_scroll)
39-
# self.Bind(wx.EVT_IDLE, self.on_idle)
39+
self.Bind(wx.EVT_IDLE, self.on_idle)
4040

4141
#self.Fit()
4242
self.set_rg = self.canvas.set_rg
@@ -91,6 +91,10 @@ def slider(self):
9191
self.sli_chan.SetMax(channels-1)
9292
self.chans,self.cn = channels, self.canvas.image.cn
9393

94+
def on_idle(self, event):
95+
if self.image.dirty: self.update()
96+
self.image.dirty = False
97+
9498
def update(self):
9599
if self.image.img is None: return
96100
self.slider()

sciwx/demo/app_demo.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@
22
sys.path.append('../../')
33
from sciwx.app import SciApp
44
from sciwx.canvas import CanvasFrame
5-
from sciwx.event import ImgEvent, Tool, DefaultTool
5+
from sciwx.action import ImgAction, Tool, DefaultTool
6+
from sciwx.plugins.curve import Curve
7+
from sciwx.plugins.channels import Channels
8+
from sciwx.plugins.histogram import Histogram
9+
from sciwx.plugins.viewport import ViewPort
610

711
from sciwx.plugins.filters import Gaussian, Undo
812
from sciwx.plugins.pencil import Pencil
@@ -13,7 +17,8 @@
1317

1418
app = wx.App(False)
1519
frame = SciApp(None)
16-
frame.Show()
20+
21+
logo = 'C:/Users/54631/Documents/projects/imagepy/imagepy/tools/Standard/magic.gif'
1722
frame.load_menu(('menu',[('File',[('Open', Open),
1823
('Save', Save)]),
1924
('Filters', [('Gaussian', Gaussian),
@@ -22,6 +27,12 @@
2227
('D', DefaultTool)]),
2328
('draw', [('X', Pencil),
2429
('X', Pencil)])]), 'draw')
30+
frame.load_widget(('widgets', [('Histogram', [('Histogram', Histogram),
31+
('Curve', Curve),
32+
('Channels', Channels)]),
33+
('Navigator', [('Viewport', ViewPort)])]))
34+
2535
frame.show_img(camera())
2636
frame.show_img(camera())
37+
frame.Show()
2738
app.MainLoop()

sciwx/event/__init__.py

Lines changed: 0 additions & 3 deletions
This file was deleted.

sciwx/event/event.py

Lines changed: 0 additions & 4 deletions
This file was deleted.

sciwx/event/pubsub.py

Lines changed: 0 additions & 37 deletions
This file was deleted.

0 commit comments

Comments
 (0)