Silverbelt commited on
Commit
b3e34bd
1 Parent(s): f31379f

script to generate icat projects to compare the images

Browse files
scripts/generate-icat-comparison-projects.py ADDED
@@ -0,0 +1,212 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from PIL import Image
2
+ import os
3
+ import re
4
+ import sys
5
+ import json
6
+ import uuid
7
+ import struct
8
+ from collections import defaultdict
9
+
10
+ def get_png_dimensions(file_path):
11
+ """
12
+ Returns the (width, height) of a PNG image by reading the file's metadata.
13
+ """
14
+ with open(file_path, 'rb') as f:
15
+ # PNG files start with an 8-byte signature
16
+ signature = f.read(8)
17
+
18
+ # Check if the file is a valid PNG by checking the signature
19
+ if signature != b'\x89PNG\r\n\x1a\n':
20
+ raise ValueError("Not a valid PNG file")
21
+
22
+ # The IHDR chunk starts after the 8-byte signature and 4-byte length field
23
+ f.read(4) # Skip the length of the IHDR chunk
24
+
25
+ # Ensure we're reading the IHDR chunk
26
+ ihdr_chunk = f.read(4)
27
+ if ihdr_chunk != b'IHDR':
28
+ raise ValueError("Invalid PNG IHDR chunk")
29
+
30
+ # Read width and height from the IHDR chunk (8 bytes total)
31
+ width, height = struct.unpack('>II', f.read(8))
32
+
33
+ return width, height
34
+
35
+ # Get the directory of the current script
36
+ script_dir = os.path.dirname(os.path.abspath(__file__))
37
+
38
+ # Construct the full path to the target directory
39
+ base_dir = os.path.join(script_dir, '..', 'result', 'flux-dev')
40
+
41
+ output_dir = os.path.join(script_dir, '..', 'output/icat_projects')
42
+
43
+ if not os.path.exists(base_dir):
44
+ print(f"The path {base_dir} does not exist!")
45
+ sys.exit(1) # Exit the program with a non-zero status to indicate an error
46
+ else:
47
+ print(f"Searching for images in {base_dir}.")
48
+
49
+ os.makedirs(output_dir, exist_ok=True)
50
+
51
+ # Regular expression to match the five-digit number in the file names
52
+ pattern = re.compile(r'\d{5}')
53
+
54
+ # Dictionary to store file groups
55
+ file_groups = defaultdict(list)
56
+
57
+ # Walk through all subdirectories and files
58
+ for root, dirs, files in os.walk(base_dir):
59
+ for file in files:
60
+ # Search for the five-digit number in the file name
61
+ match = pattern.search(file)
62
+ if match:
63
+ # Get the five-digit number
64
+ number = match.group()
65
+ # Add the file (with its full path) to the corresponding group
66
+ file_groups[number].append(os.path.join(root, file))
67
+
68
+ # JSON structure template
69
+ json_structure = {
70
+ "uiVisibility": {
71
+ "antiAliasSelect": True,
72
+ "fullscreenButton": True,
73
+ "gridSelect": True,
74
+ "helpButton": True,
75
+ "layersButton": True,
76
+ "layoutTabs": True,
77
+ "loopButton": True,
78
+ "mediaInfo": True,
79
+ "pixelDataButton": False,
80
+ "progressBar": True,
81
+ "setSelect": False,
82
+ "speedSelect": True,
83
+ "startEndTime": True,
84
+ "timeDisplay": True,
85
+ "titleBar": True,
86
+ "videoControls": True,
87
+ "zoomSelect": True,
88
+ "contentSidebar": True,
89
+ "timeline": True,
90
+ "controlBar": True,
91
+ "addFilterBtn": True,
92
+ "addLogoBtn": True,
93
+ "inOutSlider": True,
94
+ "timelineInOutSlider": False,
95
+ "appSettingsBtn": True
96
+ },
97
+ "sets": []
98
+ }
99
+
100
+ # Fill JSON structure with grouped files
101
+ for number, group in file_groups.items():
102
+ media_files = []
103
+ media_order = []
104
+
105
+ for file_path in group:
106
+ file_name = os.path.basename(file_path)
107
+ base_name = file_name.rsplit('.', 1)[0]
108
+ hex_id = uuid.uuid4().hex # Generate a unique hex ID for each file
109
+ cleaned_path = os.path.abspath(file_path).replace('\\', '%5C').replace(':', '%3A')
110
+ width, height = get_png_dimensions(file_path)
111
+
112
+ # Example media entry
113
+ media_entry = {
114
+ "id": hex_id,
115
+ "created": int(os.path.getctime(file_path) * 1000),
116
+ "codec": "PNG",
117
+ "bitrate": 0,
118
+ "width": width,
119
+ "height": height,
120
+ "type": "image",
121
+ "fps": 0,
122
+ "duration": 0,
123
+ "size": os.path.getsize(file_path),
124
+ "modified": int(os.path.getmtime(file_path) * 1000),
125
+ "hasAudio": False,
126
+ "timestamps": [],
127
+ "path": os.path.abspath(file_path).replace("/", "\\"),
128
+ "name": file_name,
129
+ "base": base_name,
130
+ "originalBase": base_name,
131
+ "extension": file_name.split('.')[-1],
132
+ "format": "PNG",
133
+ "x": 0,
134
+ "y": 0,
135
+ "z": 100,
136
+ "startTime": 0,
137
+ "thumbnails": [],
138
+ "labelVisibility": {
139
+ "codec": False,
140
+ "bitrate": False,
141
+ "dimensions": False,
142
+ "format": False,
143
+ "fps": False,
144
+ "duration": False,
145
+ "startTime": False
146
+ },
147
+ "muted": True,
148
+ "visible": True,
149
+ "shaderId": "",
150
+ "shaderInputs": [],
151
+ "shaderSettings": {},
152
+ "shaderSource": "",
153
+ "showShader": True,
154
+ "src": f"icat://localhost/?src={cleaned_path}",
155
+ "mediaOverlay": "",
156
+ "frameImages": [],
157
+ "showFrameImages": True,
158
+ "infoTexts": [],
159
+ "showInfoText": False
160
+ }
161
+
162
+ media_files.append(media_entry)
163
+ media_order.append(hex_id)
164
+
165
+ # Add the set entry to JSON structure
166
+ set_id = str(uuid.uuid4().hex)
167
+ json_structure["activeSet"] = set_id
168
+ json_structure["sets"].append({
169
+ "id": set_id,
170
+ "created": 1724539610545,
171
+ "label": f"Einstellen {len(json_structure['sets']) + 1}",
172
+ "antiAlias": False,
173
+ "endTime": 1e+29,
174
+ "grid": 0,
175
+ "layout": "split",
176
+ "overlayOrder": [],
177
+ "looping": True,
178
+ "boomerang": False,
179
+ "mediaOrder": media_order,
180
+ "x": 2170.7728827197748,
181
+ "y": 0,
182
+ "z": 30.340576171875,
183
+ "speed": 1,
184
+ "splitposition": 0.5,
185
+ "startTime": 0,
186
+ "time": 0,
187
+ "viewport": {
188
+ "top": 93.17708587646484,
189
+ "left": 0,
190
+ "paneWidth": 2560,
191
+ "paneHeight": 932.0625,
192
+ "height": 932.0625,
193
+ "width": 2560,
194
+ "minZoom": 30.340576171875,
195
+ "fitScale": 0.30340576171875,
196
+ "fitWidth": 4096,
197
+ "fitHeight": 3072,
198
+ "fitPaneWidth": 4096,
199
+ "fitPaneHeight": 3072,
200
+ "contentWidth": 4096,
201
+ "contentHeight": 3072
202
+ },
203
+ "gamma": 1,
204
+ "exposure": 1,
205
+ "media": media_files,
206
+ "overlays": []
207
+ })
208
+
209
+ json_filename = os.path.join(output_dir, f"image_comparison_{number}.json")
210
+ with open(json_filename, 'w') as json_file:
211
+ json.dump(json_structure, json_file, indent=4)
212
+ print(f"JSON-Datei für Gruppe {number} wurde geschrieben: {json_filename}")