added readme and setup script
[BS_AY250.git] / week3 / problem1 / server.py
blobf419fad5e9a19d34cdfdd96b3876e71016ba59c3
2 import SimpleXMLRPCServer
4 import numpy as np
5 from scipy import signal
7 import image
9 image.outdir = 'server_data'
11 class ImageManipulation(object):
12 def __init__(self):
13 self.log_index = 0
15 def __server_method(self,imagestr,fxchan):
16 ''' Generic server method for processing an image, marshalling, saving data products '''
17 x = image.unmarshal(imagestr)
18 image.save_image('raw%d.jpg'%(self.log_index),x)
20 y = image.process_image(x,fxchan)
22 image.save_image('final%d.jpg'%(self.log_index),y)
24 self.log_index += 1
26 return image.marshal(y)
28 def double(self,imagestr):
29 ''' Double size of an image by assuming image is band limited '''
30 def fxchan(x):
31 x = signal.resample(x,x.shape[0]*2,axis=0,window='boxcar')
32 x = signal.resample(x,x.shape[1]*2,axis=1,window='boxcar')
33 return x
35 return self.__server_method(imagestr,fxchan)
37 def rot180(self,imagestr):
38 ''' Rotate image by 180 degrees '''
39 def fxchan(x):
40 return x[::-1]
42 return self.__server_method(imagestr,fxchan)
44 def invert_color(self,imagestr):
45 ''' Flip intensities '''
46 def fxchan(x):
47 return 255 - x
49 return self.__server_method(imagestr,fxchan)
51 host,port = "localhost",5020
53 server = SimpleXMLRPCServer.SimpleXMLRPCServer((host,port),allow_none=True)
54 server.register_instance(ImageManipulation())
55 server.register_multicall_functions()
56 server.register_introspection_functions()
58 print "Server running on ",host,port
59 server.serve_forever()