final work on server/client/image
[BS_AY250.git] / week3 / problem1 / client.py
blobc3c14e914911d270e19346868246c8df49f595ae
2 import xmlrpclib
3 import sys
5 import numpy as np
6 from scipy import signal
8 import image
10 image.outdir = 'client_data'
12 imgfn = 'halfdome_from_northdome.jpg'
14 host,port = "localhost",5020
16 server = xmlrpclib.ServerProxy("http://%s:%d"%(host,port))
17 available_methods = server.system.listMethods()
19 print available_methods
21 print server.system.methodHelp('double')
22 print server.system.methodHelp('rot180')
23 print server.system.methodHelp('invert_color')
25 def wrap_server(f,img):
26 ''' Run server method f on img, marshalling and unmarshalling the img '''
27 return image.unmarshal(f(image.marshal(img)))
29 ''' Methods to undo server methods '''
30 def halve_image(img):
31 ''' Inverse of double '''
32 def fxchan(x):
33 x = signal.resample(x,x.shape[0]/2,axis=0,window='boxcar')
34 x = signal.resample(x,x.shape[1]/2,axis=1,window='boxcar')
35 return x
37 return image.process_image(img,fxchan)
39 def rot180(img):
40 ''' Inverse of 180 degree rotation '''
41 def fxchan(x):
42 return x[::-1]
44 return image.process_image(img,fxchan)
46 def invert_color(img):
47 ''' Inverse of flipping intensities '''
48 def fxchan(x):
49 return 255 - x
51 return image.process_image(img,fxchan)
54 index = 0
55 img = image.load_image(imgfn)
57 def runtest(srv,cli):
58 global index
59 image.save_image('raw%d.jpg'%index,img)
61 img_mod = wrap_server(srv,img)
63 image.save_image('mod%d.jpg'%index,img_mod)
65 img_res = cli(img_mod)
67 image.save_image('res%d.jpg'%index,img_res)
69 index += 1
71 def runtest_client(cli):
72 global index
73 image.save_image('raw%d.jpg'%index,img)
75 img_res = cli(img)
77 image.save_image('res%d.jpg'%index,img_res)
78 index += 1
80 runtest(server.double,halve_image)
81 runtest(server.rot180,rot180)
82 runtest(server.invert_color,invert_color)