欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

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

程序员文章站 2022-05-16 11:20:38
...

    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.


相关标签: cv2 gif