-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathresize_validation_images.py
More file actions
66 lines (48 loc) · 2.13 KB
/
resize_validation_images.py
File metadata and controls
66 lines (48 loc) · 2.13 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
#!/usr/bin/env python3
"""
Script to resize all PNG images in ./validation_data to 512x512 and save them to ./validation_data_512
"""
import os
import glob
import shutil
import json
from PIL import Image
from pathlib import Path
def resize_images():
# Source and destination directories
source_dir = "./validation_data"
dest_dir = "./validation_data_edit"
# Create destination directory if it doesn't exist
Path(dest_dir).mkdir(exist_ok=True)
# Find all PNG files in source directory
png_files = glob.glob(os.path.join(source_dir, "*.png"))
print(f"Found {len(png_files)} PNG files to resize")
for i, png_file in enumerate(png_files, 1):
try:
# Get the filename without path
filename = os.path.basename(png_file)
dest_path = os.path.join(dest_dir, filename)
if os.path.exists(dest_path):
continue
# Open the image
with Image.open(png_file) as img:
# Convert to RGB if necessary (in case of RGBA or other modes)
if img.mode != 'RGB':
img = img.convert('RGB')
# Resize to 512x512 with high-quality resampling
resized_img = img.resize((1024, 1024), Image.Resampling.LANCZOS)
# Save to destination directory
resized_img.save(dest_path, 'PNG', quality=95)
print(f"[{i}/{len(png_files)}] Resized: {filename}")
except Exception as e:
print(f"Error processing {png_file}: {str(e)}")
# Copy metadata files
metadata_files = glob.glob(os.path.join(source_dir, "*.json"))
for metadata_file in metadata_files:
prompt = json.load(open(metadata_file, "r"))["prompt"]
prompt_file_name = os.path.basename(metadata_file).split('_')[0]
with open(os.path.join(dest_dir, f'{prompt_file_name}_final.txt'), "w") as f:
f.write(prompt)
print(f"\nCompleted! All images resized and saved to {dest_dir}")
if __name__ == "__main__":
resize_images()