1. 程式人生 > >cv2.imread不能正常讀取gif格式圖片

cv2.imread不能正常讀取gif格式圖片

    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.