在看代码的时候,看到一行注释:use PIL, to be consistent with evaluation
 说是用PIL方法加载,却又看见了BGR这种表述,后面的调用也都是cv2格式:
说是用PIL方法加载,却又看见了BGR这种表述,后面的调用也都是cv2格式:

那我就要看下这里面是怎么实现的了,找到了read_image函数,如下:
def read_image(file_name, format=None):
    """
    Read an image into the given format.
    Will apply rotation and flipping if the image has such exif information.
    Args:
        file_name (str): image file path
        format (str): one of the supported image modes in PIL, or "BGR" or "YUV-BT.601".
    Returns:
        image (np.ndarray):
            an HWC image in the given format, which is 0-255, uint8 for
            supported image modes in PIL or "BGR"; float (0-1 for Y) for YUV-BT.601.
    """
    with PathManager.open(file_name, "rb") as f:
        image = Image.open(f)
        # work around this bug: https://github.com/python-pillow/Pillow/issues/3973
        image = _apply_exif_orientation(image)
        return convert_PIL_to_numpy(image, format)原来是用PIL打开后转成了numpy
然后还注意到image = _apply_exif_orientation(image)这句,不知道为了解决啥,幸好有个注释的issue:
# work around this bug: https://github.com/python-pillow/Pillow/issues/3973
  
看了下,就是说之前某些图片应用 exif_transpose 函数会报错,这里进行了解决。
def _apply_exif_orientation(image):
    """
    Applies the exif orientation correctly.
    This code exists per the bug:
      https://github.com/python-pillow/Pillow/issues/3973
    with the function `ImageOps.exif_transpose`. The Pillow source raises errors with
    various methods, especially `tobytes`
    Function based on:
      https://github.com/wkentaro/labelme/blob/v4.5.4/labelme/utils/image.py#L59
      https://github.com/python-pillow/Pillow/blob/7.1.2/src/PIL/ImageOps.py#L527
    Args:
        image (PIL.Image): a PIL image
    Returns:
        (PIL.Image): the PIL image with exif orientation applied, if applicable
    """
    if not hasattr(image, "getexif"):
        return image
    try:
        exif = image.getexif()
    except Exception:  # https://github.com/facebookresearch/detectron2/issues/1885
        exif = None
    if exif is None:
        return image
    orientation = exif.get(_EXIF_ORIENT)
    method = {
        2: Image.FLIP_LEFT_RIGHT,
        3: Image.ROTATE_180,
        4: Image.FLIP_TOP_BOTTOM,
        5: Image.TRANSPOSE,
        6: Image.ROTATE_270,
        7: Image.TRANSVERSE,
        8: Image.ROTATE_90,
    }.get(orientation)
    if method is not None:
        return image.transpose(method)
    return image顺便又复习了下exif是干啥的:
在图像处理中,
exif_orientation是一个EXIF(Exchangeable Image File Format)标签,它包含了关于图像方向的信息。这个标签对于正确显示图像的方向非常重要,尤其是在从相机导入照片到计算机或其他设备时。如果exif_orientation标签没有被正确处理,图像可能会以错误的方向显示,比如被旋转了90度、180度或270度。在Python的Pillow库(一个图像处理库,也称为PIL的更新版本)中,
exif_orientation的处理可能会导致图像显示不正确。这可能是由于以下原因:
错误的解析逻辑:Pillow可能没有正确解析EXIF数据中的
exif_orientation标签,导致无法正确识别图像的正确方向。
兼容性问题:随着相机和设备的发展,EXIF标准可能会有新的更新,Pillow可能需要更新以支持这些新的变化。
错误处理:在处理某些特定的EXIF数据时,Pillow可能没有正确处理异常情况,导致图像方向错误。
性能问题:修复
exif_orientation可能还包括优化代码,以提高处理大量图像时的性能。
用户反馈:用户在使用Pillow处理图像时遇到了方向问题,通过在GitHub上提出issue,开发者意识到需要修复这个问题以提高用户体验。
以前就出现过,读取的图像总是和原始图像的方向不一致,本质上是有旋转信息但是没应用过去,下次可以用这个来处理。
又是一个小细节,做个笔记,以上。



















