code: def _testimpl_jpeg_compression_distortion(
      batch_size, device, quality, layout
  ):
      @pipeline_def(batch_size=batch_size, num_threads=3, device_id=0)
      def jpeg_distortion_pipe(device="cpu", quality=None):
          if layout == "FHWC":
              iii = InputImagesIter(sequence_length)
              in_tensors = fn.external_source(
                  source=iii, layout="FHWC", batch=False
              )
          else:
              encoded, _ = fn.readers.file(file_root=images_dir)
              in_tensors = fn.decoders.image(encoded, device="cpu")
  
          inputs = in_tensors.gpu() if device == "gpu" else in_tensors
          if quality is None:
              quality = fn.random.uniform(
                  range=[1, 99], dtype=types.INT32
              )
          out_tensors = fn.jpeg_compression_distortion(
              inputs, quality=quality
          )
          return (out_tensors, in_tensors, quality)
  
      pipe = jpeg_distortion_pipe(
          device=device,
          quality=quality,
          batch_size=batch_size,
          num_threads=2,
          device_id=0,
      )
      pipe.build()
      for _ in range(3):
          out = pipe.run()
          assert out[0].layout() == layout
          out_data = out[0].as_cpu() if device == "gpu" else out[0]
          in_data = out[1]
          quality = out[2]
          for i in range(batch_size):
              out_tensor = np.array(out_data[i])
              in_tensor = np.array(in_data[i])
              q = int(np.array(quality[i]))
              if layout == "FHWC":
                  for j in range(in_tensor.shape[0]):
                      _compare_to_cv_distortion(
                          in_tensor[j], out_tensor[j], q, (i, j)
                      )
              else:
                  _compare_to_cv_distortion(
                      in_tensor, out_tensor, q, (i, 0)
                  )
  
file_path: DALI/dali/test/python/operator_1/test_jpeg_compression_distortion.py
imports: from nvidia.dali import pipeline_def
  import nvidia.dali.fn
  import nvidia.dali.types
  from random import shuffle
  import numpy
  from test_utils import as_array
  import os
  import cv2
label_desc: Use image node_id 2 for calling a global function with example usage: _testimpl_jpeg_compression_distortion(batch_size, device, quality, layout) and returns: out_tensors,  in_tensors,  quality