comparison tools/image_do.py @ 0:64374d852e36

Uploaded
author tomasz-bednarz
date Mon, 25 Nov 2013 21:32:12 -0500
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:64374d852e36
1 import os
2 import optparse
3
4 from PIL import Image
5 from PIL import ImageDraw
6 from image_transforms import tileify
7 from image_transforms import calculate_random_location
8 from image_transforms import filmify_image
9
10
11 def main():
12
13 # Parse options
14 parser = optparse.OptionParser()
15 parser.add_option(
16 "-i",
17 dest="inputImage",
18 help="Input Image",
19 metavar="FILE"
20 )
21 parser.add_option(
22 "-o",
23 dest="outputImage",
24 help="Output Image",
25 metavar="FILE"
26 )
27 parser.add_option(
28 "--command",
29 "-c",
30 dest="imageCommand",
31 help="Command",
32 metavar="string"
33 )
34 parser.add_option(
35 "-s",
36 dest='custom_string',
37 help='String to be placed as decription'
38 )
39 parser.add_option(
40 "-e",
41 dest='image_ext',
42 help='Image extension'
43 )
44 (opts, args) = parser.parse_args()
45
46 mandatory_options = ['inputImage', 'outputImage', 'image_ext']
47 for option in mandatory_options:
48 if not opts.__dict__[option]:
49 print "One or more options are missing\n"
50 parser.print_help()
51 exit(-1)
52
53 inputImage_fn = opts.inputImage
54 outputImage_fn =opts.outputImage
55 input_extension = opts.image_ext
56 if input_extension == "jpg":
57 input_extension="jpeg"
58
59 listOfValues = opts.imageCommand.split()
60
61 try:
62 imageIn = Image.open(inputImage_fn)
63 except IOError:
64 print "Cannot open file: " + inputImage_fn
65
66 if listOfValues[0] == "filmify":
67 """
68 Transform image into a signed movie frame.
69 Use:
70 python image_do.py -i input.jpg -o output.jpg -c "filmify" -s "hello world" -e "jpg"
71 """
72 print "command: " + listOfValues[0]
73 new_image_data = filmify_image(imageIn)
74 add_text_header(new_image_data, text=opts.custom_string)
75 new_image_data.save(outputImage_fn, input_extension);
76 # new_image_data.show();
77 elif listOfValues[0] == "tileify":
78 """
79 Breaks image up into rectangles and shifts them a random distance.
80 Parameters: number of tiles, max shift
81 Use:
82 python image_do.py -i input.jpg -o output.jpg -c "tileify num_tiles max_shift" -e "jpg"
83 """
84 print "command: " + listOfValues[0]
85 num_tiles = int(listOfValues[1])
86 max_shift = int(listOfValues[2])
87 new_image_data = tileify(imageIn, num_tiles, max_shift)
88 new_image_data.save(outputImage_fn, input_extension);
89 # new_image_data.show();
90 elif listOfValues[0] == "rotate":
91 """
92 Rotates an image the given number of degrees counter clockwise around its centre.
93 Parameters: deg, 1 - expand the image or 0 - leave same size, filter
94 Use:
95 python image_do.py -i input.jpg -o output.jpg -c "rotate 45 1" -e "jpg"
96 """
97 print "command: " + listOfValues[0]
98 new_image_data = imageIn.rotate(int(listOfValues[1]), expand=int(listOfValues[2]))
99 new_image_data.save(outputImage_fn, input_extension);
100 # new_image_data.show(new_image_data)
101 elif listOfValues[0] == "resize":
102 """
103 Resizes the image.
104 Parameters: width, height, filter: 0 = NEAREST, 1 = ANTIALIAS, 2 = BILINEAR, 3 = BICUBIC
105 Use:
106 python image_do.py -i sydney.jpg -o o.jpg -c "resize 140 140 0"
107 """
108 print "command: " + listOfValues[0]
109 size = int(listOfValues[1]), int(listOfValues[2])
110 filterr = int(listOfValues[3])
111 new_image_data = imageIn.resize(size, filterr)
112 new_image_data.save(outputImage_fn, input_extension);
113 # new_image_data.show()
114
115 elif listOfValues[0] == "crop":
116 # bbox
117 # returns a copy of a rectangular region from the current image. The box is a 4-tuple defining the left, upper, right, and lower pixel coordinate.
118 # example: python image_do.py -i sydney.jpg -o sydney_out.jpg -c "crop 0 0 10 10"
119 print "command: " + listOfValues[0]
120 new_image_data = imageIn.crop((int(listOfValues[1]), int(listOfValues[2]), int(listOfValues[3]), int(listOfValues[4])))
121 new_image_data.save(outputImage_fn);
122 # new_image_data.show(new_image_data)
123
124
125 def add_text_header(image_data, text='cloudimaging.net.au'):
126 """
127 Add text to the top of the document
128 Inputs
129 image_data - image to have text added
130 text - string to add
131 """
132 # TODO add string wrapping for long strings
133 # TODO figure this out programatically
134 text_location = (300, 1)
135 # This is white
136 fill_color = 255
137 # This happens if -s wasn't set
138 if text == None:
139 text = 'cloudimaging.net.au'
140 draw = ImageDraw.Draw(image_data)
141 draw.text(text_location, text, fill=fill_color)
142
143
144 if __name__ == "__main__":
145 main()
146