python获取gif的最后一帧保存成图片

2022-05-11 11:34:08 浏览数 (1)

最近业务碰到了一个标注任务,获取gif图的最后一张然后调用内部的模型进行识别看gif是不是同样的内容,用脚本的方法试试

获取gif的最后一张

代码语言:javascript复制
import os, imageio
# 获取gif图的最后一帧,并保存png
def get_gif_last_frame(gif_path):
    image_path = gif_path.replace(".gif", ".png")
    if os.path.exists(image_path):
        return image_path
    else:
        try:
            os.remove(image_path)
        except Exception as e:
            print("Error removing file:", e)
        with imageio.get_reader(gif_path) as reader:
            last_frame = reader.get_data(reader.get_length() - 1)
            imageio.imwrite(image_path, last_frame)
        return image_path

调用api(因为我们公司内部开发的,所以给大家推荐开源的百度飞浆的模型)

安装依赖

代码语言:javascript复制
python版本(尽量3.6 )
pip3 install paddlepaddle -i https://mirror.baidu.com/pypi/simple
pip3 install paddlehub -i https://mirror.baidu.com/pypi/simple
pip3 install opencv-python -i https://mirror.baidu.com/pypi/simple

调用代码

代码语言:javascript复制
import paddlehub as hub
import cv2
image_path = r""
ocr = hub.Module(name="chinese_ocr_db_crnn_mobile")
result = ocr.recognize_text(images=[cv2.imread(image_path)])

# or
# result = ocr.recognize_text(paths=[image_path])
print(result)

0 人点赞