-
Notifications
You must be signed in to change notification settings - Fork 4
/
ImageProcessing.py
167 lines (138 loc) · 5.55 KB
/
ImageProcessing.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
from PIL import Image, ImageEnhance, ImageDraw, ImageFont
# 比例 a.1:1 b.4:3 c.3:4 d.16:9 e.9:16
def img_resize(img_path, width, height, flag, out_path="temp/temp.jpg"):
# flag a.1:1 b.4:3 c.3:4 d.16:9 e.9:16
img = Image.open(img_path)
if height <= width:
short_size = height
long_size = width
else:
short_size = width
long_size = height
if flag == 1:
dst = img.resize((short_size, short_size), Image.BICUBIC)
elif flag == 2:
dst = img.resize((long_size, int(3/4*long_size)), Image.BICUBIC)
elif flag == 3:
dst = img.resize((int(3/4*long_size), long_size), Image.BICUBIC)
elif flag == 4:
dst = img.resize((long_size, int(9/16*long_size)), Image.BICUBIC)
elif flag == 5:
dst = img.resize((int(9/16*long_size), long_size), Image.BICUBIC)
else:
dst = img.resize((width, height), Image.BICUBIC)
dst_path = out_path
dst.save(dst_path)
# 反转 左右/上下
# dst = img.transpose(Image.FLIP_LEFT_RIGHT)
def img_flip_left(img_path, out_path="temp/temp.jpg"):
img = Image.open(img_path)
dst = img.transpose(Image.FLIP_LEFT_RIGHT)
dst_path = out_path
dst.save(dst_path)
# dst = img.transpose(Image.FLIP_TOP_BOTTOM)
def img_flip_up(img_path, out_path="temp/temp.jpg"):
img = Image.open(img_path)
dst = img.transpose(Image.FLIP_TOP_BOTTOM)
dst_path = out_path
dst.save(dst_path)
# 旋转 顺/逆时针
# dst = img.transpose(Image.ROTATE_270)
def img_rotate_cw(img_path, out_path="temp/temp.jpg"):
img = Image.open(img_path)
dst = img.transpose(Image.ROTATE_270)
dst_path = out_path
dst.save(dst_path)
# dst = img.transpose(Image.ROTATE_90)
def img_rotate_ccw(img_path, out_path="temp/temp.jpg"):
img = Image.open(img_path)
dst = img.transpose(Image.ROTATE_90)
dst_path = out_path
dst.save(dst_path)
# 亮度、锐利度、对比度
# dst = ImageEnhance.Brightness(img)
def img_enhance(img_path, bright, sharp, contrast, out_path="temp/temp.jpg"):
img = Image.open(img_path)
dst = ImageEnhance.Brightness(img)
img = dst.enhance(bright)
dst = ImageEnhance.Sharpness(img)
img = dst.enhance(sharp)
dst = ImageEnhance.Contrast(img)
dst = dst.enhance(contrast)
dst_path = out_path
dst.save(dst_path)
"""
def img_enhance_brightness(img_path, factor, out_path="temp/temp.jpg"):
img = Image.open(img_path)
dst = ImageEnhance.Brightness(img)
dst = dst.enhance(factor)
dst_path = out_path
dst.save(dst_path)
# dst = ImageEnhance.Sharpness(img)
def img_enhance_sharpness(img_path, factor, out_path="temp/temp.jpg"):
img = Image.open(img_path)
dst = ImageEnhance.Sharpness(img)
dst = dst.enhance(factor)
dst_path = out_path
dst.save(dst_path)
# dst = ImageEnhance.Contrast(img)
def img_enhance_contrast(img_path, factor, out_path="temp/temp.jpg"):
img = Image.open(img_path)
dst = ImageEnhance.Contrast(img)
dst = dst.enhance(factor)
dst_path = out_path
dst.save(dst_path)
"""
# 添加水印
def watermark(img_path, texture="by 404", out_path="temp/temp.jpg", flag=0, x=60, y=20, size=20):
img = Image.open(img_path)
img = img.convert('RGBA')
txt = Image.new('RGBA', img.size, (0, 0, 0, 0))
#fnt = ImageFont.truetype("C:\Windows\Fonts\FTLTLT.TTF", size)
fnt = ImageFont.truetype("~/Library/Fonts/FZDengXian_Bold.ttf", size)
dst = ImageDraw.Draw(txt)
if flag == 0:
dst.text((txt.size[0] - x, txt.size[1] - y), texture, font=fnt, fill=(0, 0, 0, 255))
else:
dst.text((txt.size[0] - x, txt.size[1] - y), texture, font=fnt, fill=(255, 255, 255, 255))
dst = Image.alpha_composite(img, txt)
dst = dst.convert('RGB')
dst_path = out_path
dst.save(dst_path)
# 添加白色边框
def border(img_path, factor=0.02):
img = Image.open(img_path)
w, h = img.size
dst = ImageDraw.Draw(img)
w_half = int(w*factor)
h_half = int(h*factor)
dst.line(((0, h_half-1), (w-1, h_half-1)), fill=(255, 255, 255), width=2*h_half)
dst.line(((0, h-1-h_half), (w-1, h-1-h_half)), fill=(255, 255, 255), width=2*h_half)
dst.line(((w_half-1, 0), (w_half-1, h-1)), fill=(255, 255, 255), width=2*w_half)
dst.line(((w-1-w_half, 0), (w-1-w_half, h-1)), fill=(255, 255, 255), width=2*w_half)
dst_path = "temp.jpg"
img.save(dst_path)
# 添加水印
# flag 0:black 1:white
# def watermark(image, texture="by 404", flag=0, x=60, y=20, size=20):
# img = image.convert('RGBA')
# txt = Image.new('RGBA', img.size, (0, 0, 0, 0))
# fnt = ImageFont.truetype("C:\Windows\Fonts\FTLTLT.TTF", size)
# dst = ImageDraw.Draw(txt)
# if flag == 0:
# dst.text((txt.size[0] - x, txt.size[1] - y), texture, font=fnt, fill=(0, 0, 0, 255))
# else:
# dst.text((txt.size[0] - x, txt.size[1] - y), texture, font=fnt, fill=(255, 255, 255, 255))
# dst = Image.alpha_composite(img, txt)
# return dst
# 添加白色边框
# def border(img, factor=0.02):
# w, h = img.size
# dst = ImageDraw.Draw(img)
# w_half = int(w*factor)
# h_half = int(h*factor)
# dst.line(((0, h_half-1), (w-1, h_half-1)), fill=(255, 255, 255), width=2*h_half)
# dst.line(((0, h-1-h_half), (w-1, h-1-h_half)), fill=(255, 255, 255), width=2*h_half)
# dst.line(((w_half-1, 0), (w_half-1, h-1)), fill=(255, 255, 255), width=2*w_half)
# dst.line(((w-1-w_half, 0), (w-1-w_half, h-1)), fill=(255, 255, 255), width=2*w_half)
# return img