-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate_pixelsort.py
93 lines (81 loc) · 3.18 KB
/
generate_pixelsort.py
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
81
82
83
84
85
86
87
88
89
90
91
92
93
import os
import random
import subprocess
import sys
from datetime import datetime
# Define sorting functions
SORTING_FUNCTIONS = ["lightness", "hue", "saturation", "intensity", "minimum"]
# Define interval functions and their associated parameters with types
INTERVAL_FUNCTIONS = {
"threshold": {
"params": [
{"flag": "-t", "type": float, "range": (0.0, 1.0)}, # lower threshold
{"flag": "-u", "type": float, "range": (0.5, 1.0)} # upper threshold
]
},
"random": {
"params": [
{"flag": "-c", "type": int, "range": (10, 100)}, # characteristic length
{"flag": "-r", "type": float, "range": (0.0, 1.0)} # randomness
]
},
"waves": {
"params": [
{"flag": "-c", "type": int, "range": (10, 100)}, # characteristic length
{"flag": "-a", "type": float, "range": (0, 360)} # angle in degrees
]
},
"edges": {
"params": [
{"flag": "-t", "type": float, "range": (0.0, 1.0)} # lower threshold
]
}
}
PIXELSORT_SCRIPT = "pixelsort"
def generate_images(input_file):
base_name = os.path.splitext(os.path.basename(input_file))[0]
timestamp = datetime.now().strftime("%Y%m%d")
output_dir = f"output_images/{base_name}_{timestamp}"
if not os.path.exists(output_dir):
os.makedirs(output_dir)
for interval, details in INTERVAL_FUNCTIONS.items():
interval_folder = os.path.join(output_dir, interval)
if not os.path.exists(interval_folder):
os.makedirs(interval_folder)
for _ in range(5): # Generate 5 variations
# Prepare parameters for each interval function
params = []
for param in details["params"]:
value = random.uniform(*param["range"]) if param["type"] == float else random.randint(*param["range"])
params.extend([param["flag"], str(value)])
# Loop through all sorting functions
for sorting in SORTING_FUNCTIONS:
output_file = os.path.join(
interval_folder,
f"{base_name}_{interval}_{sorting}_{'_'.join(params)}.png"
)
# Construct the command to run pixelsort
command = [
"python", PIXELSORT_SCRIPT,
input_file,
"-o", output_file,
"-s", sorting,
"-i", interval
] + params
# Run the command
try:
print(f"Running command: {' '.join(command)}")
subprocess.run(command, check=True)
print(f"Generated: {output_file}")
except subprocess.CalledProcessError as e:
print(f"Error generating {output_file}: {e}")
if __name__ == "__main__":
if len(sys.argv) != 2:
print("Usage: python generate_pixelsort.py <input_image>")
sys.exit(1)
input_image = sys.argv[1]
if not os.path.isfile(input_image):
print(f"Error: File '{input_image}' not found.")
sys.exit(1)
generate_images(input_image)
print("Processing complete!")