-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpcxconvert.py
More file actions
80 lines (67 loc) · 2.96 KB
/
pcxconvert.py
File metadata and controls
80 lines (67 loc) · 2.96 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
78
79
80
#!/usr/bin/env python3
import sys
import os
import argparse
from PIL import Image
def process_file(input_path, output_dir=None, recreate_dirs=None):
input_path = input_path.strip()
if not input_path or not os.path.exists(input_path):
return
root_path, ext = os.path.splitext(input_path)
ext_lower = ext.lower()
# Determine output format based on input format
if ext_lower == ".pcx":
output_format = "PNG"
output_ext = ".png"
elif ext_lower == ".png":
output_format = "PCX"
output_ext = ".pcx"
else:
print(f"Error on {input_path}: Unsupported file format '{ext}'", file=sys.stderr)
return
filename_only = os.path.basename(root_path) + output_ext
if recreate_dirs:
rel_path = os.path.relpath(root_path)
output_path = os.path.join(recreate_dirs, rel_path + output_ext)
os.makedirs(os.path.dirname(output_path), exist_ok=True)
elif output_dir:
os.makedirs(output_dir, exist_ok=True)
output_path = os.path.join(output_dir, filename_only)
else:
output_path = root_path + output_ext
try:
with Image.open(input_path) as img:
# Handle transparency for any format
if img.mode in ('RGBA', 'LA'):
img = img.convert("RGBA")
bg = Image.new("RGB", img.size, (255, 255, 255))
bg.paste(img, mask=img.split()[-1])
img = bg
else:
img = img.convert("RGB")
img.save(output_path, format=output_format)
print(f"Created: {output_path}")
except Exception as e:
print(f"Error on {input_path}: {e}", file=sys.stderr)
if __name__ == "__main__":
parser = argparse.ArgumentParser(
description="Convert between PNG and PCX formats (pixel-for-pixel). Automatically detects input format and converts to the opposite.",
add_help=True
)
parser.add_argument("-o", metavar="OUTPUT PATH", help="Specify the output path for file(s).")
parser.add_argument("-d", metavar="RECREATE DIRECTORIES", help="Specify output path for file(s). Recreates subfolder structure of the input directory in this destination.")
parser.add_argument("-help", action="help", help="Show this help message and exit.")
# 'nargs="*"' allows zero or more files to be passed as direct arguments
parser.add_argument("files", nargs="*", help="Image files to convert (PNG or PCX).")
args = parser.parse_args()
# 1. Process files passed as direct arguments (e.g., pcxconvert img.png)
if args.files:
for f in args.files:
process_file(f, args.o, args.d)
# 2. Process files passed via pipe (e.g., find . | pcxconvert)
if not sys.stdin.isatty():
for line in sys.stdin:
process_file(line, args.o, args.d)
# 3. If no files were provided via either method, show the help
if not args.files and sys.stdin.isatty():
parser.print_help()