python cv2读取图片/图片转码/切图/图片格式转换/替换图片内容

import base64
import cv2
import numpy as np
from io import BytesIO


class ImageOperation(object):
    def __init__(self):
        pass

    @staticmethod
    def cv2_base64(image):
        """
        cv2.imdecode()函数从指定的内存缓存中读取数据,并把数据转换(解码)成图像格式;主要用于从网络传输数据中恢复出图像。
        cv2.imencode()函数是将图片格式转换(编码)成流数据,赋值到内存缓存中;主要用于图像数据格式的压缩,方便网络传输。
        """
        base64_str = cv2.imencode('.jpg', image)[1].tostring()
        base64_str = base64.b64encode(base64_str)
        return base64_str

    def base64_to_bytes(self, base64_str):
        """
        base64 TO bytes
        """
        return base64.urlsafe_b64decode(base64_str)

    @staticmethod
    def image_buffer_to_cv2_img(image_buffer):
        """
        cv2.imencode()函数是将图片格式转换(编码)成流数据,赋值到内存缓存中;主要用于图像数据格式的压缩,方便网络传输。
        # npmpy.frombuffer将data以流的形式读入转化成ndarray对象
        # 第一参数为stream,第二参数为返回值的数据类型,第三参数指定从stream的第几位开始读入
        """
        img = cv2.imdecode(np.frombuffer(image_buffer, np.uint8), cv2.IMREAD_COLOR)
        return img

    def image_spilt_rect(self, img, face_rect):
        """
        调用切图并合成base64
        """
        img_base64_bytes = self.split_image(img, face_rect)
        rect_image_str = self.gen_base_64_str_by_bytes(img_base64_bytes)
        return rect_image_str

    @staticmethod
    def gen_base_64_str_by_bytes(img_base64_bytes):
        """
        合成图片的base64编码
        """
        image_str = f'data:image/png;base64,{str(img_base64_bytes, encoding="utf-8")}'
        return image_str

    def split_image(self, img, spilt_rect):
        """
        切图
        """
        # spilt_rect: [x,y,w,h]
        x, y, w, h = spilt_rect
        split_image = img[y:y + h, x:x + w]
        img_base64_str = self.cv2_base64(split_image)
        return img_base64_str

    @staticmethod
    def replace_img_rect(img, replace_img, rect_area):
        """
        替换图片中的某区域
        """
        x, y, w, h = rect_area
        # print(x, y, w, h, type(replace_img), type(img))
        img[y:y + h, x:x + w] = replace_img
        return img

    def color_imgread(self, img_path):
        """
        彩色读取
        """
        # img = cv2.imread(img_path, cv2.IMREAD_COLOR)
        img = cv2.imread(img_path, 1)
        return img

    def grayscale_imgread(self, img_path):
        """
        灰度读取
        """
        # img = cv2.imread(img_path, cv2.IMREAD_GRAYSCALE)
        img = cv2.imread(img_path, 0)
        return img


def get_video_duration(filename):
    """
    python获取视频文件时长
    """
    cap = cv2.VideoCapture(filename)
    if cap.isOpened():
        rate = cap.get(5)
        frame_num =cap.get(7)
        duration = frame_num/rate
        return duration
    return -1


if __name__ == '__main__':
    pass
    # pic_ope = ImageOperation()
    #
    # # 二进制读取图片
    # with open('./2.jpeg', 'rb') as f:
    #     img_bytes = f.read()
    # print(f"img_bytes = {img_bytes}")
    #
    # # 将图片格式转换(编码)成流数据,赋值到内存缓存中
    # cv2_img = pic_ope.image_buffer_to_cv2_img(img_bytes)
    # print(f"cv2_img = {cv2_img}")
    #
    # # 从图片中切下一部分
    # big_area = [319, 55, 84, 117]
    # face_area = [340, 84, 42, 59]
    # split_res = pic_ope.image_spilt_rect(cv2_img, big_area)
    # print(f"split_res = {split_res}")
    #
    # # 读取另一张图,切图
    # with open('./1.png', 'rb') as f:
    #     img_bytes2 = f.read()
    # print(f"img_bytes2 = {img_bytes2}")
    #
    # # 将图片格式转换(编码)成流数据,赋值到内存缓存中
    # cv2_img2 = pic_ope.image_buffer_to_cv2_img(img_bytes2)
    # print(f"cv2_img2 = {cv2_img2}")
    #
    # # 从图片中切下一部分
    # big_area = [319, 55, 84, 117]
    # split_res2 = pic_ope.split_image(cv2_img2, big_area)
    # print(f"split_res2 = {split_res2}")
    # split_res2_bytes = pic_ope.base64_to_bytes(split_res2)
    # print(f"split_res2_bytes = {split_res2_bytes}")
    #
    # split_res2_cv2_img = pic_ope.image_buffer_to_cv2_img(split_res2_bytes)
    # print(f"split_res2_cv2_img = {split_res2_cv2_img}")
    #
    # # 替换图片的某部分
    # img = pic_ope.replace_img_rect(cv2_img, split_res2_cv2_img, big_area)
    # print(f"img = {img}")
    # output_str = pic_ope.cv2_base64(img)
    # print(f"output_str = {output_str}")
    #
    # # 转换成base64编码
    # res = pic_ope.gen_base_64_str_by_bytes(output_str)
    # print(f"res = {res}")

    file_path = './output (1).mp4'
    res = get_video_duration(file_path)
    print(f"视频时长: {res} s")


你可能感兴趣的:(python cv2读取图片/图片转码/切图/图片格式转换/替换图片内容)