image.py 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. from enum import Enum
  2. from warnings import warn
  3. import torch
  4. from ..extension import _load_library
  5. from ..utils import _log_api_usage_once
  6. try:
  7. _load_library("image")
  8. except (ImportError, OSError) as e:
  9. warn(f"Failed to load image Python extension: {e}")
  10. class ImageReadMode(Enum):
  11. """
  12. Support for various modes while reading images.
  13. Use ``ImageReadMode.UNCHANGED`` for loading the image as-is,
  14. ``ImageReadMode.GRAY`` for converting to grayscale,
  15. ``ImageReadMode.GRAY_ALPHA`` for grayscale with transparency,
  16. ``ImageReadMode.RGB`` for RGB and ``ImageReadMode.RGB_ALPHA`` for
  17. RGB with transparency.
  18. """
  19. UNCHANGED = 0
  20. GRAY = 1
  21. GRAY_ALPHA = 2
  22. RGB = 3
  23. RGB_ALPHA = 4
  24. def read_file(path: str) -> torch.Tensor:
  25. """
  26. Reads and outputs the bytes contents of a file as a uint8 Tensor
  27. with one dimension.
  28. Args:
  29. path (str): the path to the file to be read
  30. Returns:
  31. data (Tensor)
  32. """
  33. if not torch.jit.is_scripting() and not torch.jit.is_tracing():
  34. _log_api_usage_once(read_file)
  35. data = torch.ops.image.read_file(path)
  36. return data
  37. def write_file(filename: str, data: torch.Tensor) -> None:
  38. """
  39. Writes the contents of a uint8 tensor with one dimension to a
  40. file.
  41. Args:
  42. filename (str): the path to the file to be written
  43. data (Tensor): the contents to be written to the output file
  44. """
  45. if not torch.jit.is_scripting() and not torch.jit.is_tracing():
  46. _log_api_usage_once(write_file)
  47. torch.ops.image.write_file(filename, data)
  48. def decode_png(input: torch.Tensor, mode: ImageReadMode = ImageReadMode.UNCHANGED) -> torch.Tensor:
  49. """
  50. Decodes a PNG image into a 3 dimensional RGB or grayscale Tensor.
  51. Optionally converts the image to the desired format.
  52. The values of the output tensor are uint8 in [0, 255].
  53. Args:
  54. input (Tensor[1]): a one dimensional uint8 tensor containing
  55. the raw bytes of the PNG image.
  56. mode (ImageReadMode): the read mode used for optionally
  57. converting the image. Default: ``ImageReadMode.UNCHANGED``.
  58. See `ImageReadMode` class for more information on various
  59. available modes.
  60. Returns:
  61. output (Tensor[image_channels, image_height, image_width])
  62. """
  63. if not torch.jit.is_scripting() and not torch.jit.is_tracing():
  64. _log_api_usage_once(decode_png)
  65. output = torch.ops.image.decode_png(input, mode.value, False)
  66. return output
  67. def encode_png(input: torch.Tensor, compression_level: int = 6) -> torch.Tensor:
  68. """
  69. Takes an input tensor in CHW layout and returns a buffer with the contents
  70. of its corresponding PNG file.
  71. Args:
  72. input (Tensor[channels, image_height, image_width]): int8 image tensor of
  73. ``c`` channels, where ``c`` must 3 or 1.
  74. compression_level (int): Compression factor for the resulting file, it must be a number
  75. between 0 and 9. Default: 6
  76. Returns:
  77. Tensor[1]: A one dimensional int8 tensor that contains the raw bytes of the
  78. PNG file.
  79. """
  80. if not torch.jit.is_scripting() and not torch.jit.is_tracing():
  81. _log_api_usage_once(encode_png)
  82. output = torch.ops.image.encode_png(input, compression_level)
  83. return output
  84. def write_png(input: torch.Tensor, filename: str, compression_level: int = 6):
  85. """
  86. Takes an input tensor in CHW layout (or HW in the case of grayscale images)
  87. and saves it in a PNG file.
  88. Args:
  89. input (Tensor[channels, image_height, image_width]): int8 image tensor of
  90. ``c`` channels, where ``c`` must be 1 or 3.
  91. filename (str): Path to save the image.
  92. compression_level (int): Compression factor for the resulting file, it must be a number
  93. between 0 and 9. Default: 6
  94. """
  95. if not torch.jit.is_scripting() and not torch.jit.is_tracing():
  96. _log_api_usage_once(write_png)
  97. output = encode_png(input, compression_level)
  98. write_file(filename, output)
  99. def decode_jpeg(
  100. input: torch.Tensor, mode: ImageReadMode = ImageReadMode.UNCHANGED, device: str = "cpu"
  101. ) -> torch.Tensor:
  102. """
  103. Decodes a JPEG image into a 3 dimensional RGB or grayscale Tensor.
  104. Optionally converts the image to the desired format.
  105. The values of the output tensor are uint8 between 0 and 255.
  106. Args:
  107. input (Tensor[1]): a one dimensional uint8 tensor containing
  108. the raw bytes of the JPEG image. This tensor must be on CPU,
  109. regardless of the ``device`` parameter.
  110. mode (ImageReadMode): the read mode used for optionally
  111. converting the image. Default: ``ImageReadMode.UNCHANGED``.
  112. See ``ImageReadMode`` class for more information on various
  113. available modes.
  114. device (str or torch.device): The device on which the decoded image will
  115. be stored. If a cuda device is specified, the image will be decoded
  116. with `nvjpeg <https://developer.nvidia.com/nvjpeg>`_. This is only
  117. supported for CUDA version >= 10.1
  118. .. betastatus:: device parameter
  119. .. warning::
  120. There is a memory leak in the nvjpeg library for CUDA versions < 11.6.
  121. Make sure to rely on CUDA 11.6 or above before using ``device="cuda"``.
  122. Returns:
  123. output (Tensor[image_channels, image_height, image_width])
  124. """
  125. if not torch.jit.is_scripting() and not torch.jit.is_tracing():
  126. _log_api_usage_once(decode_jpeg)
  127. device = torch.device(device)
  128. if device.type == "cuda":
  129. output = torch.ops.image.decode_jpeg_cuda(input, mode.value, device)
  130. else:
  131. output = torch.ops.image.decode_jpeg(input, mode.value)
  132. return output
  133. def encode_jpeg(input: torch.Tensor, quality: int = 75) -> torch.Tensor:
  134. """
  135. Takes an input tensor in CHW layout and returns a buffer with the contents
  136. of its corresponding JPEG file.
  137. Args:
  138. input (Tensor[channels, image_height, image_width])): int8 image tensor of
  139. ``c`` channels, where ``c`` must be 1 or 3.
  140. quality (int): Quality of the resulting JPEG file, it must be a number between
  141. 1 and 100. Default: 75
  142. Returns:
  143. output (Tensor[1]): A one dimensional int8 tensor that contains the raw bytes of the
  144. JPEG file.
  145. """
  146. if not torch.jit.is_scripting() and not torch.jit.is_tracing():
  147. _log_api_usage_once(encode_jpeg)
  148. if quality < 1 or quality > 100:
  149. raise ValueError("Image quality should be a positive number between 1 and 100")
  150. output = torch.ops.image.encode_jpeg(input, quality)
  151. return output
  152. def write_jpeg(input: torch.Tensor, filename: str, quality: int = 75):
  153. """
  154. Takes an input tensor in CHW layout and saves it in a JPEG file.
  155. Args:
  156. input (Tensor[channels, image_height, image_width]): int8 image tensor of ``c``
  157. channels, where ``c`` must be 1 or 3.
  158. filename (str): Path to save the image.
  159. quality (int): Quality of the resulting JPEG file, it must be a number
  160. between 1 and 100. Default: 75
  161. """
  162. if not torch.jit.is_scripting() and not torch.jit.is_tracing():
  163. _log_api_usage_once(write_jpeg)
  164. output = encode_jpeg(input, quality)
  165. write_file(filename, output)
  166. def decode_image(input: torch.Tensor, mode: ImageReadMode = ImageReadMode.UNCHANGED) -> torch.Tensor:
  167. """
  168. Detects whether an image is a JPEG or PNG and performs the appropriate
  169. operation to decode the image into a 3 dimensional RGB or grayscale Tensor.
  170. Optionally converts the image to the desired format.
  171. The values of the output tensor are uint8 in [0, 255].
  172. Args:
  173. input (Tensor): a one dimensional uint8 tensor containing the raw bytes of the
  174. PNG or JPEG image.
  175. mode (ImageReadMode): the read mode used for optionally converting the image.
  176. Default: ``ImageReadMode.UNCHANGED``.
  177. See ``ImageReadMode`` class for more information on various
  178. available modes.
  179. Returns:
  180. output (Tensor[image_channels, image_height, image_width])
  181. """
  182. if not torch.jit.is_scripting() and not torch.jit.is_tracing():
  183. _log_api_usage_once(decode_image)
  184. output = torch.ops.image.decode_image(input, mode.value)
  185. return output
  186. def read_image(path: str, mode: ImageReadMode = ImageReadMode.UNCHANGED) -> torch.Tensor:
  187. """
  188. Reads a JPEG or PNG image into a 3 dimensional RGB or grayscale Tensor.
  189. Optionally converts the image to the desired format.
  190. The values of the output tensor are uint8 in [0, 255].
  191. Args:
  192. path (str): path of the JPEG or PNG image.
  193. mode (ImageReadMode): the read mode used for optionally converting the image.
  194. Default: ``ImageReadMode.UNCHANGED``.
  195. See ``ImageReadMode`` class for more information on various
  196. available modes.
  197. Returns:
  198. output (Tensor[image_channels, image_height, image_width])
  199. """
  200. if not torch.jit.is_scripting() and not torch.jit.is_tracing():
  201. _log_api_usage_once(read_image)
  202. data = read_file(path)
  203. return decode_image(data, mode)
  204. def _read_png_16(path: str, mode: ImageReadMode = ImageReadMode.UNCHANGED) -> torch.Tensor:
  205. data = read_file(path)
  206. return torch.ops.image.decode_png(data, mode.value, True)