-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathresize_image.py
More file actions
72 lines (56 loc) · 2.33 KB
/
Copy pathresize_image.py
File metadata and controls
72 lines (56 loc) · 2.33 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
67
68
69
70
71
from PIL import Image
#pillow 9.5.0
import os
from filefolder_manage import getfilename, getdirpath,create_folder
from env_data import env_data
def calculate_scale_factor(width, height, target_size):
# Calculate the scale factor based on the smaller dimension
min_dimension = min(width, height)
scale_factor = target_size / min_dimension
return scale_factor
def resize_image_scale(input_path,output_path):
dir = getdirpath(input_path)
filename = getfilename(input_path)
create_folder(output_path=f'{output_path}/{filename}')
# Open the image
image = Image.open(input_path)
# Get the original size
original_width, original_height = image.size
# Set the target size
envdata = env_data()
target_size = envdata.get_imageresize()
# Check if either dimension is smaller than 1000
if original_width < target_size or original_height < target_size:
# If either dimension is smaller than 1000, do not resize
print('resize_image_scale not resize')
return input_path
# new_width, new_height = original_width, original_height
else:
print('resize_image_scale resize')
# Calculate scale factor based on the smaller dimension
scale_factor = calculate_scale_factor(original_width, original_height, target_size)
# Calculate new dimensions
new_width = int(original_width * scale_factor)
new_height = int(original_height * scale_factor)
# Resize the image
image = image.resize((new_width, new_height))
save_path = f'{output_path}/{filename}/{filename}_resized.tif'
# Save the resized image
image.save(save_path)
return save_path
def restore_original_size(original_path, resized_path):
print('restore_original_size')
dir = getdirpath(resized_path)
filename = getfilename(resized_path)
# Open the original image
original_image = Image.open(original_path)
# Open the resized image
resized_image = Image.open(resized_path)
# Get the original size
original_width, original_height = original_image.size
# Resize the resized image to the original size
restored_image = resized_image.resize((original_width, original_height), Image.ANTIALIAS)
save_path = f'{dir}/{filename}_restore.png'
# Save the restored image
restored_image.save(save_path)
return save_path