-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdownsize.py
85 lines (69 loc) · 2.61 KB
/
downsize.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
import os
import configparser
from pathlib import Path
from PIL import Image
# Get the directory of the current script
script_dir = Path(__file__).parent.absolute()
# Set the path to config.ini based on the script directory
config_ini_path = script_dir / "config.ini"
print(config_ini_path)
# Read parameters from config.ini using Python
config = configparser.ConfigParser()
config.read(config_ini_path)
raw_directory = config["Settings"]["image_path"]
mask_directory = config["CrackSegmentation"]["mask_directory"].replace(
"crackmask", "concretemask"
)
config_param = config["CrackSegmentation"]["config"]
model = "saved/UperNet/01-17_12-56/best_model.pth" # concreteNet
print(f"RAW_DIR={raw_directory}")
print(f"MASK_DIR={mask_directory}")
print(f"CONFIG={config_param}")
print(f"MODEL={model}")
# Ensure the mask_directory exists
os.makedirs(mask_directory, exist_ok=True)
print(f"Ensured existence of directory: {mask_directory}")
# Get the model path
model_path = (
Path("/home/roboticslab/Developer/pytorch_concrete_flaws_segmentation") / model
)
print(f"MODEL_PATH={model_path}")
# Define the directory where inference.py is located
pytorch_segmentation_dir = (
"/home/roboticslab/Developer/pytorch_concrete_flaws_segmentation"
)
def resize_image(image_path, output_dir, max_size):
"""
Resizes an image to fit within a maximum dimension while preserving aspect ratio.
Args:
image_path (string): Path to the input image.
output_dir (string): Path to the output directory for resized images.
max_size (int): The maximum width or height of the resized image.
"""
image = Image.open(image_path)
width, height = image.size
if width > height:
scale = max_size / width
else:
scale = max_size / height
new_width = int(width * scale)
new_height = int(height * scale)
new_size = (new_width, new_height)
resized_image = image.resize(new_size, Image.LANCZOS)
filename = os.path.basename(image_path)
output_path = os.path.join(output_dir, filename)
resized_image.save(output_path)
# Create the output directory
downsized_dir = os.path.join(os.path.dirname(raw_directory), "downsized_raw")
os.makedirs(downsized_dir, exist_ok=True)
max_size = 1024
# Resize and save images in the downsized directory
for filename in os.listdir(raw_directory):
if (
filename.endswith(".jpg")
or filename.endswith(".png")
or filename.endswith(".JPG")
):
image_path = os.path.join(raw_directory, filename)
new_size = (512, 512) # Adjust this to your desired size
resize_image(image_path, downsized_dir, max_size)