annotate tools/image_do.py @ 1:4f6d8ffe0a60

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