如何使用imsave在python中保存调整大小的图像?

时间:2019-04-05 20:09:06

标签: python jupyter

我正在尝试遵循机器学习代码the code 并且不确定为什么尝试修改图像时会收到FileNotFoundError: [Errno 2] No such file or directory: 'new_imgs/0.png'。检查文件夹时,我会看到lfw.gz文件和包含所有图像的目录。在这种情况下,是否需要创建一个新文件夹并将该path =设置为文件路径,以保存要调整大小的图像?这是导致错误的代码部分。任何建议都会很棒!

url = "http://vis-www.cs.umass.edu/lfw/lfw.tgz"
filename = "lfw.tgz"
directory = "imgs"
new_dir = "new_imgs"
import urllib
import tarfile
import os
import tarfile
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.image import imread
from scipy.misc import imresize, imsave
import tensorflow as tf
%matplotlib inline


if not os.path.isdir(directory):
    if not os.path.isfile(filename):
        urllib.urlretrieve (url, filename)
    tar = tarfile.open(filename, "r:gz")
    tar.extractall(path=directory)
    tar.close()



filepaths = []
for dir_, _, files in os.walk(directory):
    for fileName in files:
        relDir = os.path.relpath(dir_, directory)
        relFile = os.path.join(relDir, fileName)
        filepaths.append(directory + "/" + relFile)

for i, fp in enumerate(filepaths):
    img = imread(fp) #/ 255.0
    img = imresize(img, (40, 40))
    imsave(new_dir + "/" + str(i) + ".png", img)

0 个答案:

没有答案