forked from DownrightNifty/computercraft-stuff
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconvert_nfp.py
More file actions
77 lines (72 loc) · 3.12 KB
/
Copy pathconvert_nfp.py
File metadata and controls
77 lines (72 loc) · 3.12 KB
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#!/usr/bin/python
# -*- coding: utf-8 -*-
from PIL import Image
import nfp
import argparse
import os
# default resize width/height when converting image -> nfp
DEFAULT_WIDTH, DEFAULT_HEIGHT = 164, 81
desc = (
"Convert standard image files to ComputerCraft nfp files, and vice "
"versa. Input file type is identified by extension (.nfp, .jpg, etc.), "
"and output files use the input filename with a new extension."
)
files_help = "input files, nfp or image (must have correct file extension)"
nfp_desc = "optional arguments when converting image -> nfp"
skip_help = "skip default behavior of resizing image before conversion"
width_help = "if resizing, new width (default: {})".format(DEFAULT_WIDTH)
height_help = "if resizing, new height (default: {})".format(DEFAULT_HEIGHT)
im_desc = "optional arguments when converting nfp -> image"
format_help = (
"output format passed to Image.save() (also output file extension "
"unless -e argument specified), see PIL docs for supported formats, "
"default: PNG"
)
ext_help = (
"if specified, will be used as the output file extension instead of "
"FORMAT"
)
rm_help = "remove the original image after converting"
dither_help = "enables dithering"
parser = argparse.ArgumentParser(description=desc)
parser.add_argument("files", help=files_help, nargs='+')
nfp_group = parser.add_argument_group("nfp arguments", description=nfp_desc)
nfp_group.add_argument("--skip-resize", "-s", help=skip_help,
action="store_true", default=False)
nfp_group.add_argument("--resize-width", "-w", help=width_help,
metavar="WIDTH", type=int, default=DEFAULT_WIDTH)
nfp_group.add_argument("--resize-height", "-H", help=height_help,
metavar="HEIGHT", type=int, default=DEFAULT_HEIGHT)
im_group = parser.add_argument_group("image arguments", description=im_desc)
im_group.add_argument("--format", "-f", help=format_help, metavar="FORMAT",
dest="f_format", default="PNG")
im_group.add_argument("--extension", "-e", help=ext_help)
im_group.add_argument("--remove", "-r", help=rm_help, action="store_true")
im_group.add_argument("--dither", "-d", help=dither_help, action="store_true")
args = parser.parse_args()
for file in args.files:
filename, ext = os.path.splitext(file)
if not ext:
parser.error("filename must have appropriate extension")
if ext.upper() == ".NFP":
with open(file, "rt") as f:
nfp_file = f.read()
im = nfp.nfp_to_img(nfp_file)
new_ext = args.f_format.replace(" ", "").lower()
if args.extension:
new_ext = args.extension
im.save("{}.{}".format(filename, new_ext), args.f_format)
else:
im = Image.open(file)
if args.skip_resize:
nfp_file = nfp.img_to_nfp(im, dither=1 if args.dither else 0)
else:
nfp_file = nfp.img_to_nfp(
im,
(args.resize_width, args.resize_height),
dither=1 if args.dither else 0
)
with open("{}.nfp".format(filename), "wt") as f:
f.write(nfp_file)
if args.remove:
os.remove(file)