4 Image search and manipulation GUI using Traits
5 Author: Bryan Steinbach
13 from enthought
.traits
.api
import HasTraits
,Str
,Float
,Button
,Instance
14 from enthought
.traits
.ui
.api
import View
,Item
,ButtonEditor
,VSplit
,HSplit
15 from yahoo
.search
.image
import ImageSearch
17 from mpl_figure_editor
import MPLFigureEditor
18 from matplotlib
.figure
import Figure
21 def yahoosearch(query
):
22 ''' Yahoo searches for an image and returns a URL, or an empty string if something goes wrong '''
24 search
= ImageSearch(app_id
="junk",query
=query
)
26 for res
in search
.parse_results():
29 print "Image searched failed!"
35 ''' Fetch file from link and return path to local file '''
36 return urllib
.urlretrieve(url
)[0]
38 yuvmat
= np
.array([[0.299,0.587,0.114],[-0.14713,-0.28886,0.436],[0.615,-0.51499,-0.10001]]) # source: wikipedia YUV article
39 yuvmatinv
= np
.linalg
.inv(yuvmat
)
42 class ImgSearch(HasTraits
):
44 query
= Str("Enter search")
50 rotate_color
= Button()
54 query
= "red star winter orbit"
56 figure
= Instance(Figure
,())
58 # Controller - button callbacks
59 def _refresh_fired(self
):
60 ''' Reload raw image from disk '''
64 def _rotate90_fired(self
):
65 ''' Rotate image 90 degrees clockwise '''
66 self
.ima
= pylab
.rot90(self
.ima
)
69 def _rgb_to_yuv_fired(self
):
70 ''' Show YUV as RGB '''
71 self
.color_transform(yuvmat
)
74 def _yuv_to_rgb_fired(self
):
75 ''' Show RGB as YUV '''
76 self
.color_transform(yuvmatinv
)
79 def _rotate_color_fired(self
):
80 ''' Advance color space RGB -> BRG '''
81 x
= np
.zeros_like(self
.ima
)
82 r
,g
,b
= self
.ima
[:,:,0],self
.ima
[:,:,1],self
.ima
[:,:,2]
90 def _run_query_fired(self
):
91 ''' Search for, download and display image from query text'''
96 # Controller helper methods
98 ''' Redraw image on canvas '''
99 self
.figure
.axes
[0].images
=[]
100 self
.figure
.axes
[0].imshow(self
.ima
,aspect
='auto',interpolation
='nearest')
101 wx
.CallAfter(self
.figure
.canvas
.draw
)
103 def load_image(self
):
104 ''' Load image from disk '''
105 im
= Image
.open(self
.localfile
)
106 self
.ima
= np
.asarray(im
)/256.0
107 print "ima.shape: ",self
.ima
.shape
110 def fetch_image(self
):
111 ''' Search for and download image '''
112 print "query: ",self
.query
113 self
.url
= yahoosearch(self
.query
)
114 print "url: ",self
.url
115 self
.localfile
= fetchlink(self
.url
)
116 print "localfile: ",self
.localfile
118 def color_transform(self
,mat
):
119 ''' Linear colorspace transform '''
120 x
= np
.zeros_like(self
.ima
)
121 for i
in range(self
.ima
.shape
[0]):
122 for j
in range(self
.ima
.shape
[1]):
123 x
[i
,j
,:] = np
.dot(mat
,self
.ima
[i
,j
,:])
128 qi
= Item('query',label
='Input',width
=250)
129 rqi
= Item('run_query',show_label
=False)
130 figi
= Item('figure',editor
=MPLFigureEditor(),show_label
=False)
131 urli
= Item('url',label
='Image URL')
133 refi
= Item('refresh',show_label
=False)
134 rot90i
= Item('rotate90',show_label
=False)
135 rgbyuvi
= Item('rgb_to_yuv',show_label
=False)
136 yuvrgbi
= Item('yuv_to_rgb',show_label
=False)
137 rci
= Item('rotate_color',show_label
=False)
139 view
= View(HSplit(qi
,rqi
),urli
,figi
,HSplit(refi
,rot90i
,rci
,rgbyuvi
,yuvrgbi
),
140 width
=400,height
=300,resizable
=True)
143 super(ImgSearch
,self
).__init
__()
144 self
.figure
.add_subplot(111)