-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscale.py
91 lines (73 loc) · 2.33 KB
/
scale.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
86
87
88
89
90
from PIL import Image
import os
import random
ori_img = "zero.png"
# ori_img = Image.open(ori_img)
#
#
# rot_angle = random.randint(0, 360)
# trans_img = ori_img.rotate(rot_angle)
#
# trans_img.show()
# shift = 1
# image = Image.open(ori_img)
# inverted_image = Image.invert(image)
#
# out = image[:image.rfind('.')]
# inverted_image.save('%s-n.JPG'%out)
# Shift the image 5 pixels
# width, height = image.size
# shifted_image = Image.new("RGB", (width+shift, height))
# shifted_image.paste(image, (1, 0))
# shifted_image.show()
#shifted_image.save('%s-shifted.JPG' % out)
# Affine Transformation
# Image.transform(size, method, data)
# with method=Image.AFFINE returns a copy of an image
# where an affine transformation matrix
# (given as 6-tuple (a, b, c, d, e, f)via data) has been applied.
# For each pixel (x, y),
# the output will be calculated as (ax+by+c, dx+ey+f).
# So if you want to apply a translation,
# you only have to look at the c and f values of your matrix.
# ori_image='three.png'
# scale_img_path=''
# img = Image.open(ori_image)
#Apply scaling by changing a and e values
# a = 0.5
# b = 0
# c = 0 #left/right (i.e. 5/-5)
# d = 0
# e = 0.5
# f = 0 #up/down (i.e. 5/-5)
# img = img.transform(img.size, Image.AFFINE, (a, b, c, d, e, f))
#img.save()
# img.show()
dir = "./test_images_ori"
for name in os.listdir(dir):
print(name)
path = os.path.join(dir, name)
fol = path.split('/')[2]
# print(fol)
for filename in os.listdir(path):
ori_img_path = path +"/"+ filename
print("ori_img_path : " + ori_img_path)
ori_img= Image.open(ori_img_path)
#create dir fol inside test_imgs_scale
scale_im_p= "test_imgs_scale/" + fol
scale_img_path = "test_imgs_scale/" + fol + "/" + filename
if not os.path.exists(scale_im_p):
os.makedirs(scale_im_p)
#TODO discuss scale_factor(2/3) with supervisor
#change a and e to change the scale
scale_factor = random.randint(2, 3)
#rot_img = ori_img.rotate(rot_angle)
a = scale_factor
b = 0
c = 0 # left/right (i.e. 5/-5)
d = 0
e = scale_factor
f = 0 # up/down (i.e. 5/-5)
scale_img = ori_img.transform(ori_img.size, Image.AFFINE, (a, b, c, d, e, f))
scale_img.save(scale_img_path)
print("scale_img_path : "+scale_img_path)