parameters: (pic)
docstring: Function for turning Image.Image or np.ndarray (HWC) to jt.Var (CHW).
      Args::
          [in] img(PIL Image.Image or np.ndarray): Input image.
               If input type is np.ndarray, the shape should be in HWC.
      Return:
          [out] jt.Var in shape CHW.
      Example::
          
          img = Image.open(...)
          img_ = transform.to_tensor(img)
docstring_summary: Function for turning Image.Image or np.ndarray (HWC) to jt.Var (CHW).
      Args::
          [in] img(PIL Image.Image or np.ndarray): Input image.
               If input type is np.ndarray, the shape should be in HWC.
      Return:
          [out] jt.Var in shape CHW.
      Example::
          
          img = Image.open(...)
          img_ = transform.to_tensor(img)
docstring_tokens: Function for turning Image . Image or np . ndarray ( HWC ) to jt . Var ( CHW ) . Args :: [ in ] img ( PIL Image . Image or np . ndarray ) : Input image . If input type is np . ndarray the shape should be in HWC . Return : [ out ] jt . Var in shape CHW . Example :: img = Image . open ( ... ) img_ = transform . to_tensor ( img )
function: def _to_jittor_array(pic):
      """
      Function for turning Image.Image or np.ndarray (HWC) to jt.Var (CHW).
      Args::
          [in] img(PIL Image.Image or np.ndarray): Input image.
               If input type is np.ndarray, the shape should be in HWC.
      Return:
          [out] jt.Var in shape CHW.
      Example::
          
          img = Image.open(...)
          img_ = transform.to_tensor(img)
      """
      if not(F_pil._is_pil_image(pic) or _is_numpy(pic)):
          raise TypeError(f'img should be PIL Image or ndarray. Got {type(pic)}.')
  
      if _is_numpy(pic) and not _is_numpy_image(pic):
          raise ValueError(f'img should be 2/3 dimensional. Got {pic.ndim} dimensions.')
  
      if _is_numpy(pic):
          # handle numpy array
          if pic.ndim == 2:
              pic = pic[:, :, None]
  
          img = jt.array(pic.transpose((2, 0, 1)))
          # backward compatibility
          if img.dtype == 'uint8':
              return img.float().divide(255)
          else:
              return img
  
      # handle PIL Image
      if pic.mode == 'I':
          img = jt.array(np.array(pic, np.int32, copy=False))
      elif pic.mode == 'I;16':
          img = jt.array(np.array(pic, np.int16, copy=False))
      elif pic.mode == 'F':
          img = jt.array(np.array(pic, np.float32, copy=False))
      elif pic.mode == '1':
          img = jt.array(np.array(pic, np.uint8, copy=False) * 255, dtype='uint8')
      else:
          img = jt.array(np.array(pic, np.uint8, copy=False))
  
      # put it from HWC to CHW format
      img = img.reshape(pic.size[1], pic.size[0], len(pic.getbands()))
      img = img.permute((2, 0, 1))
      if img.dtype == 'uint8':
          return img.float().divide(255)
      else:
          return img
function_tokens: def _to_jittor_array ( pic ) : if not ( F_pil . _is_pil_image ( pic ) or _is_numpy ( pic ) ) : raise TypeError ( f'img should be PIL Image or ndarray. Got {type(pic)}.' ) if _is_numpy ( pic ) and not _is_numpy_image ( pic ) : raise ValueError ( f'img should be 2/3 dimensional. Got {pic.ndim} dimensions.' ) if _is_numpy ( pic ) : # handle numpy array if pic . ndim == 2 : pic = pic [ : , : , None ] img = jt . array ( pic . transpose ( ( 2 , 0 , 1 ) ) ) # backward compatibility if img . dtype == 'uint8' : return img . float ( ) . divide ( 255 ) else : return img # handle PIL Image if pic . mode == 'I' : img = jt . array ( np . array ( pic , np . int32 , copy = False ) ) elif pic . mode == 'I;16' : img = jt . array ( np . array ( pic , np . int16 , copy = False ) ) elif pic . mode == 'F' : img = jt . array ( np . array ( pic , np . float32 , copy = False ) ) elif pic . mode == '1' : img = jt . array ( np . array ( pic , np . uint8 , copy = False ) * 255 , dtype = 'uint8' ) else : img = jt . array ( np . array ( pic , np . uint8 , copy = False ) ) # put it from HWC to CHW format img = img . reshape ( pic . size [ 1 ] , pic . size [ 0 ] , len ( pic . getbands ( ) ) ) img = img . permute ( ( 2 , 0 , 1 ) ) if img . dtype == 'uint8' : return img . float ( ) . divide ( 255 ) else : return img