code: def image_compression(img, quality, image_type):
      if image_type in [".jpeg", ".jpg"]:
          quality_flag = cv2.IMWRITE_JPEG_QUALITY
      elif image_type == ".webp":
          quality_flag = cv2.IMWRITE_WEBP_QUALITY
      else:
          NotImplementedError(
              "Only '.jpg' and '.webp' compression transforms are implemented. "
          )
  
      input_dtype = img.dtype
      needs_float = False
  
      if input_dtype == np.float32:
          warn(
              "Image compression augmentation "
              "is most effective with uint8 inputs, "
              "{} is used as input.".format(input_dtype),
              UserWarning,
          )
          img = from_float(img, dtype=np.dtype("uint8"))
          needs_float = True
      elif input_dtype not in (np.uint8, np.float32):
          raise ValueError(
              "Unexpected dtype {} for image augmentation".format(
                  input_dtype
              )
          )
  
      _, encoded_img = cv2.imencode(
          image_type, img, (int(quality_flag), quality)
      )
      img = cv2.imdecode(encoded_img, cv2.IMREAD_UNCHANGED)
  
      if needs_float:
          img = to_float(img, max_value=255)
      return img
  
imports: from __future__ import division
  from typing import Optional, Sequence, Union
  from warnings import warn
  import cv2
  import numpy
  import skimage
  from albumentations import random_utils
  from albumentations.augmentations.utils import MAX_VALUES_BY_DTYPE, _maybe_process_in_chunks, clip, clipped, ensure_contiguous, is_grayscale_image, is_rgb_image, non_rgb_warning, preserve_channel_dim, preserve_shape
label_desc: Use image node_id 20 for calling a global function with example usage: image_compression(img, quality, image_type) and returns: img