2020-12-29 18:24:29 +00:00
|
|
|
import argparse
|
2021-01-08 22:44:28 +00:00
|
|
|
import os.path
|
2021-01-12 10:00:56 +00:00
|
|
|
import time
|
2020-12-29 18:24:29 +00:00
|
|
|
|
2021-01-03 22:32:04 +00:00
|
|
|
from PIL import Image
|
2020-12-29 18:24:29 +00:00
|
|
|
import numpy as np
|
2021-01-15 22:18:25 +00:00
|
|
|
|
2021-01-15 22:20:28 +00:00
|
|
|
import dither as dither_pyx
|
2021-01-15 22:18:25 +00:00
|
|
|
import dither_pattern
|
|
|
|
import image as image_py
|
|
|
|
import palette as palette_py
|
|
|
|
import screen as screen_py
|
2021-01-03 23:23:15 +00:00
|
|
|
|
2021-01-11 20:21:00 +00:00
|
|
|
|
2020-12-29 21:03:17 +00:00
|
|
|
# TODO:
|
2021-01-04 21:08:29 +00:00
|
|
|
# - only lookahead for 560px
|
2020-12-30 10:27:33 +00:00
|
|
|
# - compare to bmp2dhr and a2bestpix
|
2021-01-03 22:32:04 +00:00
|
|
|
|
2020-12-30 10:27:33 +00:00
|
|
|
|
2020-12-29 18:24:29 +00:00
|
|
|
def main():
|
|
|
|
parser = argparse.ArgumentParser()
|
|
|
|
parser.add_argument("input", type=str, help="Input file to process")
|
2020-12-30 10:27:33 +00:00
|
|
|
parser.add_argument("output", type=str, help="Output file for ")
|
2021-01-08 22:44:28 +00:00
|
|
|
parser.add_argument(
|
|
|
|
"--lookahead", type=int, default=4,
|
|
|
|
help=("How many pixels to look ahead to compensate for NTSC colour "
|
|
|
|
"artifacts."))
|
2021-01-15 22:25:06 +00:00
|
|
|
parser.add_argument('--dither', type=str,
|
|
|
|
choices=list(dither_pattern.PATTERNS.keys()),
|
|
|
|
default=dither_pattern.DEFAULT_PATTERN)
|
2021-01-08 22:44:28 +00:00
|
|
|
args = parser.parse_args()
|
|
|
|
|
2021-01-15 22:18:25 +00:00
|
|
|
palette = palette_py.Palette()
|
2021-01-03 23:23:15 +00:00
|
|
|
# screen = DHGR140Screen()
|
2021-01-15 22:18:25 +00:00
|
|
|
screen = screen_py.DHGR560Screen(palette)
|
2020-12-30 10:27:33 +00:00
|
|
|
|
2021-01-15 22:18:25 +00:00
|
|
|
image = image_py.open(screen.X_RES, screen.Y_RES, args.input)
|
2021-01-10 16:06:14 +00:00
|
|
|
# image_rgb.show()
|
2021-01-03 22:32:04 +00:00
|
|
|
|
2021-01-15 22:25:06 +00:00
|
|
|
dither = dither_pattern.PATTERNS[args.dither]()
|
2020-12-30 10:27:33 +00:00
|
|
|
|
2021-01-12 10:00:56 +00:00
|
|
|
start = time.time()
|
2021-01-15 22:20:28 +00:00
|
|
|
output_4bit, output_rgb = dither_pyx.dither_image(
|
2021-01-15 22:18:25 +00:00
|
|
|
screen, image, dither, lookahead=args.lookahead)
|
2021-01-12 10:00:56 +00:00
|
|
|
print(time.time() - start)
|
2021-01-10 16:06:14 +00:00
|
|
|
screen.pack(output_4bit)
|
2020-12-30 10:27:33 +00:00
|
|
|
|
2021-01-15 22:18:25 +00:00
|
|
|
out_image = Image.fromarray(image_py.linear_to_srgb(output_rgb).astype(
|
|
|
|
np.uint8))
|
2021-01-08 22:44:28 +00:00
|
|
|
outfile = os.path.join(os.path.splitext(args.output)[0] + ".png")
|
|
|
|
out_image.save(outfile, "PNG")
|
|
|
|
out_image.show(title=outfile)
|
2021-01-04 21:08:29 +00:00
|
|
|
# bitmap = Image.fromarray(screen.bitmap.astype('uint8') * 255)
|
|
|
|
|
2021-01-03 22:32:04 +00:00
|
|
|
with open(args.output, "wb") as f:
|
2021-01-03 23:23:15 +00:00
|
|
|
f.write(bytes(screen.main))
|
|
|
|
f.write(bytes(screen.aux))
|
2020-12-29 18:24:29 +00:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2021-01-09 18:05:36 +00:00
|
|
|
main()
|