当前位置:网站首页>Image reading: image open(ImgPath)

Image reading: image open(ImgPath)

2022-06-23 16:15:00 Growth of code Xiaobai

from PIL import Image

utilize img = Image.open(ImgPath) The open picture is PIL Type of , It comes with resize function . because pytorch The order is (batch,c,h,w), So we need to do PIL Type to numpy Type conversion ,tensorflow,numpy The order is (batch,h,w,c):

  # Load Image
  img_fn = os.path.join(self.input_path, filenames)
  img = Image.open(img_fn)  # RGB( Default )
  # img.show()

  # resize/crop if needed:[128*128*3]
  if self.input_size != 0:
      height = width = self.input_size
      img = img.resize((height, width), Image.BILINEAR)

  #  take PIL Type conversion to numpy type 
  img = np.array(img).uint8()    # H*W*C

or

 # Load Image
  img_fn = os.path.join(self.input_path, filenames)
  img = Image.open(img_fn)  # RGB( Default )
  # img.show()
  #  take PIL Type conversion to numpy type 
  img = np.array(img).uint8()    # H*W*C
 
  # resize/crop if needed:[128*128*3]
  if self.input_size != 0:
      height = width = self.input_size
      img = self.resize(img, height, width)

 def resize(self, img, height, width, centerCrop=True, interp='bilinear'):
     imgh, imgw = img.shape[0:2]
     if centerCrop and imgh != imgw:
         # center crop
         side = np.minimum(imgh, imgw)
         j = (imgh - side) // 2
         i = (imgw - side) // 2
         img = img[j:j + side, i:i + side, ...]
     #  Change image size and hide normalized to 0-255 Section operation 
     img = scipy.misc.imresize(img, [height, width], interp=interp)
     return img

from scipy.misc import imread

utilize img = imread(ImgPath) The open picture is numpy Type of , And it's also RGB Format . But because of scipy In version 1.3.0 Then it was canceled imread function , If you download a lower version scipy Will be with tensorflow There are various incompatibilities , It's troublesome

原网站

版权声明
本文为[Growth of code Xiaobai]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/174/202206231508506720.html