dev-tool scripts. can be deleted or not
parent
7ae3c74c4f
commit
987e45a225
|
|
@ -0,0 +1,116 @@
|
||||||
|
"""
|
||||||
|
def calculate_interpolation_steps_goldenratio(self,original_size, target_size, steps):
|
||||||
|
width, height = original_size
|
||||||
|
target_width, target_height = target_size
|
||||||
|
golden_ratio = (1 + 5 ** 0.5) / 2 - 1 # Approx. 0.618
|
||||||
|
|
||||||
|
if width <= 0 or height <= 0 or target_width <= 0 or target_height <= 0 or steps <= 0:
|
||||||
|
return None
|
||||||
|
|
||||||
|
original_ratio = width / height
|
||||||
|
scaling_steps = []
|
||||||
|
for i in range(1, steps + 1):
|
||||||
|
t = i / steps
|
||||||
|
factor = 1 - golden_ratio * t
|
||||||
|
new_width = width * factor + target_width * (1 - factor)
|
||||||
|
new_height = height * factor + target_height * (1 - factor)
|
||||||
|
|
||||||
|
floor_width, ceil_width = int(new_width // 1), int(new_width // 1 + 1)
|
||||||
|
floor_height, ceil_height = int(new_height // 1), int(new_height // 1 + 1)
|
||||||
|
|
||||||
|
floor_ratio = floor_width / floor_height
|
||||||
|
ceil_ratio = ceil_width / ceil_height
|
||||||
|
|
||||||
|
if abs(floor_ratio - original_ratio) < abs(ceil_ratio - original_ratio):
|
||||||
|
new_width, new_height = floor_width, floor_height
|
||||||
|
else:
|
||||||
|
new_width, new_height = ceil_width, ceil_height
|
||||||
|
|
||||||
|
scaling_steps.append((new_width, new_height))
|
||||||
|
|
||||||
|
return scaling_steps
|
||||||
|
|
||||||
|
|
||||||
|
def calculate_interpolation_steps_log(self, original_size, target_size, steps):
|
||||||
|
width, height = original_size
|
||||||
|
target_width, target_height = target_size
|
||||||
|
|
||||||
|
if width <= 0 or height <= 0 or target_width <= 0 or target_height <= 0 or steps <= 0:
|
||||||
|
return None
|
||||||
|
|
||||||
|
original_ratio = width / height
|
||||||
|
scaling_steps = []
|
||||||
|
|
||||||
|
log_w_ratio = math.log(target_width / width) / steps
|
||||||
|
log_h_ratio = math.log(target_height / height) / steps
|
||||||
|
|
||||||
|
for i in range(1, steps):
|
||||||
|
new_width = width * math.exp(i * log_w_ratio)
|
||||||
|
new_height = height * math.exp(i * log_h_ratio)
|
||||||
|
|
||||||
|
floor_width, ceil_width = int(new_width // 1), int(new_width // 1 + 1)
|
||||||
|
floor_height, ceil_height = int(new_height // 1), int(new_height // 1 + 1)
|
||||||
|
|
||||||
|
floor_ratio = floor_width / floor_height
|
||||||
|
ceil_ratio = ceil_width / ceil_height
|
||||||
|
|
||||||
|
if abs(floor_ratio - original_ratio) < abs(ceil_ratio - original_ratio):
|
||||||
|
new_width, new_height = floor_width, floor_height
|
||||||
|
else:
|
||||||
|
new_width, new_height = ceil_width, ceil_height
|
||||||
|
|
||||||
|
scaling_steps.append((new_width, new_height))
|
||||||
|
|
||||||
|
# Add the last step that is one pixel away from the target size
|
||||||
|
scaling_steps.append((target_width - 1, target_height - 1))
|
||||||
|
|
||||||
|
return scaling_steps
|
||||||
|
|
||||||
|
|
||||||
|
def calculate_interpolation_steps_exponential(self, original_size, target_size, steps,exponent=2):
|
||||||
|
width, height = original_size
|
||||||
|
target_width, target_height = target_size
|
||||||
|
scaling_steps = []
|
||||||
|
for i in range(1, steps + 1):
|
||||||
|
t = i / steps
|
||||||
|
factor = (1 - t) + t * (math.pow(t, exponent - 1))
|
||||||
|
new_width = width * (1 - factor) + target_width * factor
|
||||||
|
new_height = height * (1 - factor) + target_height * factor
|
||||||
|
scaling_steps.append((math.floor(new_width), math.floor(new_height)))
|
||||||
|
return scaling_steps
|
||||||
|
"""
|
||||||
|
|
||||||
|
"""
|
||||||
|
scaling_steps = self.apply_savitzky_golay_filter(scaling_steps,self.width/self.height)
|
||||||
|
for s in scaling_steps:
|
||||||
|
print(f"Ratios: {str(s[0]/s[1])}",end=";")
|
||||||
|
|
||||||
|
print(f"After SAVGOL: {scaling_steps}, length: {len(scaling_steps)}")
|
||||||
|
for s in scaling_steps:
|
||||||
|
print(f"Ratios: {str(s[0]/s[1])}",end=";")
|
||||||
|
"""
|
||||||
|
|
||||||
|
|
||||||
|
"""
|
||||||
|
def apply_savitzky_golay_filter(self,scaling_steps, original_ratio, window_length=5, polyorder=2):
|
||||||
|
widths, heights = zip(*scaling_steps)
|
||||||
|
smoothed_widths = savgol_filter(widths, window_length, polyorder)
|
||||||
|
smoothed_heights = savgol_filter(heights, window_length, polyorder)
|
||||||
|
|
||||||
|
integer_steps = []
|
||||||
|
for new_width, new_height in zip(smoothed_widths, smoothed_heights):
|
||||||
|
floor_width, ceil_width = int(new_width // 1), int(new_width // 1 + 1)
|
||||||
|
floor_height, ceil_height = int(new_height // 1), int(new_height // 1 + 1)
|
||||||
|
|
||||||
|
floor_ratio = floor_width / floor_height
|
||||||
|
ceil_ratio = ceil_width / ceil_height
|
||||||
|
|
||||||
|
if abs(floor_ratio - original_ratio) < abs(ceil_ratio - original_ratio):
|
||||||
|
new_width, new_height = floor_width, floor_height
|
||||||
|
else:
|
||||||
|
new_width, new_height = ceil_width, ceil_height
|
||||||
|
|
||||||
|
integer_steps.append((new_width, new_height))
|
||||||
|
|
||||||
|
return integer_steps
|
||||||
|
"""
|
||||||
|
|
@ -0,0 +1,23 @@
|
||||||
|
import numpy as np
|
||||||
|
import matplotlib.pyplot as plt
|
||||||
|
|
||||||
|
# Your original sizes
|
||||||
|
original_sizes = [(3842, 3842), (3810, 3810), (3780, 3780), (3752, 3752), (3722, 3722), (3692, 3692), (3662, 3662), (3632, 3632), (3602, 3602), (3572, 3572), (3542, 3542), (3512, 3512), (3482, 3482), (3454, 3454), (3422, 3422), (3392, 3392), (3362, 3362), (3332, 3332), (3304, 3304), (3274, 3274), (3244, 3244), (3214, 3214), (3184, 3184), (3154, 3154), (3124, 3124), (3094, 3094), (3064, 3064), (3034, 3034), (3006, 3006), (2974, 2974), (2944, 2944), (2914, 2914), (2884, 2884), (2856, 2856), (2826, 2826), (2796, 2796), (2766, 2766), (2736, 2736), (2706, 2706), (2676, 2676), (2646, 2646), (2616, 2616), (2586, 2586), (2558, 2558), (2526, 2526), (2496, 2496), (2466, 2466), (2436, 2436), (2408, 2408), (2378, 2378), (2348, 2348), (2318, 2318), (2288, 2288), (2258, 2258), (2228, 2228), (2198, 2198), (2168, 2168), (2138, 2138), (2108, 2108), (2080, 2080)]
|
||||||
|
|
||||||
|
# Unzip to separate width and height (in this case they are the same)
|
||||||
|
sizes = np.array(original_sizes)
|
||||||
|
widths = sizes[:,0]
|
||||||
|
|
||||||
|
# Define moving average function
|
||||||
|
def moving_average(x, w):
|
||||||
|
return np.convolve(x, np.ones(w), 'valid') / w
|
||||||
|
|
||||||
|
# Apply moving average
|
||||||
|
window_size = 5 # Set the size of the moving window
|
||||||
|
smooth_widths = moving_average(widths, window_size)
|
||||||
|
|
||||||
|
# Plot old and new sizes
|
||||||
|
plt.plot(widths, label='Original')
|
||||||
|
plt.plot(smooth_widths, label='Smoothed')
|
||||||
|
plt.legend()
|
||||||
|
plt.show()
|
||||||
Loading…
Reference in New Issue