def cvimg_to_base64(image_np):
cvimg = cv2.cvtColor(image_np, cv2.COLOR_BGR2RGB)
cvimg = cv2.imencode('.jpg',cvimg)[1]
cvimg = str(base64.b64encode(cvimg))[2:-1]
return cvimg
def base64_to_cvimg(base64_code):
# base64解码
img_data = base64.b64decode(base64_code)
# 转换为np数组
img_array = np.fromstring(img_data, np.uint8)
# 转换成opencv可用格式
img = cv2.imdecode(img_array, cv2.COLOR_RGB2BGR)
return img
def cvimg_to_pilimg(cvimg):
cvimg = cv2.cvtColor(cvimg, cv2.COLOR_BGR2RGB)
pilimg = Image.fromarray(cvimg).convert('RGB')
return pilimg
def pilimg_to_cvimg(pilimg):
cvimg = cv2.cvtColor(np.asarray(pilimg), cv2.COLOR_RGB2BGR)
return cvimg
def pilimg_to_base64(pilimg):
cvimg = pilimg_to_cvimg(pilimg)
cvimg = cv2.imencode('.jpg',cvimg)[1]
cvimg = str(base64.b64encode(cvimg))[2:-1]
return cvimg
def pilimg_to_base64(pilimg):
output_buffer = BytesIO()
pilimg.save(output_buffer, format='JPEG')
byte_data = output_buffer.getvalue()
base64_str = base64.b64encode(byte_data)
return base64_str
def base64_to_pilimg(base64_code):
# base64解码
img_data = base64.b64decode(base64_code)
# 转换为np数组
img_array = np.fromstring(img_data, np.uint8)
# 转换成opencv可用格式
img = cv2.imdecode(img_array, cv2.COLOR_RGB2BGR)
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
pilimg = Image.fromarray(img)
return pilimg
def base64_to_pilimg(base64_str, image_path=None):
base64_data = re.sub('^data:image/.+;base64,', '', base64_str)
byte_data = base64.b64decode(base64_data)
image_data = BytesIO(byte_data)
img = Image.open(image_data)
if image_path:
img.save(image_path)
return img
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)