cv2.imread不能正常读取gif格式图片

本文介绍使用Python处理GIF格式图片的方法。由于OpenCV的imread函数无法直接读取GIF图片,本文提供了一个解决方案:利用imageio库的mimread函数来实现GIF图片的读取。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

    Python中cv2模块的imread函数呆以正常读取'jpg','png'格式的图片,但是不能处理'gif'图片。可以改用imageio模块来处理。

import cv2
import imageio

def readImg(im_fn):
    im = cv2.imread(im_fn)
    if im is None :
        print('{} cv2.imread failed'.format(im_fn))
        tmp = imageio.mimread(im_fn)
        if tmp is not None:
            imt = np.array(tmp)
            imt = imt[0]
            im = imt[:,:,0:3]
    return im
help(imageio.mimread)
mimread(uri, format=None, memtest=True, **kwargs)
    mimread(uri, format=None, memtest=True, **kwargs)
    #可以同时读取多个文件,返回num arrays列表
    Reads multiple images from the specified file. Returns a list of
    numpy arrays, each with a dict of meta data at its 'meta' attribute.

    Parameters
    ----------
    uri : {str, bytes, file}
        The resource to load the images from, e.g. a filename, http address or
        file object, see the docs for more info.
    format : str
        The format to use to read the file. By default imageio selects
        the appropriate for you based on the filename and its contents.
    memtest : bool
        If True (default), this function will raise an error if the
        resulting list of images consumes over 256 MB of memory. This
        is to protect the system using so much memory that it needs to
        resort to swapping, and thereby stall the computer. E.g.
        ``mimread('hunger_games.avi')``.
    kwargs : ...
        Further keyword arguments are passed to the reader. See :func:`.help`
        to see what arguments are available for a particular format.


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值