paul@0 | 1 | #!/usr/bin/env python |
paul@0 | 2 | |
paul@56 | 3 | """ |
paul@56 | 4 | Convert and optimise images for display in an Acorn Electron MODE 1 variant |
paul@67 | 5 | with four colours per line but eight colours available for selection on each |
paul@56 | 6 | line. |
paul@56 | 7 | |
paul@56 | 8 | Copyright (C) 2015 Paul Boddie <paul@boddie.org.uk> |
paul@56 | 9 | |
paul@56 | 10 | This program is free software; you can redistribute it and/or modify it under |
paul@56 | 11 | the terms of the GNU General Public License as published by the Free Software |
paul@56 | 12 | Foundation; either version 3 of the License, or (at your option) any later |
paul@56 | 13 | version. |
paul@56 | 14 | |
paul@56 | 15 | This program is distributed in the hope that it will be useful, but WITHOUT ANY |
paul@56 | 16 | WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A |
paul@56 | 17 | PARTICULAR PURPOSE. See the GNU General Public License for more details. |
paul@56 | 18 | |
paul@56 | 19 | You should have received a copy of the GNU General Public License along |
paul@56 | 20 | with this program. If not, see <http://www.gnu.org/licenses/>. |
paul@56 | 21 | """ |
paul@56 | 22 | |
paul@58 | 23 | from random import random, randrange |
paul@56 | 24 | from os.path import split, splitext |
paul@12 | 25 | import EXIF |
paul@0 | 26 | import PIL.Image |
paul@60 | 27 | import itertools |
paul@0 | 28 | import sys |
paul@0 | 29 | |
paul@20 | 30 | corners = [ |
paul@20 | 31 | (0, 0, 0), (255, 0, 0), (0, 255, 0), (255, 255, 0), |
paul@20 | 32 | (0, 0, 255), (255, 0, 255), (0, 255, 255), (255, 255, 255) |
paul@0 | 33 | ] |
paul@0 | 34 | |
paul@56 | 35 | # Basic colour operations. |
paul@56 | 36 | |
paul@49 | 37 | def within(v, lower, upper): |
paul@49 | 38 | return min(max(v, lower), upper) |
paul@49 | 39 | |
paul@49 | 40 | def clip(v): |
paul@69 | 41 | return int(within(v, 0, 255)) |
paul@49 | 42 | |
paul@35 | 43 | def restore(srgb): |
paul@76 | 44 | r, g, b = srgb |
paul@76 | 45 | return int(r * 255.0), int(g * 255.0), int(b * 255.0) |
paul@35 | 46 | |
paul@34 | 47 | def scale(rgb): |
paul@76 | 48 | r, g, b = rgb |
paul@76 | 49 | return r / 255.0, g / 255.0, b / 255.0 |
paul@34 | 50 | |
paul@34 | 51 | def invert(srgb): |
paul@76 | 52 | r, g, b = srgb |
paul@76 | 53 | return 1.0 - r, 1.0 - g, 1.0 - b |
paul@34 | 54 | |
paul@79 | 55 | scaled_corners = map(scale, corners) |
paul@79 | 56 | zipped_corners = zip(corners, scaled_corners) |
paul@79 | 57 | |
paul@56 | 58 | # Colour distribution functions. |
paul@56 | 59 | |
paul@34 | 60 | def combination(rgb): |
paul@56 | 61 | |
paul@56 | 62 | "Return the colour distribution for 'rgb'." |
paul@56 | 63 | |
paul@63 | 64 | # Get the colour with components scaled from 0 to 1, plus the inverted |
paul@63 | 65 | # component values. |
paul@56 | 66 | |
paul@81 | 67 | srgb = scale(rgb) |
paul@81 | 68 | rgbi = invert(srgb) |
paul@81 | 69 | pairs = zip(rgbi, srgb) |
paul@56 | 70 | |
paul@63 | 71 | # For each corner of the colour cube (primary and secondary colours plus |
paul@63 | 72 | # black and white), calculate the corner value's contribution to the |
paul@63 | 73 | # input colour. |
paul@56 | 74 | |
paul@63 | 75 | d = [] |
paul@79 | 76 | for corner, scaled in zipped_corners: |
paul@79 | 77 | rs, gs, bs = scaled |
paul@56 | 78 | |
paul@63 | 79 | # Obtain inverted channel values where corner channels are low; |
paul@63 | 80 | # obtain original channel values where corner channels are high. |
paul@56 | 81 | |
paul@63 | 82 | d.append((pairs[0][int(rs)] * pairs[1][int(gs)] * pairs[2][int(bs)], corner)) |
paul@56 | 83 | |
paul@63 | 84 | # Balance the corner contributions. |
paul@63 | 85 | |
paul@63 | 86 | return balance(d) |
paul@35 | 87 | |
paul@35 | 88 | def complements(rgb): |
paul@56 | 89 | |
paul@56 | 90 | "Return 'rgb' and its complement." |
paul@56 | 91 | |
paul@35 | 92 | r, g, b = rgb |
paul@35 | 93 | return rgb, restore(invert(scale(rgb))) |
paul@35 | 94 | |
paul@79 | 95 | bases = [(0, 0, 0), (255, 0, 0), (0, 255, 0), (0, 0, 255)] |
paul@79 | 96 | base_complements = map(complements, bases) |
paul@79 | 97 | |
paul@35 | 98 | def balance(d): |
paul@56 | 99 | |
paul@56 | 100 | """ |
paul@56 | 101 | Balance distribution 'd', cancelling opposing values and their complements |
paul@56 | 102 | and replacing their common contributions with black and white contributions. |
paul@56 | 103 | """ |
paul@56 | 104 | |
paul@35 | 105 | d = dict([(value, f) for f, value in d]) |
paul@79 | 106 | for primary, secondary in base_complements: |
paul@35 | 107 | common = min(d[primary], d[secondary]) |
paul@35 | 108 | d[primary] -= common |
paul@35 | 109 | d[secondary] -= common |
paul@35 | 110 | return [(f, value) for value, f in d.items()] |
paul@20 | 111 | |
paul@34 | 112 | def combine(d): |
paul@56 | 113 | |
paul@56 | 114 | "Combine distribution 'd' to get a colour value." |
paul@56 | 115 | |
paul@34 | 116 | out = [0, 0, 0] |
paul@34 | 117 | for v, rgb in d: |
paul@34 | 118 | out[0] += v * rgb[0] |
paul@34 | 119 | out[1] += v * rgb[1] |
paul@34 | 120 | out[2] += v * rgb[2] |
paul@81 | 121 | return tuple(map(int, out)) |
paul@34 | 122 | |
paul@48 | 123 | def pattern(rgb, chosen=None): |
paul@56 | 124 | |
paul@56 | 125 | """ |
paul@56 | 126 | Obtain a sorted colour distribution for 'rgb', optionally limited to any |
paul@56 | 127 | specified 'chosen' colours. |
paul@56 | 128 | """ |
paul@56 | 129 | |
paul@60 | 130 | l = [(f, value) for f, value in combination(rgb) if not chosen or value in chosen] |
paul@32 | 131 | l.sort(reverse=True) |
paul@20 | 132 | return l |
paul@19 | 133 | |
paul@60 | 134 | def get_value(rgb, chosen=None, fail=False): |
paul@56 | 135 | |
paul@56 | 136 | """ |
paul@56 | 137 | Get an output colour for 'rgb', optionally limited to any specified 'chosen' |
paul@60 | 138 | colours. If 'fail' is set to a true value, return None if the colour cannot |
paul@60 | 139 | be expressed using any of the chosen colours. |
paul@56 | 140 | """ |
paul@56 | 141 | |
paul@48 | 142 | l = pattern(rgb, chosen) |
paul@48 | 143 | limit = sum([f for f, c in l]) |
paul@58 | 144 | if not limit: |
paul@60 | 145 | if fail: |
paul@60 | 146 | return None |
paul@60 | 147 | else: |
paul@60 | 148 | return l[randrange(0, len(l))][1] |
paul@58 | 149 | |
paul@48 | 150 | choose = random() * limit |
paul@32 | 151 | threshold = 0 |
paul@48 | 152 | for f, c in l: |
paul@32 | 153 | threshold += f |
paul@32 | 154 | if choose < threshold: |
paul@32 | 155 | return c |
paul@32 | 156 | return c |
paul@0 | 157 | |
paul@56 | 158 | # Colour processing operations. |
paul@56 | 159 | |
paul@21 | 160 | def sign(x): |
paul@21 | 161 | return x >= 0 and 1 or -1 |
paul@21 | 162 | |
paul@22 | 163 | def saturate_rgb(rgb, exp): |
paul@77 | 164 | r, g, b = rgb |
paul@77 | 165 | return saturate_value(r, exp), saturate_value(g, exp), saturate_value(b, exp) |
paul@21 | 166 | |
paul@22 | 167 | def saturate_value(x, exp): |
paul@24 | 168 | return int(127.5 + sign(x - 127.5) * 127.5 * pow(abs(x - 127.5) / 127.5, exp)) |
paul@21 | 169 | |
paul@49 | 170 | def amplify_rgb(rgb, exp): |
paul@77 | 171 | r, g, b = rgb |
paul@77 | 172 | return amplify_value(r, exp), amplify_value(g, exp), amplify_value(b, exp) |
paul@49 | 173 | |
paul@49 | 174 | def amplify_value(x, exp): |
paul@49 | 175 | return int(pow(x / 255.0, exp) * 255.0) |
paul@49 | 176 | |
paul@56 | 177 | # Image operations. |
paul@56 | 178 | |
paul@50 | 179 | def get_colours(im, y): |
paul@56 | 180 | |
paul@56 | 181 | "Get a colour distribution from image 'im' for the row 'y'." |
paul@56 | 182 | |
paul@50 | 183 | width, height = im.size |
paul@49 | 184 | c = {} |
paul@110 | 185 | x = 0 |
paul@110 | 186 | while x < width: |
paul@49 | 187 | rgb = im.getpixel((x, y)) |
paul@49 | 188 | |
paul@49 | 189 | # Sum the colour probabilities. |
paul@49 | 190 | |
paul@49 | 191 | for f, value in combination(rgb): |
paul@49 | 192 | if not c.has_key(value): |
paul@49 | 193 | c[value] = f |
paul@49 | 194 | else: |
paul@49 | 195 | c[value] += f |
paul@49 | 196 | |
paul@110 | 197 | x += 1 |
paul@110 | 198 | |
paul@81 | 199 | d = [(n/width, value) for value, n in c.items()] |
paul@81 | 200 | d.sort(reverse=True) |
paul@81 | 201 | return d |
paul@33 | 202 | |
paul@60 | 203 | def get_combinations(c, n): |
paul@60 | 204 | |
paul@60 | 205 | """ |
paul@60 | 206 | Get combinations of colours from 'c' of size 'n' in decreasing order of |
paul@60 | 207 | probability. |
paul@60 | 208 | """ |
paul@60 | 209 | |
paul@60 | 210 | all = [] |
paul@60 | 211 | for l in itertools.combinations(c, n): |
paul@60 | 212 | total = 0 |
paul@60 | 213 | for f, value in l: |
paul@60 | 214 | total += f |
paul@60 | 215 | all.append((total, l)) |
paul@60 | 216 | all.sort(reverse=True) |
paul@60 | 217 | return [l for total, l in all] |
paul@60 | 218 | |
paul@20 | 219 | def test(): |
paul@56 | 220 | |
paul@56 | 221 | "Generate slices of the colour cube." |
paul@56 | 222 | |
paul@20 | 223 | size = 512 |
paul@20 | 224 | for r in (0, 63, 127, 191, 255): |
paul@20 | 225 | im = PIL.Image.new("RGB", (size, size)) |
paul@20 | 226 | for g in range(0, size): |
paul@20 | 227 | for b in range(0, size): |
paul@28 | 228 | value = get_value((r, (g * 256) / size, (b * 256 / size))) |
paul@20 | 229 | im.putpixel((g, b), value) |
paul@20 | 230 | im.save("rgb%d.png" % r) |
paul@20 | 231 | |
paul@20 | 232 | def test_flat(rgb): |
paul@56 | 233 | |
paul@56 | 234 | "Generate a flat image for the colour 'rgb'." |
paul@56 | 235 | |
paul@20 | 236 | size = 64 |
paul@20 | 237 | im = PIL.Image.new("RGB", (size, size)) |
paul@110 | 238 | y = 0 |
paul@110 | 239 | while y < height: |
paul@110 | 240 | x = 0 |
paul@110 | 241 | while x < width: |
paul@28 | 242 | im.putpixel((x, y), get_value(rgb)) |
paul@110 | 243 | x += 1 |
paul@110 | 244 | y += 1 |
paul@20 | 245 | im.save("rgb%02d%02d%02d.png" % rgb) |
paul@20 | 246 | |
paul@50 | 247 | def rotate_and_scale(exif, im, width, height, rotate): |
paul@56 | 248 | |
paul@56 | 249 | """ |
paul@56 | 250 | Using the given 'exif' information, rotate and scale image 'im' given the |
paul@56 | 251 | indicated 'width' and 'height' constraints and any explicit 'rotate' |
paul@56 | 252 | indication. The returned image will be within the given 'width' and |
paul@56 | 253 | 'height', filling either or both, and preserve its original aspect ratio. |
paul@56 | 254 | """ |
paul@56 | 255 | |
paul@50 | 256 | if rotate or exif and exif["Image Orientation"].values == [6L]: |
paul@11 | 257 | im = im.rotate(270) |
paul@19 | 258 | |
paul@4 | 259 | w, h = im.size |
paul@19 | 260 | if w > h: |
paul@19 | 261 | height = (width * h) / w |
paul@19 | 262 | else: |
paul@19 | 263 | width = (height * w) / h |
paul@19 | 264 | |
paul@20 | 265 | return im.resize((width, height)) |
paul@1 | 266 | |
paul@50 | 267 | def count_colours(im, colours): |
paul@56 | 268 | |
paul@56 | 269 | """ |
paul@56 | 270 | Count colours on each row of image 'im', returning a tuple indicating the |
paul@56 | 271 | first row with more than the given number of 'colours' together with the |
paul@56 | 272 | found colours; otherwise returning None. |
paul@56 | 273 | """ |
paul@56 | 274 | |
paul@50 | 275 | width, height = im.size |
paul@70 | 276 | |
paul@110 | 277 | y = 0 |
paul@110 | 278 | while y < height: |
paul@50 | 279 | l = set() |
paul@110 | 280 | x = 0 |
paul@110 | 281 | while x < width: |
paul@50 | 282 | l.add(im.getpixel((x, y))) |
paul@110 | 283 | x += 1 |
paul@50 | 284 | if len(l) > colours: |
paul@50 | 285 | return (y, l) |
paul@110 | 286 | y += 1 |
paul@50 | 287 | return None |
paul@50 | 288 | |
paul@70 | 289 | def process_image(im, saturate, desaturate, darken, brighten): |
paul@70 | 290 | |
paul@70 | 291 | """ |
paul@70 | 292 | Process image 'im' using the given options: 'saturate', 'desaturate', |
paul@70 | 293 | 'darken', 'brighten'. |
paul@70 | 294 | """ |
paul@70 | 295 | |
paul@70 | 296 | width, height = im.size |
paul@70 | 297 | |
paul@70 | 298 | if saturate or desaturate or darken or brighten: |
paul@110 | 299 | y = 0 |
paul@110 | 300 | while y < height: |
paul@110 | 301 | x = 0 |
paul@110 | 302 | while x < width: |
paul@70 | 303 | rgb = im.getpixel((x, y)) |
paul@70 | 304 | if saturate or desaturate: |
paul@70 | 305 | rgb = saturate_rgb(rgb, saturate and 0.5 / saturate or 2 * desaturate) |
paul@70 | 306 | if darken or brighten: |
paul@70 | 307 | rgb = amplify_rgb(rgb, brighten and 0.5 / brighten or 2 * darken) |
paul@70 | 308 | im.putpixel((x, y), rgb) |
paul@110 | 309 | x += 1 |
paul@110 | 310 | y += 1 |
paul@70 | 311 | |
paul@104 | 312 | def convert_image(im, colours): |
paul@70 | 313 | |
paul@70 | 314 | "Convert image 'im' to an appropriate output representation." |
paul@70 | 315 | |
paul@70 | 316 | width, height = im.size |
paul@70 | 317 | |
paul@110 | 318 | y = 0 |
paul@110 | 319 | while y < height: |
paul@70 | 320 | c = get_colours(im, y) |
paul@70 | 321 | |
paul@107 | 322 | suggestions = [] |
paul@107 | 323 | |
paul@104 | 324 | for l in get_combinations(c, colours): |
paul@70 | 325 | most = [value for f, value in l] |
paul@107 | 326 | missing = 0 |
paul@107 | 327 | |
paul@110 | 328 | x = 0 |
paul@110 | 329 | while x < width: |
paul@70 | 330 | rgb = im.getpixel((x, y)) |
paul@70 | 331 | value = get_value(rgb, most, True) |
paul@70 | 332 | if value is None: |
paul@107 | 333 | missing += 1 |
paul@110 | 334 | x += 1 |
paul@107 | 335 | |
paul@107 | 336 | if not missing: |
paul@70 | 337 | break # use this combination |
paul@107 | 338 | suggestions.append((missing, l)) |
paul@107 | 339 | |
paul@107 | 340 | # Find the most accurate suggestion. |
paul@107 | 341 | |
paul@70 | 342 | else: |
paul@107 | 343 | suggestions.sort() |
paul@107 | 344 | most = [value for f, value in suggestions[0][1]] # get the combination |
paul@70 | 345 | |
paul@110 | 346 | x = 0 |
paul@110 | 347 | while x < width: |
paul@70 | 348 | rgb = im.getpixel((x, y)) |
paul@70 | 349 | value = get_value(rgb, most) |
paul@70 | 350 | im.putpixel((x, y), value) |
paul@70 | 351 | |
paul@70 | 352 | if x < width - 1: |
paul@70 | 353 | rgbn = im.getpixel((x+1, y)) |
paul@77 | 354 | rgbn = ( |
paul@77 | 355 | clip(rgbn[0] + (rgb[0] - value[0]) / 4.0), |
paul@77 | 356 | clip(rgbn[1] + (rgb[1] - value[1]) / 4.0), |
paul@77 | 357 | clip(rgbn[2] + (rgb[2] - value[2]) / 4.0) |
paul@77 | 358 | ) |
paul@70 | 359 | im.putpixel((x+1, y), rgbn) |
paul@70 | 360 | |
paul@70 | 361 | if y < height - 1: |
paul@70 | 362 | rgbn = im.getpixel((x, y+1)) |
paul@77 | 363 | rgbn = ( |
paul@77 | 364 | clip(rgbn[0] + (rgb[0] - value[0]) / 2.0), |
paul@77 | 365 | clip(rgbn[1] + (rgb[1] - value[1]) / 2.0), |
paul@77 | 366 | clip(rgbn[2] + (rgb[2] - value[2]) / 2.0) |
paul@77 | 367 | ) |
paul@70 | 368 | im.putpixel((x, y+1), rgbn) |
paul@70 | 369 | |
paul@110 | 370 | x += 1 |
paul@110 | 371 | |
paul@110 | 372 | y += 1 |
paul@110 | 373 | |
paul@104 | 374 | def get_parameter(options, flag, conversion, default, missing): |
paul@104 | 375 | |
paul@104 | 376 | """ |
paul@104 | 377 | From 'options', return any parameter following the given 'flag', applying |
paul@104 | 378 | the 'conversion' which has the given 'default' if no valid parameter is |
paul@104 | 379 | found, or returning the given 'missing' value if the flag does not appear at |
paul@104 | 380 | all. |
paul@104 | 381 | """ |
paul@104 | 382 | |
paul@65 | 383 | try: |
paul@65 | 384 | i = options.index(flag) |
paul@100 | 385 | try: |
paul@104 | 386 | return conversion(options[i+1]) |
paul@100 | 387 | except (IndexError, ValueError): |
paul@104 | 388 | return default |
paul@65 | 389 | except ValueError: |
paul@104 | 390 | return missing |
paul@65 | 391 | |
paul@56 | 392 | # Main program. |
paul@56 | 393 | |
paul@20 | 394 | if __name__ == "__main__": |
paul@56 | 395 | |
paul@56 | 396 | # Test options. |
paul@56 | 397 | |
paul@20 | 398 | if "--test" in sys.argv: |
paul@20 | 399 | test() |
paul@20 | 400 | sys.exit(0) |
paul@20 | 401 | elif "--test-flat" in sys.argv: |
paul@20 | 402 | test_flat((120, 40, 60)) |
paul@20 | 403 | sys.exit(0) |
paul@56 | 404 | elif "--help" in sys.argv: |
paul@56 | 405 | print >>sys.stderr, """\ |
paul@56 | 406 | Usage: %s <input filename> <output filename> [ <options> ] |
paul@56 | 407 | |
paul@56 | 408 | Options are... |
paul@56 | 409 | |
paul@104 | 410 | -W - Indicate the output width (default is 320) |
paul@104 | 411 | -C - Number of colours per scanline (default is 4) |
paul@104 | 412 | |
paul@104 | 413 | -s - Saturate the input image (optional float, 1.0 if unspecified) |
paul@104 | 414 | -d - Desaturate the input image (optional float, 1.0 if unspecified) |
paul@104 | 415 | -D - Darken the input image (optional float, 1.0 if unspecified) |
paul@104 | 416 | -B - Brighten the input image (optional float, 1.0 if unspecified) |
paul@56 | 417 | |
paul@56 | 418 | -r - Rotate the input image clockwise |
paul@56 | 419 | -p - Generate a separate preview image |
paul@56 | 420 | -h - Make the preview image with half horizontal resolution (MODE 2) |
paul@56 | 421 | -v - Verify the output image (loaded if -n is given) |
paul@56 | 422 | -n - Generate no output image |
paul@56 | 423 | """ % split(sys.argv[0])[1] |
paul@56 | 424 | sys.exit(1) |
paul@0 | 425 | |
paul@104 | 426 | base_width = 320 |
paul@20 | 427 | height = 256 |
paul@20 | 428 | |
paul@20 | 429 | input_filename, output_filename = sys.argv[1:3] |
paul@26 | 430 | basename, ext = splitext(output_filename) |
paul@35 | 431 | preview_filename = "".join([basename + "_preview", ext]) |
paul@26 | 432 | |
paul@50 | 433 | options = sys.argv[3:] |
paul@1 | 434 | |
paul@104 | 435 | # Basic image properties. |
paul@104 | 436 | |
paul@104 | 437 | width = get_parameter(options, "-W", int, base_width, base_width) |
paul@104 | 438 | number_of_colours = get_parameter(options, "-C", int, 4, 4) |
paul@56 | 439 | |
paul@104 | 440 | # Preprocessing options that employ parameters. |
paul@104 | 441 | |
paul@104 | 442 | saturate = get_parameter(options, "-s", float, 1.0, 0.0) |
paul@104 | 443 | desaturate = get_parameter(options, "-d", float, 1.0, 0.0) |
paul@104 | 444 | darken = get_parameter(options, "-D", float, 1.0, 0.0) |
paul@104 | 445 | brighten = get_parameter(options, "-B", float, 1.0, 0.0) |
paul@64 | 446 | |
paul@56 | 447 | # General output options. |
paul@56 | 448 | |
paul@56 | 449 | rotate = "-r" in options |
paul@50 | 450 | preview = "-p" in options |
paul@50 | 451 | half_resolution_preview = "-h" in options |
paul@50 | 452 | verify = "-v" in options |
paul@50 | 453 | no_normal_output = "-n" in options |
paul@50 | 454 | make_image = not no_normal_output |
paul@1 | 455 | |
paul@56 | 456 | # Load the input image if requested. |
paul@56 | 457 | |
paul@50 | 458 | if make_image or preview: |
paul@50 | 459 | exif = EXIF.process_file(open(input_filename)) |
paul@50 | 460 | im = PIL.Image.open(input_filename).convert("RGB") |
paul@104 | 461 | im = rotate_and_scale(exif, im, base_width, height, rotate) |
paul@104 | 462 | |
paul@104 | 463 | # Scale images to the appropriate width. |
paul@104 | 464 | |
paul@104 | 465 | if width != base_width: |
paul@104 | 466 | im = im.resize((width, height)) |
paul@50 | 467 | |
paul@70 | 468 | process_image(im, saturate, desaturate, darken, brighten) |
paul@10 | 469 | |
paul@56 | 470 | # Generate a preview if requested. |
paul@56 | 471 | |
paul@35 | 472 | if preview: |
paul@104 | 473 | imp = im.copy() |
paul@104 | 474 | if half_resolution_preview: |
paul@104 | 475 | imp = imp.resize((width / 2, height)) |
paul@104 | 476 | convert_image(imp, 8) |
paul@104 | 477 | if half_resolution_preview: |
paul@104 | 478 | imp = imp.resize((width, height)) |
paul@104 | 479 | imp.save(preview_filename) |
paul@35 | 480 | |
paul@56 | 481 | # Generate an output image if requested. |
paul@56 | 482 | |
paul@50 | 483 | if make_image: |
paul@104 | 484 | convert_image(im, number_of_colours) |
paul@50 | 485 | im.save(output_filename) |
paul@1 | 486 | |
paul@56 | 487 | # Verify the output image (which may be loaded) if requested. |
paul@56 | 488 | |
paul@50 | 489 | if verify: |
paul@50 | 490 | if no_normal_output: |
paul@50 | 491 | im = PIL.Image.open(output_filename).convert("RGB") |
paul@49 | 492 | |
paul@104 | 493 | result = count_colours(im, number_of_colours) |
paul@50 | 494 | if result is not None: |
paul@50 | 495 | y, colours = result |
paul@56 | 496 | print "Image %s: row %d has the following colours: %s" % (output_filename, y, "; ".join([repr(c) for c in colours])) |
paul@0 | 497 | |
paul@0 | 498 | # vim: tabstop=4 expandtab shiftwidth=4 |