-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimageColor.py
More file actions
211 lines (159 loc) · 5.23 KB
/
imageColor.py
File metadata and controls
211 lines (159 loc) · 5.23 KB
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
194
195
196
197
198
199
200
201
202
203
204
# -*- coding: utf-8 -*-
import numpy as np
import urllib
import cv2
import time
import os
import random
import pexif
import string
#保存图片
def save_img(img_url):
#保存图片到磁盘文件夹 file_path中,默认为当前脚本运行目录下的 book\img文件夹
try:
#下载图片,并保存到文件夹中
response = urllib.urlopen(img_url)
firstLine = str(response.readline())
nowTime = time.time()#生成当前的时间
randomNum = random.randint(0,100000)#生成随机数n,其中0<=n<=100
if firstLine.find('PNG') >= 0:
filename = "/tmp/com.shiqichuban.image-sharpening/"+str(nowTime) + "_" + str(randomNum)+".png"
else :
filename = "/tmp/com.shiqichuban.image-sharpening/"+str(nowTime) + "_" + str(randomNum)+".jpg"
with open(filename, 'wb') as f:
f.write(firstLine)
f.write(response.read())
return filename
except IOError as e:
print '文件操作失败',e
except Exception as e:
print '错误 :',e
return ""
# URL到图片
def url_to_image(url):
try:
# download the image, convert it to a NumPy array, and then read
# it into OpenCV format
resp = urllib.urlopen(url)
# bytearray将数据转换成(返回)一个新的字节数组
# asarray 复制数据,将结构化数据转换成ndarray
image = np.asarray(bytearray(resp.read()), dtype="uint8")
# cv2.imdecode()函数将数据解码成Opencv图像格式
image = cv2.imdecode(image, cv2.IMREAD_COLOR)
except:
print("url下载失败"+url)
image = cv2.imread("1.jpg")
finally:
# return the image
return image
def convertImage(image,red,green,blue):
rows,cols,channels=image.shape
print(rows,cols,channels)
for i in range(rows):
for j in range(cols):
if channels == 4:
if image[i,j][3] == 0:
image[i,j][0] = 0;
image[i,j][1] = 0;
image[i,j][2] = 0;
else:
image[i,j][0] = blue;
image[i,j][1] = green;
image[i,j][2] = red;
else:
image[i,j][0] = blue;
image[i,j][1] = green;
image[i,j][2] = red;
return image
def convertLocalImage(path,red,green,blue):
img = pexif.JpegFile.fromFile(path)
orientation = img.exif.primary.Orientation
img.exif.primary.Orientation = [1]
img.writeFile(path)
image = cv2.imread(path)
image = convertImage(image,red,green,blue)
cv2.imwrite(path,image)
img = pexif.JpegFile.fromFile(path)
img.exif.primary.Orientation = orientation
img.writeFile(path)
image = cv2.imread(path)
cv2.imshow(path+"_shape", image)
img_encode = cv2.imencode('.jpg', image)[1]
# return img_encode.tobytes()
return image
def convertURLToImage(url,red,green,blue):
if url.find('http') != -1:
sourcePath = path = save_img(url)
else:
path = url
if len(path) <= 0:
return ""
if path.endswith(('jpg','JPG')):
tmpPath = convertJPEGImage(path,red,green,blue)
elif path.endswith(('png','PNG')):
tmpPath = convertPNGImage(path,red,green,blue)
image = cv2.imread(tmpPath);
try:
os.remove(tmpPath)
except Exception as e:
print e
try:
os.remove(sourcePath)
except Exception as e:
print e
return image
def convertURLToData(url,red,green,blue):
if url.find('http') != -1:
sourcePath = path = save_img(url)
else:
path = url
if len(path) <= 0:
return ""
if path.endswith(('jpg','JPG')):
tmpPath = convertJPEGImage(path,red,green,blue)
elif path.endswith(('png','PNG')):
tmpPath = convertPNGImage(path,red,green,blue)
##图片转码
with open(tmpPath, 'r') as f:
result = f.read()
try:
os.remove(tmpPath)
except Exception as e:
print e
try:
os.remove(sourcePath)
except Exception as e:
print e
return result
def convertPNGImage(path,red,green,blue):
image = cv2.imread(path,cv2.IMREAD_UNCHANGED)
#图像处理
image = convertImage(image,red,green,blue)
path = path + ".temp.png"
cv2.imwrite(path,image)
return path
def convertJPEGImage(path,red,green,blue):
try:
img = pexif.JpegFile.fromFile(path)
orientation = img.exif.primary.Orientation
img.exif.primary.Orientation = [1]
img.writeFile(path)
except Exception as e:
print "读取orientation失败"
image = cv2.imread(path)
#图像处理
image = convertImage(image,red,green,blue)
cv2.imwrite(path,image)
return path
image = cv2.imread(path)
#图像处理
image = convertImage(image,red,green,blue)
cv2.imwrite(path,image)
#修改orientation
try:
img = pexif.JpegFile.fromFile(path)
img.exif.primary.Orientation = orientation
img.writeFile(path)
except Exception as e:
print "修改orientation失败"
return path