File size: 1,684 Bytes
c8a6842
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#!/usr/bin/env python3
"""Fake pixel art -> real pixel art (Retro Diffusion pixel-art-fixer, MIT, vendor/pixelfixer).

  python3 scripts/pixelfix.py in.png out.png [--mode full|fast] [--step 8] [--colors 0]

Finds the pseudo-pixel grid of an upscaled / AI-generated "pixel art" image and
rebuilds every cell as one true pixel. Prints a JSON line: cols, rows, step_x,
step_y, consensus, confidence, timings. Needs numpy, scipy, opencv, Pillow.
"""
import argparse
import json
import os
import sys

sys.path.insert(0, os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))), 'vendor', 'pixelfixer'))


def main():
    ap = argparse.ArgumentParser(description=__doc__.splitlines()[0])
    ap.add_argument('input')
    ap.add_argument('output')
    ap.add_argument('--mode', choices=['full', 'fast'], default='full')
    ap.add_argument('--step', type=float, default=None, help='force the cell size in source pixels')
    ap.add_argument('--colors', type=int, default=0, help='structure quantisation colours (0 = adaptive)')
    args = ap.parse_args()
    try:
        from pixelfixer.api import process
    except ImportError as err:
        print(json.dumps({'error': f'pixelfixer needs numpy, scipy, opencv-python-headless and Pillow: {err}'}))
        sys.exit(2)
    with open(args.input, 'rb') as f:
        data = f.read()
    try:
        result = process(data, mode=args.mode, force_step=args.step, k_colors=args.colors)
    except ValueError as err:
        print(json.dumps({'error': str(err)}))
        sys.exit(1)
    with open(args.output, 'wb') as f:
        f.write(result.pop('png'))
    print(json.dumps(result))


if __name__ == '__main__':
    main()