图片各类格式转化

打开图片与保存图片

##PIL 读取、保存图片方法
from PIL import Image
img = Image.open("eee.jpg")
img.save("eee_result.jpg")

##cv2 读取、保存图片方法
import cv2
img = cv2.imread("eee.jpg")
cv2.imwrite("eee_result.jpg", img)

##图片文件打开为base64
import base64
def img_base64(img_path):
    with open(img_path, "rb") as f:
        base64_str = base64.b64encode(f.read())
    return base64_str

CV2中的 BGR转RGB

from matplotlib import pyplot as plt
import cv2

img = cv2.imread("eee.jpg")
cv2.imwrite("eee_result.jpg", img)
imgrgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
plt.imshow(imgrgb)
plt.show()


pillow CV2 的相互转换

## PIL转cv2
import cv2
from PIL import Image
import numpy as np

img_rgb = Image.open("eee.jpg")
img_bgr = cv2.cvtColor(np.asarray(img_rgb), cv2.COLOR_RGB2BGR) # 成为cv2的numpy.ndarray


PIL base64 的相互转换

##PIL转base64
import base64
from io import BytesIO
from PIL import Image


def pillow_to_base64(image):
    img_buffer = BytesIO()
    image.save(img_buffer, format='JPEG')
    byte_data = img_buffer.getvalue()
    base64_str = base64.b64encode(byte_data)
    return base64_str


# example
img = Image.open("eee.jpg")
print(pillow_to_base64(img))

##PIL转base64
import base64
from io import BytesIO
from PIL import Image


def base64_to_pillow(base64_str):
    image = base64.b64decode(base64_str)
    image = BytesIO(image)
    image = Image.open(image)
    return image


# example
with open("eee.jpg", "rb") as f:
    base64_str = base64.b64encode(f.read())
print(base64_to_pillow(base64_str))


CV2 base64 的相互转换

# cv2 转 base64
import base64
import cv2


def cv2_to_base64(image):
    base64_str = cv2.imencode('.jpg', image)[1].tostring()
    base64_str = base64.b64encode(base64_str)
    return base64_str

# example
image = cv2.imread("eee.jpg")
print(cv2_to_base64(image))


# base64 转 cv2
import base64
import numpy as np
import cv2
from PIL import Image
from io import BytesIO

def base64_to_cv2(base64_str):
    imgString = base64.b64decode(base64_str)
    nparr = np.frombuffer(imgString, np.uint8)
    image = cv2.imdecode(nparr, cv2.IMREAD_COLOR)
    return image


# example
with open("eee.jpg", "rb") as f:
    base64_str = base64.b64encode(f.read())
print(base64_to_cv2(base64_str))


FastAPI中bytes 转CV2

img = cv2.imdecode(np.frombuffer(file, dtype=np.uint8), cv2.IMREAD_COLOR)

FastAPI中uploadfile 转CV2

img = cv2.imdecode(np.fromstring(fileb.file.read(), np.uint8), cv2.IMREAD_COLOR)

CV2 numpy 转成 字节流(open(“file.jpg”,‘rb’))

img = cv2.imread("lpr1.png")
# 对数组的图片格式进行编码
success, encoded_image = cv2.imencode(".jpg", img)
# 将数组转为bytes
f = encoded_image.tobytes()

to_show = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
            to_show = Image.fromarray(np.uint8(to_show))  # numpy.ndarray 转 PIL.Image.Image
            bytes_io = io.BytesIO()  # 将图片转为bytesio发送出去
            to_show.save(bytes_io, format="PNG")

            estimate = process(bytes_io.getvalue(), backend)
            estimated_image = Image.open(io.BytesIO(estimate.content)).convert("RGB")
            image_placeholder.image(to_show, caption='Video')  # 原图片 将图片帧展示在同一位置得到视频效果
            result_place.image(estimated_image, caption='Video')  # 结果图片 将图片帧展示在同一位置得到视频效果

 
img = Image.open(io.BytesIO(image_bytes))

        results = model(img, size=640)  # reduce size=320 for faster inference
        return results.pandas().xyxy[0].to_json(orient="records")

参考文章

  • https://codeantenna.com/a/syiFxEejDG

你可能感兴趣的:(python,python,numpy,opencv)