1 | #!/usr/bin/python |
---|
2 | |
---|
3 | # This program is free software: you can redistribute it and/or modify |
---|
4 | # it under the terms of the GNU General Public License as published by |
---|
5 | # the Free Software Foundation, either version 3 of the License, or |
---|
6 | # (at your option) any later version. |
---|
7 | |
---|
8 | # This program is distributed in the hope that it will be useful, |
---|
9 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
10 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
11 | # GNU General Public License for more details. |
---|
12 | |
---|
13 | # You should have received a copy of the GNU General Public License |
---|
14 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
---|
15 | |
---|
16 | |
---|
17 | import PythonMagick |
---|
18 | import getopt |
---|
19 | import logging |
---|
20 | import os |
---|
21 | import sys |
---|
22 | |
---|
23 | log = logging.getLogger("splicer") |
---|
24 | log.setLevel(logging.DEBUG) |
---|
25 | |
---|
26 | def saveSlice(img, filename, num, left, right, slice_width): |
---|
27 | """ Takes a slice out of the image passed. This slice is defined by |
---|
28 | left and righ and will be saved as filename extended with num before |
---|
29 | the prefix. |
---|
30 | @type img: PythonMagick.Image |
---|
31 | @param img: Source image to take the slice from |
---|
32 | @type filename: String |
---|
33 | @param filename: Base filename |
---|
34 | @type num: Integer |
---|
35 | @param nun: Suffix for the filename |
---|
36 | @type left: Integer |
---|
37 | @param left: Left boundary of the slice |
---|
38 | @type right: Integer |
---|
39 | @param right: Right boundary of the slice |
---|
40 | @type slice_width: Integer |
---|
41 | @param slice_width: The desired width of a slice, trimmed images will be |
---|
42 | extended to this width. |
---|
43 | @rtype: Boolean |
---|
44 | @return True on success, False on failure """ |
---|
45 | |
---|
46 | # Create the slice |
---|
47 | new_image = PythonMagick.Image(img) |
---|
48 | |
---|
49 | # Crop and trim it |
---|
50 | new_image.crop(PythonMagick._PythonMagick.Geometry(right-left,\ |
---|
51 | new_image.size().height()-1,\ |
---|
52 | left,0)) |
---|
53 | |
---|
54 | new_image.trim() |
---|
55 | |
---|
56 | # Obtain the filename |
---|
57 | (name,ext) = os.path.splitext(filename) |
---|
58 | new_filename = name + str(num) + ext |
---|
59 | log.debug("Going to write slide %d to %s" %(num,new_filename)) |
---|
60 | new_image.write(new_filename) |
---|
61 | log.info("Wrote %s",new_filename) |
---|
62 | return True |
---|
63 | |
---|
64 | def sliceImage(filename, slice_width=70): |
---|
65 | """ Opens an image, and produces slice_width pixel wide slices of it |
---|
66 | @type filename: String |
---|
67 | @param filename: Pathname of the image to open |
---|
68 | @type slice_width: Integer |
---|
69 | @param slice_width: Width of the slices that should be made |
---|
70 | @rtype: Bool |
---|
71 | @return True on success, false on failure. """ |
---|
72 | |
---|
73 | if not os.path.isfile(filename): |
---|
74 | log.info("File does not exist: %s", filename) |
---|
75 | return False |
---|
76 | |
---|
77 | img = PythonMagick.Image(filename) |
---|
78 | log.debug("Loaded file: %s", filename) |
---|
79 | img_width = img.size().width() |
---|
80 | img_height = img.size().height() |
---|
81 | log.debug("Image geometry before trim %dx%d", img_width, img_height) |
---|
82 | |
---|
83 | img.trim() |
---|
84 | img_width = img.size().width() |
---|
85 | img_height = img.size().height() |
---|
86 | log.debug("Image geometry after trim %dx%d", img_width, img_height) |
---|
87 | |
---|
88 | current_pos = 0 |
---|
89 | num = 0 |
---|
90 | while current_pos + slice_width < img_width: |
---|
91 | # Save the slice |
---|
92 | if not saveSlice(img, filename, num, current_pos, \ |
---|
93 | current_pos + slice_width, slice_width): |
---|
94 | log.info("Failed to save slice: %d", num) |
---|
95 | return False |
---|
96 | num = num + 1 |
---|
97 | current_pos += slice_width |
---|
98 | |
---|
99 | # Handle the tail |
---|
100 | if(current_pos < img_width): |
---|
101 | if not saveSlice(img, filename, num, current_pos, \ |
---|
102 | img_width-1, slice_width): |
---|
103 | log.info("Failed to save slice: %d", num) |
---|
104 | return False |
---|
105 | return True |
---|
106 | |
---|
107 | def usage(): |
---|
108 | """ Prints the help message. """ |
---|
109 | print "Usage: %s [options]" % sys.argv[0] |
---|
110 | print "Options:" |
---|
111 | print " --verbose, -v Verbose" |
---|
112 | print " --help, -h This help message" |
---|
113 | print " --file FILE, -f File to splice" |
---|
114 | |
---|
115 | def main(argv): |
---|
116 | """ Main function, parses the command line arguments |
---|
117 | and launches the splicing logic. """ |
---|
118 | |
---|
119 | try: |
---|
120 | opts, args = getopt.getopt(argv, "hf:v", |
---|
121 | ["help","file", "verbose"]) |
---|
122 | except getopt.GetoptError: |
---|
123 | usage() |
---|
124 | sys.exit(2) |
---|
125 | |
---|
126 | ch = logging.StreamHandler() |
---|
127 | ch.setLevel(logging.INFO) |
---|
128 | |
---|
129 | file = "" |
---|
130 | for opt, arg in opts: |
---|
131 | if opt in ("-f","--file"): |
---|
132 | file = arg |
---|
133 | elif opt in ("-v","--verbose"): |
---|
134 | ch.setLevel(logging.DEBUG) |
---|
135 | else: |
---|
136 | usage() |
---|
137 | sys.exit() |
---|
138 | |
---|
139 | formatter = logging.Formatter("%(levelname)s: %(message)s") |
---|
140 | ch.setFormatter(formatter) |
---|
141 | log.addHandler(ch) |
---|
142 | |
---|
143 | if sliceImage(file, 70): |
---|
144 | log.info("Done") |
---|
145 | else: |
---|
146 | log.info("Aborted") |
---|
147 | |
---|
148 | if __name__ == '__main__': |
---|
149 | main(sys.argv[1:]) |
---|