code: def to_grayscale(
      img: Image.Image, num_output_channels: int
  ) -> Image.Image:
      if not _is_pil_image(img):
          raise TypeError(f"img should be PIL Image. Got {type(img)}")
  
      if num_output_channels == 1:
          img = img.convert("L")
      elif num_output_channels == 3:
          img = img.convert("L")
          np_img = np.array(img, dtype=np.uint8)
          np_img = np.dstack([np_img, np_img, np_img])
          img = Image.fromarray(np_img, "RGB")
      else:
          raise ValueError(
              "num_output_channels should be either 1 or 3"
          )
  
      return img
  
imports: import numbers
  from typing import Any, Dict, List, Literal, Optional, Sequence, Tuple, Union
  import numpy
  import torch
  from PIL import Image, ImageEnhance, ImageOps
label_desc: Use image node_id 19 for calling a global function with example usage: to_grayscale(img, num_output_channels) and returns: img