问题记录 | 记录PIL中Image.save的一个坑

Image.save然后open数值是会变的

我找了一个下午终于找出问题所在,PIL的Image库中把图片resize了之后存在本地然后再读进来,与直接resize后的数值是不一样的。

data_val:
 [[[[175 104  78]
   [174 102  77]
   [178 106  81]

resize_progress: 
[[[175 104  78]
  [174 102  77]
  [178 106  81]
  ...

IMG_read_again resize_progress:
 [[[182 105  75]
  [182 105  75]
  [182 105  75]
  ...

数组太长了,只放一部分上博客。
函数写成如下:

    def resize_img(self, img_path):
        dirname = os.path.dirname(img_path)
        img_basename=os.path.basename(img_path)
        tmp_dir = os.path.join(dirname,'resize/')
        if not os.path.exists(tmp_dir):
            os.makedirs(tmp_dir)
        img = Image.open(img_path)
        img = img.resize((self.img_row, self.img_col), Image.ANTIALIAS)
        print("resize_progress:",np.array(img))
        resize_img_path = os.path.join(tmp_dir,img_basename)
        img.save(resize_img_path)
        self.img_path = resize_img_path
        IMG_read_again = Image.open(resize_img_path)
        print("IMG_read_again resize_progress:\n",np.array(IMG_read_again))
        return resize_img_path

猜你喜欢

转载自www.cnblogs.com/ManWingloeng/p/11537192.html