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@104 | 75 | def get_parameter(options, flag, conversion, default, missing): |
paul@104 | 76 | |
paul@104 | 77 | """ |
paul@104 | 78 | From 'options', return any parameter following the given 'flag', applying |
paul@104 | 79 | the 'conversion' which has the given 'default' if no valid parameter is |
paul@104 | 80 | found, or returning the given 'missing' value if the flag does not appear at |
paul@104 | 81 | all. |
paul@104 | 82 | """ |
paul@104 | 83 | |
paul@65 | 84 | try: |
paul@65 | 85 | i = options.index(flag) |
paul@100 | 86 | try: |
paul@104 | 87 | return conversion(options[i+1]) |
paul@100 | 88 | except (IndexError, ValueError): |
paul@104 | 89 | return default |
paul@65 | 90 | except ValueError: |
paul@104 | 91 | return missing |
paul@65 | 92 | |
paul@56 | 93 | # Main program. |
paul@56 | 94 | |
paul@20 | 95 | if __name__ == "__main__": |
paul@56 | 96 | |
paul@56 | 97 | # Test options. |
paul@56 | 98 | |
paul@20 | 99 | if "--test" in sys.argv: |
paul@20 | 100 | test() |
paul@20 | 101 | sys.exit(0) |
paul@20 | 102 | elif "--test-flat" in sys.argv: |
paul@20 | 103 | test_flat((120, 40, 60)) |
paul@20 | 104 | sys.exit(0) |
paul@56 | 105 | elif "--help" in sys.argv: |
paul@56 | 106 | print >>sys.stderr, """\ |
paul@56 | 107 | Usage: %s <input filename> <output filename> [ <options> ] |
paul@56 | 108 | |
paul@56 | 109 | Options are... |
paul@56 | 110 | |
paul@104 | 111 | -W - Indicate the output width (default is 320) |
paul@104 | 112 | -C - Number of colours per scanline (default is 4) |
paul@104 | 113 | |
paul@104 | 114 | -s - Saturate the input image (optional float, 1.0 if unspecified) |
paul@104 | 115 | -d - Desaturate the input image (optional float, 1.0 if unspecified) |
paul@104 | 116 | -D - Darken the input image (optional float, 1.0 if unspecified) |
paul@104 | 117 | -B - Brighten the input image (optional float, 1.0 if unspecified) |
paul@56 | 118 | |
paul@56 | 119 | -r - Rotate the input image clockwise |
paul@56 | 120 | -p - Generate a separate preview image |
paul@56 | 121 | -h - Make the preview image with half horizontal resolution (MODE 2) |
paul@56 | 122 | -v - Verify the output image (loaded if -n is given) |
paul@56 | 123 | -n - Generate no output image |
paul@56 | 124 | """ % split(sys.argv[0])[1] |
paul@56 | 125 | sys.exit(1) |
paul@0 | 126 | |
paul@104 | 127 | base_width = 320 |
paul@20 | 128 | height = 256 |
paul@20 | 129 | |
paul@20 | 130 | input_filename, output_filename = sys.argv[1:3] |
paul@26 | 131 | basename, ext = splitext(output_filename) |
paul@35 | 132 | preview_filename = "".join([basename + "_preview", ext]) |
paul@26 | 133 | |
paul@50 | 134 | options = sys.argv[3:] |
paul@1 | 135 | |
paul@104 | 136 | # Basic image properties. |
paul@104 | 137 | |
paul@104 | 138 | width = get_parameter(options, "-W", int, base_width, base_width) |
paul@104 | 139 | number_of_colours = get_parameter(options, "-C", int, 4, 4) |
paul@56 | 140 | |
paul@104 | 141 | # Preprocessing options that employ parameters. |
paul@104 | 142 | |
paul@104 | 143 | saturate = get_parameter(options, "-s", float, 1.0, 0.0) |
paul@104 | 144 | desaturate = get_parameter(options, "-d", float, 1.0, 0.0) |
paul@104 | 145 | darken = get_parameter(options, "-D", float, 1.0, 0.0) |
paul@104 | 146 | brighten = get_parameter(options, "-B", float, 1.0, 0.0) |
paul@64 | 147 | |
paul@56 | 148 | # General output options. |
paul@56 | 149 | |
paul@56 | 150 | rotate = "-r" in options |
paul@50 | 151 | preview = "-p" in options |
paul@50 | 152 | half_resolution_preview = "-h" in options |
paul@50 | 153 | verify = "-v" in options |
paul@50 | 154 | no_normal_output = "-n" in options |
paul@50 | 155 | make_image = not no_normal_output |
paul@1 | 156 | |
paul@56 | 157 | # Load the input image if requested. |
paul@56 | 158 | |
paul@50 | 159 | if make_image or preview: |
paul@50 | 160 | exif = EXIF.process_file(open(input_filename)) |
paul@50 | 161 | im = PIL.Image.open(input_filename).convert("RGB") |
paul@104 | 162 | im = rotate_and_scale(exif, im, base_width, height, rotate) |
paul@104 | 163 | |
paul@104 | 164 | # Scale images to the appropriate width. |
paul@104 | 165 | |
paul@104 | 166 | if width != base_width: |
paul@104 | 167 | im = im.resize((width, height)) |
paul@50 | 168 | |
paul@87 | 169 | sim = SimpleImage(list(im.getdata()), im.size) |
paul@87 | 170 | process_image(sim, saturate, desaturate, darken, brighten) |
paul@87 | 171 | im.putdata(sim.getdata()) |
paul@10 | 172 | |
paul@56 | 173 | # Generate a preview if requested. |
paul@56 | 174 | |
paul@35 | 175 | if preview: |
paul@104 | 176 | imp = im.copy() |
paul@104 | 177 | if half_resolution_preview: |
paul@104 | 178 | imp = imp.resize((width / 2, height)) |
paul@106 | 179 | sim = SimpleImage(list(imp.getdata()), imp.size) |
paul@106 | 180 | convert_image(sim, 8) |
paul@106 | 181 | imp.putdata(sim.getdata()) |
paul@104 | 182 | if half_resolution_preview: |
paul@104 | 183 | imp = imp.resize((width, height)) |
paul@104 | 184 | imp.save(preview_filename) |
paul@35 | 185 | |
paul@56 | 186 | # Generate an output image if requested. |
paul@56 | 187 | |
paul@50 | 188 | if make_image: |
paul@91 | 189 | sim = SimpleImage(list(im.getdata()), im.size) |
paul@106 | 190 | convert_image(sim, number_of_colours) |
paul@91 | 191 | im.putdata(sim.getdata()) |
paul@50 | 192 | im.save(output_filename) |
paul@1 | 193 | |
paul@56 | 194 | # Verify the output image (which may be loaded) if requested. |
paul@56 | 195 | |
paul@50 | 196 | if verify: |
paul@50 | 197 | if no_normal_output: |
paul@50 | 198 | im = PIL.Image.open(output_filename).convert("RGB") |
paul@49 | 199 | |
paul@87 | 200 | im = SimpleImage(list(im.getdata()), im.size) |
paul@104 | 201 | result = count_colours(im, number_of_colours) |
paul@50 | 202 | if result is not None: |
paul@50 | 203 | y, colours = result |
paul@56 | 204 | print "Image %s: row %d has the following colours: %s" % (output_filename, y, "; ".join([repr(c) for c in colours])) |
paul@0 | 205 | |
paul@0 | 206 | # vim: tabstop=4 expandtab shiftwidth=4 |