-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathimage_tools.py
193 lines (155 loc) · 6.25 KB
/
image_tools.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
import os
import PIL
import base64
import io
from PIL import Image
import cv2
import numpy
class ImageObj(object):
'''
图片工具类,用于处理图片转换成
'''
def __init__(self, image):
if isinstance(image, str):
if os.path.isfile(image):
self.image = open(image, 'rb').read()
else:
raise TypeError('No such path!')
elif isinstance(image, (PIL.Image.Image)):
self.image = image
elif isinstance(image, (numpy.ndarray,)):
self.image = image
elif self.isBase64(image):
self.image = image
elif isinstance(image, (bytes,)):
self.image = image
else:
raise TypeError('input obj err! please check!')
def __cv2_to_base64(self, image):
return base64.b64encode(image.tobytes())
def __base64_to_cv2(self, image):
return numpy.frombuffer(base64.b64decode(image), dtype=numpy.uint8)
def __pil_to_cv2(self, image):
return numpy.asarray(image)
def __cv2_to_pil(self, image):
return Image.fromarray(image)
def __bytes_to_pil(self, image):
image = self.__cv2_to_pil(self.__bytes_to_cv2(image))
return image
def __pil_to_bytes(self, image):
return self.__pil_to_cv2(image).tobytes()
def __cv2_to_bytes(self, image):
return image.tobytes()
def __bytes_to_cv2(self, image):
return numpy.frombuffer(image, dtype=numpy.uint8)
def __base64_to_bytes(self, image):
return base64.b64decode(image)
def __bytes_to_base64(self, image):
return base64.b64encode(image)
def __pil_to_base64(self, image):
return base64.b64encode(self.__pil_to_cv2(image).tobytes())
def isBase64(self, s):
try:
return (base64.b64encode(base64.b64decode(s,validate=True)) == s)
except Exception:
return False
def __base64_to_pil(self, image):
return Image.fromarray(self.__bytes_to_cv2(self.__base64_to_bytes(image)))
def to_base64(self):
"""
将image对象转换为base64对象
:param image: 可以是numpy数组、pil图片对象、图片的base64对象、图片的bytes形式
:return:base64对象
"""
if isinstance(self.image, (PIL.Image.Image)):
self.image = self.__pil_to_base64(self.image)
elif isinstance(self.image, (numpy.ndarray,)):
self.image = self.__cv2_to_base64(self.image)
elif self.isBase64(self.image):
...
elif isinstance(self.image, (bytes,)):
self.image = self.__bytes_to_base64(self.image)
return self
def to_bytes(self):
"""
将image对象转换为bytes对象
:param image: 可以是numpy数组、pil图片对象、图片的base64对象、图片的bytes形式
:return:PIl对象
"""
if isinstance(self.image, (numpy.ndarray,)):
self.image = self.__cv2_to_bytes(self.image)
elif isinstance(self.image, (PIL.Image.Image)):
self.image = self.__pil_to_bytes(self.image)
elif self.isBase64(self.image):
self.image = self.__base64_to_bytes(self.image)
elif isinstance(self.image, (bytes,)):
pass
return self
def to_array(self):
"""
将image对象转换为cv对象(numpy数组)
:param image: 可以是numpy数组、pil图片对象、图片的base64对象、图片的bytes形式
:return:numpy数组
"""
if isinstance(self.image, (PIL.Image.Image)):
self.image = self.__pil_to_cv2(self.image)
elif self.isBase64(self.image):
self.image = self.__base64_to_cv2(self.image)
elif isinstance(self.image, (bytes,)):
self.image = self.__bytes_to_cv2(self.image)
elif isinstance(self.image, (numpy.ndarray,)):
pass
return self
def to_pillowobj(self):
"""
将image对象转换为PIL对象
:param image: 可以是numpy数组、pil图片对象、图片的base64对象、图片的bytes形式
:return:PIL对象
"""
if self.isBase64(self.image):
self.image = self.__base64_to_pil(self.image)
elif isinstance(self.image, (bytes,)):
self.image = self.__bytes_to_pil(self.image)
elif isinstance(self.image, (numpy.ndarray,)):
self.image = self.__cv2_to_pil(self.image)
elif isinstance(self.image, (PIL.Image.Image)):
pass
return self
def save(self, imagefile):
self.to_bytes()
with open(imagefile, 'wb') as f:
f.write(self.image)
if __name__ == '__main__':
image = r'./test1.jpg'
img = ImageObj(image)
img0 = img.to_base64().image
img = img.to_bytes().to_array().to_pillowobj().to_array().to_pillowobj().to_bytes().to_pillowobj().to_base64().image
if img0 == img:
print('数据吻合')
image = r'./test1.jpg'
image = cv2.imread(image)
img = ImageObj(image)
img0 = img.to_base64().image
img = img.to_bytes().to_array().to_pillowobj().to_array().to_pillowobj().to_bytes().to_pillowobj().to_base64().image
if img0 == img:
print('数据吻合')
img = ImageObj(r'./test1.jpg')
img0 = img.to_base64().image
img = img.to_bytes().to_array().to_pillowobj().to_array().to_pillowobj().to_bytes().to_pillowobj().to_base64().image
if img0 == img:
print('数据吻合')
image = r'./test1.jpg'
image = open(image, 'rb').read()
img = ImageObj(image)
img0 = img.to_base64().image
img = img.to_bytes().to_array().to_pillowobj().to_array().to_pillowobj().to_bytes().to_pillowobj().to_base64().image
if img0 == img:
print('数据吻合')
image = r'./test1.jpg'
image = base64.b64encode(open(image, 'rb').read())
img1 = ImageObj(image)
img0 = img1.to_base64().image
img = img1.to_bytes().to_array().to_pillowobj().to_array().to_pillowobj().to_bytes().to_pillowobj().to_base64().image
if img0 == img:
print('数据吻合')
img1.save('text.png')