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@114 | 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@20 | 29 | def test(): |
paul@56 | 30 | |
paul@56 | 31 | "Generate slices of the colour cube." |
paul@56 | 32 | |
paul@20 | 33 | size = 512 |
paul@20 | 34 | for r in (0, 63, 127, 191, 255): |
paul@20 | 35 | im = PIL.Image.new("RGB", (size, size)) |
paul@20 | 36 | for g in range(0, size): |
paul@20 | 37 | for b in range(0, size): |
paul@28 | 38 | value = get_value((r, (g * 256) / size, (b * 256 / size))) |
paul@20 | 39 | im.putpixel((g, b), value) |
paul@20 | 40 | im.save("rgb%d.png" % r) |
paul@20 | 41 | |
paul@20 | 42 | def test_flat(rgb): |
paul@56 | 43 | |
paul@56 | 44 | "Generate a flat image for the colour 'rgb'." |
paul@56 | 45 | |
paul@20 | 46 | size = 64 |
paul@20 | 47 | im = PIL.Image.new("RGB", (size, size)) |
paul@20 | 48 | for y in range(0, size): |
paul@20 | 49 | for x in range(0, size): |
paul@28 | 50 | im.putpixel((x, y), get_value(rgb)) |
paul@20 | 51 | im.save("rgb%02d%02d%02d.png" % rgb) |
paul@20 | 52 | |
paul@50 | 53 | def rotate_and_scale(exif, im, width, height, rotate): |
paul@56 | 54 | |
paul@56 | 55 | """ |
paul@56 | 56 | Using the given 'exif' information, rotate and scale image 'im' given the |
paul@56 | 57 | indicated 'width' and 'height' constraints and any explicit 'rotate' |
paul@56 | 58 | indication. The returned image will be within the given 'width' and |
paul@56 | 59 | 'height', filling either or both, and preserve its original aspect ratio. |
paul@56 | 60 | """ |
paul@56 | 61 | |
paul@50 | 62 | if rotate or exif and exif["Image Orientation"].values == [6L]: |
paul@11 | 63 | im = im.rotate(270) |
paul@19 | 64 | |
paul@4 | 65 | w, h = im.size |
paul@19 | 66 | if w > h: |
paul@19 | 67 | height = (width * h) / w |
paul@19 | 68 | else: |
paul@19 | 69 | width = (height * w) / h |
paul@19 | 70 | |
paul@20 | 71 | return im.resize((width, height)) |
paul@1 | 72 | |
paul@104 | 73 | def get_parameter(options, flag, conversion, default, missing): |
paul@104 | 74 | |
paul@104 | 75 | """ |
paul@104 | 76 | From 'options', return any parameter following the given 'flag', applying |
paul@104 | 77 | the 'conversion' which has the given 'default' if no valid parameter is |
paul@104 | 78 | found, or returning the given 'missing' value if the flag does not appear at |
paul@104 | 79 | all. |
paul@104 | 80 | """ |
paul@104 | 81 | |
paul@65 | 82 | try: |
paul@65 | 83 | i = options.index(flag) |
paul@100 | 84 | try: |
paul@104 | 85 | return conversion(options[i+1]) |
paul@100 | 86 | except (IndexError, ValueError): |
paul@104 | 87 | return default |
paul@65 | 88 | except ValueError: |
paul@104 | 89 | return missing |
paul@65 | 90 | |
paul@56 | 91 | # Main program. |
paul@56 | 92 | |
paul@20 | 93 | if __name__ == "__main__": |
paul@56 | 94 | |
paul@56 | 95 | # Test options. |
paul@56 | 96 | |
paul@20 | 97 | if "--test" in sys.argv: |
paul@20 | 98 | test() |
paul@20 | 99 | sys.exit(0) |
paul@20 | 100 | elif "--test-flat" in sys.argv: |
paul@20 | 101 | test_flat((120, 40, 60)) |
paul@20 | 102 | sys.exit(0) |
paul@124 | 103 | elif "--help" in sys.argv or len(sys.argv) < 3: |
paul@124 | 104 | basename = split(sys.argv[0])[1] |
paul@56 | 105 | print >>sys.stderr, """\ |
paul@124 | 106 | Usage: |
paul@124 | 107 | |
paul@124 | 108 | %s <input filename> <output filename> [ <options> ] |
paul@124 | 109 | |
paul@124 | 110 | %s -v <filename> [ -C <number of colours> ] |
paul@56 | 111 | |
paul@56 | 112 | Options are... |
paul@56 | 113 | |
paul@104 | 114 | -W - Indicate the output width (default is 320) |
paul@104 | 115 | -C - Number of colours per scanline (default is 4) |
paul@120 | 116 | -A - Produce an output image with the same aspect ratio as the input |
paul@120 | 117 | (useful for previewing) |
paul@104 | 118 | |
paul@104 | 119 | -s - Saturate the input image (optional float, 1.0 if unspecified) |
paul@104 | 120 | -d - Desaturate the input image (optional float, 1.0 if unspecified) |
paul@104 | 121 | -D - Darken the input image (optional float, 1.0 if unspecified) |
paul@104 | 122 | -B - Brighten the input image (optional float, 1.0 if unspecified) |
paul@56 | 123 | |
paul@117 | 124 | -l - Use colours producing the least error |
paul@117 | 125 | (slower but useful for fewer than 4 colours) |
paul@117 | 126 | |
paul@117 | 127 | -r - Rotate the input image clockwise explicitly |
paul@117 | 128 | (EXIF information is used otherwise) |
paul@56 | 129 | -p - Generate a separate preview image |
paul@56 | 130 | -h - Make the preview image with half horizontal resolution (MODE 2) |
paul@56 | 131 | -v - Verify the output image (loaded if -n is given) |
paul@56 | 132 | -n - Generate no output image |
paul@117 | 133 | |
paul@124 | 134 | Specifying -v instead of input filename permits the verification of |
paul@124 | 135 | previously-generated images. Doing so causes all other options except for -C |
paul@124 | 136 | to be ignored. |
paul@124 | 137 | """ % (basename, basename) |
paul@56 | 138 | sys.exit(1) |
paul@0 | 139 | |
paul@120 | 140 | base_width = width = 320 |
paul@120 | 141 | base_height = height = 256 |
paul@20 | 142 | |
paul@20 | 143 | input_filename, output_filename = sys.argv[1:3] |
paul@26 | 144 | basename, ext = splitext(output_filename) |
paul@35 | 145 | preview_filename = "".join([basename + "_preview", ext]) |
paul@26 | 146 | |
paul@124 | 147 | verify_only = input_filename == "-v" |
paul@50 | 148 | options = sys.argv[3:] |
paul@1 | 149 | |
paul@104 | 150 | # Basic image properties. |
paul@104 | 151 | |
paul@104 | 152 | width = get_parameter(options, "-W", int, base_width, base_width) |
paul@104 | 153 | number_of_colours = get_parameter(options, "-C", int, 4, 4) |
paul@120 | 154 | preserve_aspect_ratio = "-A" in options |
paul@120 | 155 | |
paul@120 | 156 | scale_factor = float(width) / base_width |
paul@120 | 157 | height = int(base_height * scale_factor) |
paul@56 | 158 | |
paul@104 | 159 | # Preprocessing options that employ parameters. |
paul@104 | 160 | |
paul@104 | 161 | saturate = get_parameter(options, "-s", float, 1.0, 0.0) |
paul@104 | 162 | desaturate = get_parameter(options, "-d", float, 1.0, 0.0) |
paul@104 | 163 | darken = get_parameter(options, "-D", float, 1.0, 0.0) |
paul@104 | 164 | brighten = get_parameter(options, "-B", float, 1.0, 0.0) |
paul@64 | 165 | |
paul@56 | 166 | # General output options. |
paul@56 | 167 | |
paul@124 | 168 | no_normal_output = "-n" in options or verify_only |
paul@124 | 169 | verify = "-v" in options or verify_only |
paul@124 | 170 | |
paul@124 | 171 | rotate = "-r" in options and not verify_only |
paul@124 | 172 | preview = "-p" in options and not verify_only |
paul@124 | 173 | |
paul@124 | 174 | half_resolution_preview = "-h" in options |
paul@117 | 175 | least_error = "-l" in options |
paul@124 | 176 | |
paul@50 | 177 | make_image = not no_normal_output |
paul@1 | 178 | |
paul@56 | 179 | # Load the input image if requested. |
paul@56 | 180 | |
paul@50 | 181 | if make_image or preview: |
paul@50 | 182 | exif = EXIF.process_file(open(input_filename)) |
paul@50 | 183 | im = PIL.Image.open(input_filename).convert("RGB") |
paul@120 | 184 | im = rotate_and_scale(exif, im, width, height, rotate) |
paul@120 | 185 | image_width, image_height = im.size |
paul@104 | 186 | |
paul@120 | 187 | # Scale images to the appropriate height. |
paul@104 | 188 | |
paul@120 | 189 | if scale_factor != 1: |
paul@120 | 190 | im = im.resize((image_width, int(image_height / scale_factor))) |
paul@50 | 191 | |
paul@87 | 192 | sim = SimpleImage(list(im.getdata()), im.size) |
paul@87 | 193 | process_image(sim, saturate, desaturate, darken, brighten) |
paul@87 | 194 | im.putdata(sim.getdata()) |
paul@10 | 195 | |
paul@56 | 196 | # Generate a preview if requested. |
paul@56 | 197 | |
paul@35 | 198 | if preview: |
paul@104 | 199 | imp = im.copy() |
paul@104 | 200 | if half_resolution_preview: |
paul@120 | 201 | imp = imp.resize((image_width / 2, image_height)) |
paul@106 | 202 | sim = SimpleImage(list(imp.getdata()), imp.size) |
paul@106 | 203 | convert_image(sim, 8) |
paul@106 | 204 | imp.putdata(sim.getdata()) |
paul@104 | 205 | if half_resolution_preview: |
paul@120 | 206 | imp = imp.resize((image_width, image_height)) |
paul@120 | 207 | |
paul@120 | 208 | # Scale images to a height determined by the aspect ratio. |
paul@120 | 209 | |
paul@120 | 210 | if preserve_aspect_ratio and scale_factor != 1: |
paul@120 | 211 | imp = imp.resize((image_width, image_height)) |
paul@120 | 212 | |
paul@104 | 213 | imp.save(preview_filename) |
paul@35 | 214 | |
paul@56 | 215 | # Generate an output image if requested. |
paul@56 | 216 | |
paul@50 | 217 | if make_image: |
paul@91 | 218 | sim = SimpleImage(list(im.getdata()), im.size) |
paul@119 | 219 | convert_image(sim, number_of_colours, least_error) |
paul@91 | 220 | im.putdata(sim.getdata()) |
paul@120 | 221 | |
paul@120 | 222 | # Scale images to a height determined by the aspect ratio. |
paul@120 | 223 | |
paul@120 | 224 | if preserve_aspect_ratio and scale_factor != 1: |
paul@120 | 225 | im = im.resize((image_width, image_height)) |
paul@120 | 226 | |
paul@50 | 227 | im.save(output_filename) |
paul@1 | 228 | |
paul@56 | 229 | # Verify the output image (which may be loaded) if requested. |
paul@56 | 230 | |
paul@50 | 231 | if verify: |
paul@50 | 232 | if no_normal_output: |
paul@50 | 233 | im = PIL.Image.open(output_filename).convert("RGB") |
paul@49 | 234 | |
paul@87 | 235 | im = SimpleImage(list(im.getdata()), im.size) |
paul@104 | 236 | result = count_colours(im, number_of_colours) |
paul@50 | 237 | if result is not None: |
paul@50 | 238 | y, colours = result |
paul@56 | 239 | print "Image %s: row %d has the following colours: %s" % (output_filename, y, "; ".join([repr(c) for c in colours])) |
paul@0 | 240 | |
paul@0 | 241 | # vim: tabstop=4 expandtab shiftwidth=4 |