-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimage_scaler.py
56 lines (43 loc) · 1.67 KB
/
image_scaler.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
import numpy as np
from PIL import Image
import matplotlib.pyplot as plt
import os
from skimage import io, img_as_float
def load_image(image_path):
# Load and preprocess the image
image = io.imread(image_path)
# Ensures pixel values are between 0 and 1
image = img_as_float(image).astype(np.float64)
# Read as RGB, not RGBA
if image.shape[2] > 3:
image = image[:, :, :3]
print("Image loaded and normalized.")
return image
def scale(image, scaling_factor):
image_height, image_width, _ = image.shape
scaled_image = Image.fromarray(image).resize((int(image_width * scaling_factor), int(image_height * scaling_factor)))
return np.asarray(scaled_image)
def scale(image, width, height):
scaled_image = Image.fromarray(image).resize((width, height))
return np.asarray(scaled_image)
if __name__ == '__main__':
input_folder = './data/noise'
name = 'fire.png'
file_name = os.path.join(input_folder, name)
img = Image.open(file_name).convert('RGB')
input_image = np.asarray(img)
scaling_factor = 0.25
# scaled_image = scale(input_image, scaling_factor)
scaled_image = scale(input_image, 256, 256)
# print the dimensions of the images
print("Original Image Dimensions: ", input_image.shape)
print("Scaled Image Dimensions: ", scaled_image.shape)
# Plot the images
fig, axes = plt.subplots(1, 2, figsize=(10, 5))
axes[0].imshow(input_image)
axes[0].set_title('Input Image')
axes[1].imshow(scaled_image)
axes[1].set_title('Scaled Image')
plt.show()
output_name = input_folder + '/'+ name.split('.')[0] + '_256.png'
Image.fromarray(scaled_image).save(output_name)