Turns Agnes Martin paintings into numbers
| #!/usr/bin/env python3 | |
| # for Disquiet 0456: https://llllllll.co/t/disquiet-junto-project-0456-line-up/36613/ | |
| """ | |
| martin - converts an image with horizontal lines into JSON with a list of lists of numbers | |
| Usage: | |
| martin <file> | |
| martin --help | |
| Options: | |
| -h, --help This info. | |
| """ | |
| from __future__ import annotations | |
| from typing import * | |
| import json | |
| import sys | |
| import docopt | |
| from PIL import Image | |
| if TYPE_CHECKING: | |
| Line = List[float] | |
| def gen_energy_lines(image: Image, *, threshold: int = 25) -> Iterator[Line]: | |
| energy_line = [0.0] * image.width | |
| for line in range(image.height): | |
| current_energy = [0.0] * image.width | |
| for x in range(image.width): | |
| current_energy[x] = image.getpixel((x, line)) | |
| if max(current_energy) >= threshold: | |
| for i, energy in enumerate(current_energy): | |
| energy_line[i] += (energy + 1) / 256 | |
| elif max(energy_line) >= threshold: | |
| # End of visible line in the image, emit current data and prepare a new one. | |
| yield energy_line | |
| energy_line = [0.0] * image.width | |
| if max(energy_line) > threshold: | |
| # Left-over line at the end of the image. Emit it before exiting. | |
| yield energy_line | |
| def main(file: str) -> None: | |
| with Image.open(file) as source: | |
| lines = list(gen_energy_lines(source)) | |
| json.dump(lines, sys.stdout) | |
| if __name__ == '__main__': | |
| args = docopt.docopt(__doc__) | |
| main(file=args['<file>']) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment