Image格式转换

PIL.Image转换成OpenCV格式:

import cv2
from PIL import Image
import numpy
path = '1.jpg'
img = Image.open(path).convert("RGB")#.convert("RGB")可不要,默认打开就是RGB
img.show()
"""key line PIL-->CV"""
#img = cv2.cvtColor(numpy.asarray(image),cv2.COLOR_RGB2BGR)
img = cv2.cvtColor(np.array(img),cv2.COLOR_RGB2BGR)
cv2.imshow("OpenCV",img)
cv2.waitKey()

OpenCV转换成PIL.Image格式:

import cv2
from PIL import Image
import numpy
img = cv2.imread("1.jpg") # opencv打开的是BRG
cv2.imshow("OpenCV",img)
"""key line CV-->PIL"""
image = Image.fromarray(cv2.cvtColor(img,cv2.COLOR_BGR2RGB))
image.show()
cv2.waitKey()

判断图像数据是否是OpenCV格式:

isinstance(img, np.ndarray)

OpenCV转换成二进制:

import cv2
from PIL import Image
import numpy as np
import io
from baidu.person_detect import person_detect_byte

#读取网络摄像头
#camera = cv2.VideoCapture('rtsp://admin:password@10.0.10.1:554/h264/ch1/main/av_stream')

#读取本地摄像头
camera = cv2.VideoCapture (0)

success, image = camera.read ()
cv2.imshow("xxxx",image)
cv2.waitKey (5000)
imgRGB = cv2.cvtColor (image, cv2.IMREAD_COLOR)
# 将图片格式转换(编码)成流数据,赋值到内存缓存中;主要用于图像数据格式的压缩,方便网络传输。
r, buf = cv2.imencode (".jpg", imgRGB)

#array转换成image
bytes_image = Image.fromarray (np.uint8 (buf)).tobytes ()
#array转换成二进制
# image = io.BytesIO (bytes_image)

JpegImageFile转换成二进制:


from io import BytesIO
img = Image.open('/home/hensel/Desktop/1.jpg')

# Create a buffer to hold the bytes
buf = BytesIO()

# Save the image as jpeg to the buffer
img.save(buf, 'jpeg')

# Rewind the buffer's file pointer
buf.seek(0)

# Read the bytes from the buffer
image_bytes = buf.read()
# res=person_detect_byte(image_bytes)
# print(res)

# Close the buffer
buf.close()

参考:
https://blog.csdn.net/Gavinmiaoc/article/details/83614833

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值