-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdownscale_imgs.py
More file actions
59 lines (46 loc) · 1.99 KB
/
Copy pathdownscale_imgs.py
File metadata and controls
59 lines (46 loc) · 1.99 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
import os
import cv2
import sys
# Downscale factor
scale_factor = 2
# Function to process and save images
def downscale_and_save_images(input_dir, output_dir, factor):
for root, dirs, files in os.walk(input_dir):
for file in files:
if file.lower().endswith(('.png', '.jpg', '.jpeg')):
# Construct full input path
img_path = os.path.join(root, file)
print("cur img:",img_path)
# Read image
image = cv2.imread(img_path,cv2.IMREAD_UNCHANGED)
if image is None:
print(f"Failed to load {file}")
continue
# Calculate new dimensions
new_width = int(image.shape[1] / factor)
new_height = int(image.shape[0] / factor)
# Downscale image using bicubic interpolation
downscaled_image = cv2.resize(image, (new_width, new_height), interpolation=cv2.INTER_CUBIC)
# Construct output path and save image
base_name, ext = os.path.splitext(file)
output_path = os.path.join(output_dir, f"{base_name}x2{ext}")
cv2.imwrite(output_path, downscaled_image)
print(f"Saved downscaled image: {output_path}")
def printUsageAndExit():
print("Usage: python3 " + sys.argv[0] + " input_HR_folder output_LR_X2 folder")
sys.exit(-1)
# Run the function
if len(sys.argv) < 2:
printUsageAndExit()
input_folder=sys.argv[1]
output_folder=sys.argv[2]
if os.path.isdir(input_folder) is False:
print("Cannot find existing input folder: " + input_folder)
sys.exit(-1)
os.makedirs(output_folder, exist_ok=True)
if os.path.isdir(output_folder) is False:
print("Unable to create output folder: " + output_folder)
sys.exit(-1)
print("Input Folder: " + input_folder)
print("Output Folder: " + output_folder)
downscale_and_save_images(input_folder, output_folder, scale_factor)