-
Notifications
You must be signed in to change notification settings - Fork 1
/
resize_images.py
65 lines (50 loc) · 1.96 KB
/
resize_images.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
# -*- coding: utf-8 -*-
"""
Created on Fri Jul 29 18:30:27 2022
@patch: 2022.08.01
@author: Paul
@file: resize_images.py
@dependencies:
env pt3.7
python 3.7.13
torch >= 1.7.1
torchvision >= 0.8.2
Pillow >= 8.1.0
Resize image to a certain size
"""
# import the required libraries
import torchvision.transforms as T # for resizing the images
from PIL import Image # for loading and saving the images
# test the basic functionality of resizing an image to certain size
def testing(i=1, file_type='jpg'):
# read the input image
# img = Image.open(f'D:/Datasets/RD_maps/scaled_colors/{i}_sc.png')
img = Image.open(f'D:/Datasets/RD_maps/scaled_colors/{i}_sc.{file_type}')
# compute the size (width, height) of image
before = img.size
print(f"original image size: {before}")
# define the transform function to resize the image with given size, say 416-by-416
transform = T.Resize(size=(416,416))
# apply the transform on the input image
img = transform(img)
# check the size (width, height) of image
after = img.size
print(f"resized image size: {after}")
# overwrite the original image with the resized one
# img = img.save(f'D:/Datasets/RD_maps/scaled_colors/{i}_sc.png')
img.show()
def main(max_iter=1, file_type='jpg'):
# 1600
for i in range(1, max_iter + 1):
# read the input image
img = Image.open(f'D:/Datasets/RD_maps/scaled_colors/{i}_sc.{file_type}')
# define the transform function to resize the image with given size, say 416-by-416
transform = T.Resize(size=(416,416))
# apply the transform on the input image
img = transform(img)
# overwrite the original image with the resized one
img = img.save(f'D:/Datasets/RD_maps/scaled_colors/{i}_sc.{file_type}')
print(f"{i}")
if __name__ == '__main__':
# testing(1, 'jpg')
main(2000, 'jpg')