Read an image from bytes. Args: content (bytes): Image bytes got from files or other streams. flag (str): Flags specifying the color type of a loaded image, candidates are `color`, `grayscale` and `unchanged`. float32 (bool): Whether to change to float32., If
(content, flag='color', float32=False)
| 112 | |
| 113 | |
| 114 | def imfrombytes(content, flag='color', float32=False): |
| 115 | """Read an image from bytes. |
| 116 | |
| 117 | Args: |
| 118 | content (bytes): Image bytes got from files or other streams. |
| 119 | flag (str): Flags specifying the color type of a loaded image, |
| 120 | candidates are `color`, `grayscale` and `unchanged`. |
| 121 | float32 (bool): Whether to change to float32., If True, will also norm |
| 122 | to [0, 1]. Default: False. |
| 123 | |
| 124 | Returns: |
| 125 | ndarray: Loaded image array. |
| 126 | """ |
| 127 | img_np = np.frombuffer(content, np.uint8) |
| 128 | imread_flags = {'color': cv2.IMREAD_COLOR, 'grayscale': cv2.IMREAD_GRAYSCALE, 'unchanged': cv2.IMREAD_UNCHANGED} |
| 129 | img = cv2.imdecode(img_np, imread_flags[flag]) |
| 130 | if float32: |
| 131 | img = img.astype(np.float32) / 255. |
| 132 | return img |
| 133 | |
| 134 | |
| 135 | def imwrite(img, file_path, params=None, auto_mkdir=True): |
no outgoing calls
no test coverage detected