paul@71 | 1 | #!/usr/bin/env python |
paul@71 | 2 | |
paul@71 | 3 | """ |
paul@71 | 4 | Convert and optimise images for display in an Acorn Electron MODE 1 variant |
paul@71 | 5 | with four colours per line but eight colours available for selection on each |
paul@71 | 6 | line. |
paul@71 | 7 | |
paul@71 | 8 | Copyright (C) 2015 Paul Boddie <paul@boddie.org.uk> |
paul@71 | 9 | |
paul@71 | 10 | This program is free software; you can redistribute it and/or modify it under |
paul@71 | 11 | the terms of the GNU General Public License as published by the Free Software |
paul@71 | 12 | Foundation; either version 3 of the License, or (at your option) any later |
paul@71 | 13 | version. |
paul@71 | 14 | |
paul@71 | 15 | This program is distributed in the hope that it will be useful, but WITHOUT ANY |
paul@71 | 16 | WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A |
paul@71 | 17 | PARTICULAR PURPOSE. See the GNU General Public License for more details. |
paul@71 | 18 | |
paul@71 | 19 | You should have received a copy of the GNU General Public License along |
paul@71 | 20 | with this program. If not, see <http://www.gnu.org/licenses/>. |
paul@71 | 21 | """ |
paul@71 | 22 | |
paul@71 | 23 | from optimiser import * |
paul@71 | 24 | from os.path import split, splitext |
paul@71 | 25 | import EXIF |
paul@71 | 26 | import PIL.Image |
paul@71 | 27 | import sys |
paul@71 | 28 | |
paul@71 | 29 | def rotate_and_scale(exif, im, width, height, rotate): |
paul@71 | 30 | |
paul@71 | 31 | """ |
paul@71 | 32 | Using the given 'exif' information, rotate and scale image 'im' given the |
paul@71 | 33 | indicated 'width' and 'height' constraints and any explicit 'rotate' |
paul@71 | 34 | indication. The returned image will be within the given 'width' and |
paul@71 | 35 | 'height', filling either or both, and preserve its original aspect ratio. |
paul@71 | 36 | """ |
paul@71 | 37 | |
paul@71 | 38 | if rotate or exif and exif["Image Orientation"].values == [6L]: |
paul@71 | 39 | im = im.rotate(270) |
paul@71 | 40 | |
paul@71 | 41 | w, h = im.size |
paul@71 | 42 | if w > h: |
paul@71 | 43 | height = (width * h) / w |
paul@71 | 44 | else: |
paul@71 | 45 | width = (height * w) / h |
paul@71 | 46 | |
paul@71 | 47 | return im.resize((width, height)) |
paul@71 | 48 | |
paul@71 | 49 | def test(): |
paul@71 | 50 | |
paul@71 | 51 | "Generate slices of the colour cube." |
paul@71 | 52 | |
paul@71 | 53 | size = 64 |
paul@71 | 54 | for r in (0, 63, 127, 191, 255): |
paul@71 | 55 | pim = PIL.Image.new("RGB", (size, size)) |
paul@71 | 56 | im = SimpleImage(list(pim.getdata()), pim.size) |
paul@71 | 57 | test_slice(im, size, r) |
paul@71 | 58 | pim.putdata(im.getdata()) |
paul@71 | 59 | pim.save("rgb%d.png" % r) |
paul@71 | 60 | |
paul@71 | 61 | def test_flat(rgb): |
paul@71 | 62 | |
paul@71 | 63 | "Generate a flat image for the colour 'rgb'." |
paul@71 | 64 | |
paul@71 | 65 | size = 64 |
paul@71 | 66 | pim = PIL.Image.new("RGB", (size, size)) |
paul@71 | 67 | im = SimpleImage(list(pim.getdata()), pim.size) |
paul@71 | 68 | test_flat_slice(im, size, rgb) |
paul@71 | 69 | pim.putdata(im.getdata()) |
paul@71 | 70 | pim.save("rgb%02d%02d%02d.png" % rgb) |
paul@71 | 71 | |
paul@71 | 72 | def get_float(options, flag): |
paul@71 | 73 | try: |
paul@71 | 74 | i = options.index(flag) |
paul@71 | 75 | if i+1 < len(options) and options[i+1].isdigit(): |
paul@71 | 76 | return float(options[i+1]) |
paul@71 | 77 | else: |
paul@71 | 78 | return 1.0 |
paul@71 | 79 | except ValueError: |
paul@71 | 80 | return 0.0 |
paul@71 | 81 | |
paul@71 | 82 | # Main program. |
paul@71 | 83 | |
paul@71 | 84 | if __name__ == "__main__": |
paul@71 | 85 | |
paul@71 | 86 | # Test options. |
paul@71 | 87 | |
paul@71 | 88 | if "--test" in sys.argv: |
paul@71 | 89 | test() |
paul@71 | 90 | sys.exit(0) |
paul@71 | 91 | elif "--test-flat" in sys.argv: |
paul@71 | 92 | test_flat((120, 40, 60)) |
paul@71 | 93 | sys.exit(0) |
paul@71 | 94 | elif "--help" in sys.argv: |
paul@71 | 95 | print >>sys.stderr, """\ |
paul@71 | 96 | Usage: %s <input filename> <output filename> [ <options> ] |
paul@71 | 97 | |
paul@71 | 98 | Options are... |
paul@71 | 99 | |
paul@71 | 100 | -s - Saturate the input image (can be followed by a float, default 1.0) |
paul@71 | 101 | -d - Desaturate the input image (can be followed by a float, default 1.0) |
paul@71 | 102 | -D - Darken the input image (can be followed by a float, default 1.0) |
paul@71 | 103 | -B - Brighten the input image (can be followed by a float, default 1.0) |
paul@71 | 104 | |
paul@71 | 105 | -r - Rotate the input image clockwise |
paul@71 | 106 | -p - Generate a separate preview image |
paul@71 | 107 | -h - Make the preview image with half horizontal resolution (MODE 2) |
paul@71 | 108 | -v - Verify the output image (loaded if -n is given) |
paul@71 | 109 | -n - Generate no output image |
paul@71 | 110 | """ % split(sys.argv[0])[1] |
paul@71 | 111 | sys.exit(1) |
paul@71 | 112 | |
paul@71 | 113 | width = 320 |
paul@71 | 114 | height = 256 |
paul@71 | 115 | |
paul@71 | 116 | input_filename, output_filename = sys.argv[1:3] |
paul@71 | 117 | basename, ext = splitext(output_filename) |
paul@71 | 118 | preview_filename = "".join([basename + "_preview", ext]) |
paul@71 | 119 | |
paul@71 | 120 | options = sys.argv[3:] |
paul@71 | 121 | |
paul@71 | 122 | # Preprocessing options that can be repeated for extra effect. |
paul@71 | 123 | |
paul@71 | 124 | saturate = get_float(options, "-s") |
paul@71 | 125 | desaturate = get_float(options, "-d") |
paul@71 | 126 | darken = get_float(options, "-D") |
paul@71 | 127 | brighten = get_float(options, "-B") |
paul@71 | 128 | |
paul@71 | 129 | # General output options. |
paul@71 | 130 | |
paul@71 | 131 | rotate = "-r" in options |
paul@71 | 132 | preview = "-p" in options |
paul@71 | 133 | half_resolution_preview = "-h" in options |
paul@71 | 134 | verify = "-v" in options |
paul@71 | 135 | no_normal_output = "-n" in options |
paul@71 | 136 | make_image = not no_normal_output |
paul@71 | 137 | |
paul@71 | 138 | # Load the input image if requested. |
paul@71 | 139 | |
paul@71 | 140 | if make_image or preview: |
paul@71 | 141 | exif = EXIF.process_file(open(input_filename)) |
paul@71 | 142 | pim = PIL.Image.open(input_filename).convert("RGB") |
paul@71 | 143 | pim = rotate_and_scale(exif, pim, width, height, rotate) |
paul@71 | 144 | im = SimpleImage(list(pim.getdata()), pim.size) |
paul@71 | 145 | process_image(im, saturate, desaturate, darken, brighten) |
paul@71 | 146 | |
paul@71 | 147 | # Generate a preview if requested. |
paul@71 | 148 | |
paul@71 | 149 | if preview: |
paul@71 | 150 | imp = preview_image(im, half_resolution_preview) |
paul@71 | 151 | pimp = pim.copy() |
paul@71 | 152 | pimp.putdata(imp.getdata()) |
paul@71 | 153 | pimp.save(preview_filename) |
paul@71 | 154 | |
paul@71 | 155 | # Generate an output image if requested. |
paul@71 | 156 | |
paul@71 | 157 | if make_image: |
paul@71 | 158 | convert_image(im) |
paul@71 | 159 | pim.putdata(im.getdata()) |
paul@71 | 160 | pim.save(output_filename) |
paul@71 | 161 | |
paul@71 | 162 | # Verify the output image (which may be loaded) if requested. |
paul@71 | 163 | |
paul@71 | 164 | if verify: |
paul@71 | 165 | if no_normal_output: |
paul@71 | 166 | pim = PIL.Image.open(output_filename).convert("RGB") |
paul@71 | 167 | im = SimpleImage(list(pim.getdata()), pim.size) |
paul@71 | 168 | |
paul@71 | 169 | result = count_colours(im, 4) |
paul@71 | 170 | if result is not None: |
paul@71 | 171 | y, colours = result |
paul@71 | 172 | print "Image %s: row %d has the following colours: %s" % (output_filename, y, "; ".join([repr(c) for c in colours])) |
paul@71 | 173 | |
paul@71 | 174 | # vim: tabstop=4 expandtab shiftwidth=4 |