png转jpg
import cv2
import numpy as np
import os
""" 读取图片 """
def get_file_content(filePath):
with open(filePath, 'rb') as fp:
return fp.read()
#PNG图像(便携式网络图形(Portable Network Graphics))
#是一种典型的4通道图像。alpha通道可以赋值0到1,或者0到255,表示透明到不透明,但是一般直接imshow()是看不出什么效果来的
def save_jpg(fstream, fname):
nparr = np.fromstring(fstream, np.uint8)
orgimage = cv2.imdecode(nparr, cv2.IMREAD_UNCHANGED)
if len(cv2.split(orgimage)) > 3:
orgimage[np.all(orgimage == [0, 0, 0, 0], axis=2)] = [255, 255, 255, 0]
print(len(cv2.split(orgimage)))
savepath = fname[:-4] + '.jpg'
print(savepath)
cv2.imwrite(savepath, orgimage)
if __name__ == '__main__':
image_name = '000284.jpg'
image_file = get_file_content(image_name)
save_jpg(image_file, image_name)