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