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@83 | 23 | from optimiserlib import * |
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@56 | 30 | # Image operations. |
paul@56 | 31 | |
paul@50 | 32 | def get_colours(im, y): |
paul@56 | 33 | |
paul@56 | 34 | "Get a colour distribution from image 'im' for the row 'y'." |
paul@56 | 35 | |
paul@50 | 36 | width, height = im.size |
paul@49 | 37 | c = {} |
paul@49 | 38 | for x in range(0, width): |
paul@49 | 39 | rgb = im.getpixel((x, y)) |
paul@49 | 40 | |
paul@49 | 41 | # Sum the colour probabilities. |
paul@49 | 42 | |
paul@49 | 43 | for f, value in combination(rgb): |
paul@49 | 44 | if not c.has_key(value): |
paul@49 | 45 | c[value] = f |
paul@49 | 46 | else: |
paul@49 | 47 | c[value] += f |
paul@49 | 48 | |
paul@81 | 49 | d = [(n/width, value) for value, n in c.items()] |
paul@81 | 50 | d.sort(reverse=True) |
paul@81 | 51 | return d |
paul@33 | 52 | |
paul@60 | 53 | def get_combinations(c, n): |
paul@60 | 54 | |
paul@60 | 55 | """ |
paul@60 | 56 | Get combinations of colours from 'c' of size 'n' in decreasing order of |
paul@60 | 57 | probability. |
paul@60 | 58 | """ |
paul@60 | 59 | |
paul@60 | 60 | all = [] |
paul@60 | 61 | for l in itertools.combinations(c, n): |
paul@60 | 62 | total = 0 |
paul@60 | 63 | for f, value in l: |
paul@60 | 64 | total += f |
paul@60 | 65 | all.append((total, l)) |
paul@60 | 66 | all.sort(reverse=True) |
paul@60 | 67 | return [l for total, l in all] |
paul@60 | 68 | |
paul@20 | 69 | def test(): |
paul@56 | 70 | |
paul@56 | 71 | "Generate slices of the colour cube." |
paul@56 | 72 | |
paul@20 | 73 | size = 512 |
paul@20 | 74 | for r in (0, 63, 127, 191, 255): |
paul@20 | 75 | im = PIL.Image.new("RGB", (size, size)) |
paul@20 | 76 | for g in range(0, size): |
paul@20 | 77 | for b in range(0, size): |
paul@28 | 78 | value = get_value((r, (g * 256) / size, (b * 256 / size))) |
paul@20 | 79 | im.putpixel((g, b), value) |
paul@20 | 80 | im.save("rgb%d.png" % r) |
paul@20 | 81 | |
paul@20 | 82 | def test_flat(rgb): |
paul@56 | 83 | |
paul@56 | 84 | "Generate a flat image for the colour 'rgb'." |
paul@56 | 85 | |
paul@20 | 86 | size = 64 |
paul@20 | 87 | im = PIL.Image.new("RGB", (size, size)) |
paul@20 | 88 | for y in range(0, size): |
paul@20 | 89 | for x in range(0, size): |
paul@28 | 90 | im.putpixel((x, y), get_value(rgb)) |
paul@20 | 91 | im.save("rgb%02d%02d%02d.png" % rgb) |
paul@20 | 92 | |
paul@50 | 93 | def rotate_and_scale(exif, im, width, height, rotate): |
paul@56 | 94 | |
paul@56 | 95 | """ |
paul@56 | 96 | Using the given 'exif' information, rotate and scale image 'im' given the |
paul@56 | 97 | indicated 'width' and 'height' constraints and any explicit 'rotate' |
paul@56 | 98 | indication. The returned image will be within the given 'width' and |
paul@56 | 99 | 'height', filling either or both, and preserve its original aspect ratio. |
paul@56 | 100 | """ |
paul@56 | 101 | |
paul@50 | 102 | if rotate or exif and exif["Image Orientation"].values == [6L]: |
paul@11 | 103 | im = im.rotate(270) |
paul@19 | 104 | |
paul@4 | 105 | w, h = im.size |
paul@19 | 106 | if w > h: |
paul@19 | 107 | height = (width * h) / w |
paul@19 | 108 | else: |
paul@19 | 109 | width = (height * w) / h |
paul@19 | 110 | |
paul@20 | 111 | return im.resize((width, height)) |
paul@1 | 112 | |
paul@50 | 113 | def count_colours(im, colours): |
paul@56 | 114 | |
paul@56 | 115 | """ |
paul@56 | 116 | Count colours on each row of image 'im', returning a tuple indicating the |
paul@56 | 117 | first row with more than the given number of 'colours' together with the |
paul@56 | 118 | found colours; otherwise returning None. |
paul@56 | 119 | """ |
paul@56 | 120 | |
paul@50 | 121 | width, height = im.size |
paul@70 | 122 | |
paul@50 | 123 | for y in range(0, height): |
paul@50 | 124 | l = set() |
paul@50 | 125 | for x in range(0, width): |
paul@50 | 126 | l.add(im.getpixel((x, y))) |
paul@50 | 127 | if len(l) > colours: |
paul@50 | 128 | return (y, l) |
paul@50 | 129 | return None |
paul@50 | 130 | |
paul@75 | 131 | def process_image(pim, saturate, desaturate, darken, brighten): |
paul@70 | 132 | |
paul@70 | 133 | """ |
paul@75 | 134 | Process image 'pim' using the given options: 'saturate', 'desaturate', |
paul@70 | 135 | 'darken', 'brighten'. |
paul@70 | 136 | """ |
paul@70 | 137 | |
paul@75 | 138 | width, height = pim.size |
paul@75 | 139 | im = SimpleImage(list(pim.getdata()), pim.size) |
paul@70 | 140 | |
paul@70 | 141 | if saturate or desaturate or darken or brighten: |
paul@70 | 142 | for y in range(0, height): |
paul@70 | 143 | for x in range(0, width): |
paul@70 | 144 | rgb = im.getpixel((x, y)) |
paul@70 | 145 | if saturate or desaturate: |
paul@70 | 146 | rgb = saturate_rgb(rgb, saturate and 0.5 / saturate or 2 * desaturate) |
paul@70 | 147 | if darken or brighten: |
paul@70 | 148 | rgb = amplify_rgb(rgb, brighten and 0.5 / brighten or 2 * darken) |
paul@70 | 149 | im.putpixel((x, y), rgb) |
paul@70 | 150 | |
paul@75 | 151 | pim.putdata(im.getdata()) |
paul@70 | 152 | |
paul@75 | 153 | def preview_image(pim, half_resolution_preview=False): |
paul@70 | 154 | |
paul@75 | 155 | "Return a preview copy of image 'pim'." |
paul@70 | 156 | |
paul@75 | 157 | width, height = pim.size |
paul@75 | 158 | imp = pim.copy() |
paul@75 | 159 | im = SimpleImage(list(pim.getdata()), pim.size) |
paul@70 | 160 | step = half_resolution_preview and 2 or 1 |
paul@70 | 161 | |
paul@70 | 162 | for y in range(0, height): |
paul@75 | 163 | for x in range(0, width): |
paul@75 | 164 | rgb = im.getpixel((x, y)) |
paul@70 | 165 | value = get_value(rgb) |
paul@75 | 166 | im.putpixel((x, y), value) |
paul@70 | 167 | if half_resolution_preview: |
paul@75 | 168 | im.putpixel((x+1, y), value) |
paul@70 | 169 | |
paul@75 | 170 | imp.putdata(im.getdata()) |
paul@70 | 171 | return imp |
paul@70 | 172 | |
paul@75 | 173 | def convert_image(pim): |
paul@70 | 174 | |
paul@75 | 175 | "Convert image 'pim' to an appropriate output representation." |
paul@70 | 176 | |
paul@75 | 177 | width, height = pim.size |
paul@75 | 178 | im = SimpleImage(list(pim.getdata()), pim.size) |
paul@70 | 179 | |
paul@70 | 180 | for y in range(0, height): |
paul@70 | 181 | c = get_colours(im, y) |
paul@70 | 182 | |
paul@70 | 183 | for l in get_combinations(c, 4): |
paul@70 | 184 | most = [value for f, value in l] |
paul@70 | 185 | for x in range(0, width): |
paul@70 | 186 | rgb = im.getpixel((x, y)) |
paul@70 | 187 | value = get_value(rgb, most, True) |
paul@70 | 188 | if value is None: |
paul@70 | 189 | break # try next combination |
paul@70 | 190 | else: |
paul@70 | 191 | break # use this combination |
paul@70 | 192 | else: |
paul@70 | 193 | most = [value for f, value in c[:4]] # use the first four |
paul@70 | 194 | |
paul@70 | 195 | for x in range(0, width): |
paul@70 | 196 | rgb = im.getpixel((x, y)) |
paul@70 | 197 | value = get_value(rgb, most) |
paul@70 | 198 | im.putpixel((x, y), value) |
paul@70 | 199 | |
paul@70 | 200 | if x < width - 1: |
paul@70 | 201 | rgbn = im.getpixel((x+1, y)) |
paul@77 | 202 | rgbn = ( |
paul@77 | 203 | clip(rgbn[0] + (rgb[0] - value[0]) / 4.0), |
paul@77 | 204 | clip(rgbn[1] + (rgb[1] - value[1]) / 4.0), |
paul@77 | 205 | clip(rgbn[2] + (rgb[2] - value[2]) / 4.0) |
paul@77 | 206 | ) |
paul@70 | 207 | im.putpixel((x+1, y), rgbn) |
paul@70 | 208 | |
paul@70 | 209 | if y < height - 1: |
paul@70 | 210 | rgbn = im.getpixel((x, y+1)) |
paul@77 | 211 | rgbn = ( |
paul@77 | 212 | clip(rgbn[0] + (rgb[0] - value[0]) / 2.0), |
paul@77 | 213 | clip(rgbn[1] + (rgb[1] - value[1]) / 2.0), |
paul@77 | 214 | clip(rgbn[2] + (rgb[2] - value[2]) / 2.0) |
paul@77 | 215 | ) |
paul@70 | 216 | im.putpixel((x, y+1), rgbn) |
paul@70 | 217 | |
paul@75 | 218 | pim.putdata(im.getdata()) |
paul@75 | 219 | |
paul@65 | 220 | def get_float(options, flag): |
paul@65 | 221 | try: |
paul@65 | 222 | i = options.index(flag) |
paul@66 | 223 | if i+1 < len(options) and options[i+1].isdigit(): |
paul@65 | 224 | return float(options[i+1]) |
paul@65 | 225 | else: |
paul@65 | 226 | return 1.0 |
paul@65 | 227 | except ValueError: |
paul@65 | 228 | return 0.0 |
paul@65 | 229 | |
paul@75 | 230 | class SimpleImage: |
paul@75 | 231 | |
paul@75 | 232 | "An image behaving like PIL.Image." |
paul@75 | 233 | |
paul@75 | 234 | def __init__(self, data, size): |
paul@75 | 235 | self.data = data |
paul@75 | 236 | self.width, self.height = self.size = size |
paul@75 | 237 | |
paul@75 | 238 | def copy(self): |
paul@75 | 239 | return SimpleImage(self.data[:], self.size) |
paul@75 | 240 | |
paul@75 | 241 | def getpixel(self, xy): |
paul@75 | 242 | x, y = xy |
paul@75 | 243 | return self.data[y * self.width + x] |
paul@75 | 244 | |
paul@75 | 245 | def putpixel(self, xy, value): |
paul@75 | 246 | x, y = xy |
paul@75 | 247 | self.data[y * self.width + x] = value |
paul@75 | 248 | |
paul@75 | 249 | def getdata(self): |
paul@75 | 250 | return self.data |
paul@75 | 251 | |
paul@56 | 252 | # Main program. |
paul@56 | 253 | |
paul@20 | 254 | if __name__ == "__main__": |
paul@56 | 255 | |
paul@56 | 256 | # Test options. |
paul@56 | 257 | |
paul@20 | 258 | if "--test" in sys.argv: |
paul@20 | 259 | test() |
paul@20 | 260 | sys.exit(0) |
paul@20 | 261 | elif "--test-flat" in sys.argv: |
paul@20 | 262 | test_flat((120, 40, 60)) |
paul@20 | 263 | sys.exit(0) |
paul@56 | 264 | elif "--help" in sys.argv: |
paul@56 | 265 | print >>sys.stderr, """\ |
paul@56 | 266 | Usage: %s <input filename> <output filename> [ <options> ] |
paul@56 | 267 | |
paul@56 | 268 | Options are... |
paul@56 | 269 | |
paul@67 | 270 | -s - Saturate the input image (can be followed by a float, default 1.0) |
paul@67 | 271 | -d - Desaturate the input image (can be followed by a float, default 1.0) |
paul@65 | 272 | -D - Darken the input image (can be followed by a float, default 1.0) |
paul@65 | 273 | -B - Brighten the input image (can be followed by a float, default 1.0) |
paul@56 | 274 | |
paul@56 | 275 | -r - Rotate the input image clockwise |
paul@56 | 276 | -p - Generate a separate preview image |
paul@56 | 277 | -h - Make the preview image with half horizontal resolution (MODE 2) |
paul@56 | 278 | -v - Verify the output image (loaded if -n is given) |
paul@56 | 279 | -n - Generate no output image |
paul@56 | 280 | """ % split(sys.argv[0])[1] |
paul@56 | 281 | sys.exit(1) |
paul@0 | 282 | |
paul@20 | 283 | width = 320 |
paul@20 | 284 | height = 256 |
paul@20 | 285 | |
paul@20 | 286 | input_filename, output_filename = sys.argv[1:3] |
paul@26 | 287 | basename, ext = splitext(output_filename) |
paul@35 | 288 | preview_filename = "".join([basename + "_preview", ext]) |
paul@26 | 289 | |
paul@50 | 290 | options = sys.argv[3:] |
paul@1 | 291 | |
paul@56 | 292 | # Preprocessing options that can be repeated for extra effect. |
paul@56 | 293 | |
paul@67 | 294 | saturate = get_float(options, "-s") |
paul@67 | 295 | desaturate = get_float(options, "-d") |
paul@65 | 296 | darken = get_float(options, "-D") |
paul@65 | 297 | brighten = get_float(options, "-B") |
paul@64 | 298 | |
paul@56 | 299 | # General output options. |
paul@56 | 300 | |
paul@56 | 301 | rotate = "-r" in options |
paul@50 | 302 | preview = "-p" in options |
paul@50 | 303 | half_resolution_preview = "-h" in options |
paul@50 | 304 | verify = "-v" in options |
paul@50 | 305 | no_normal_output = "-n" in options |
paul@50 | 306 | make_image = not no_normal_output |
paul@1 | 307 | |
paul@56 | 308 | # Load the input image if requested. |
paul@56 | 309 | |
paul@50 | 310 | if make_image or preview: |
paul@50 | 311 | exif = EXIF.process_file(open(input_filename)) |
paul@50 | 312 | im = PIL.Image.open(input_filename).convert("RGB") |
paul@50 | 313 | im = rotate_and_scale(exif, im, width, height, rotate) |
paul@50 | 314 | |
paul@70 | 315 | process_image(im, saturate, desaturate, darken, brighten) |
paul@10 | 316 | |
paul@56 | 317 | # Generate a preview if requested. |
paul@56 | 318 | |
paul@35 | 319 | if preview: |
paul@70 | 320 | preview_image(im, half_resolution_preview).save(preview_filename) |
paul@35 | 321 | |
paul@56 | 322 | # Generate an output image if requested. |
paul@56 | 323 | |
paul@50 | 324 | if make_image: |
paul@70 | 325 | convert_image(im) |
paul@50 | 326 | im.save(output_filename) |
paul@1 | 327 | |
paul@56 | 328 | # Verify the output image (which may be loaded) if requested. |
paul@56 | 329 | |
paul@50 | 330 | if verify: |
paul@50 | 331 | if no_normal_output: |
paul@50 | 332 | im = PIL.Image.open(output_filename).convert("RGB") |
paul@49 | 333 | |
paul@50 | 334 | result = count_colours(im, 4) |
paul@50 | 335 | if result is not None: |
paul@50 | 336 | y, colours = result |
paul@56 | 337 | print "Image %s: row %d has the following colours: %s" % (output_filename, y, "; ".join([repr(c) for c in colours])) |
paul@0 | 338 | |
paul@0 | 339 | # vim: tabstop=4 expandtab shiftwidth=4 |