Safemotion Lib
Loading...
Searching...
No Matches
data_utils.py
Go to the documentation of this file.
1# encoding: utf-8
2"""
3@author: liaoxingyu
4@contact: sherlockliao01@gmail.com
5"""
6import numpy as np
7from PIL import Image, ImageOps
8
9from fastreid.utils.file_io import PathManager
10
11
12def read_image(file_name, format=None):
13 """
14 Read an image into the given format.
15 Will apply rotation and flipping if the image has such exif information.
16 Args:
17 file_name (str): image file path
18 format (str): one of the supported image modes in PIL, or "BGR"
19 Returns:
20 image (np.ndarray): an HWC image
21 """
22 with PathManager.open(file_name, "rb") as f:
23 image = Image.open(f)
24
25 # capture and ignore this bug: https://github.com/python-pillow/Pillow/issues/3973
26 try:
27 image = ImageOps.exif_transpose(image)
28 except Exception:
29 pass
30
31 if format is not None:
32 # PIL only supports RGB, so convert to RGB and flip channels over below
33 conversion_format = format
34 if format == "BGR":
35 conversion_format = "RGB"
36 image = image.convert(conversion_format)
37 image = np.asarray(image)
38 if format == "BGR":
39 # flip channels if needed
40 image = image[:, :, ::-1]
41 # PIL squeezes out the channel dimension for "L", so make it HWC
42 if format == "L":
43 image = np.expand_dims(image, -1)
44 image = Image.fromarray(image)
45 return image
read_image(file_name, format=None)
Definition data_utils.py:12