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