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