图片读取:Image.open(ImgPath)
·
from PIL import Image
利用 img = Image.open(ImgPath) 打开的图片是PIL类型的,它自带resize函数。由于pytorch的顺序是(batch,c,h,w),所以需要进行PIL类型到numpy类型转换,tensorflow,numpy的顺序是(batch,h,w,c):
# Load Image
img_fn = os.path.join(self.input_path, filenames)
img = Image.open(img_fn) # RGB(默认)
# 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)
# 将PIL类型转化成numpy类型
img = np.array(img).uint8() # H*W*C
或
# Load Image
img_fn = os.path.join(self.input_path, filenames)
img = Image.open(img_fn) # RGB(默认)
# img.show()
# 将PIL类型转化成numpy类型
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, ...]
# 改变图像大小并且隐藏归一化到0-255区间的操作
img = scipy.misc.imresize(img, [height, width], interp=interp)
return img
from scipy.misc import imread
利用 img = imread(ImgPath) 打开的图片虽然是numpy类型的,并且也是RGB格式。但是由于 scipy 在版本1.3.0后就取消了imread函数,如果下载低版本的scipy会与tensorflow出现各种不兼容现象,挺麻烦的
更多推荐
已为社区贡献5条内容
所有评论(0)