【发布时间】:2018-08-05 14:22:42
【问题描述】:
我有 100 个图像,每个数字 10 个,我正在尝试将其转换为 Python 中的 MNIST 图像。但是,我经常遇到错误。错误已发布!
from PIL import Image, ImageFilter
from os import listdir
def imageprepare(argv):
"""
This function returns the pixel values.
The imput is a png file location.
"""
imagesList = listdir(argv)
for image in imagesList:
im = Image.open(argv).convert('L')
width = float(im.size[0])
height = float(im.size[1])
newImage = Image.new('L', (28, 28), (255)) # creates white canvas of 28x28 pixels
if width > height: # check which dimension is bigger
# Width is bigger. Width becomes 20 pixels.
nheight = int(round((20.0 / width * height), 0)) # resize height according to ratio width
if (nheight == 0): # rare case but minimum is 1 pixel
nheight = 1
# resize and sharpen
img = im.resize((20, nheight), Image.ANTIALIAS).filter(ImageFilter.SHARPEN)
wtop = int(round(((28 - nheight) / 2), 0)) # calculate horizontal position
newImage.paste(img, (4, wtop)) # paste resized image on white canvas
else:
# Height is bigger. Heigth becomes 20 pixels.
nwidth = int(round((20.0 / height * width), 0)) # resize width according to ratio height
if (nwidth == 0): # rare case but minimum is 1 pixel
nwidth = 1
# resize and sharpen
img = im.resize((nwidth, 20), Image.ANTIALIAS).filter(ImageFilter.SHARPEN)
wleft = int(round(((28 - nwidth) / 2), 0)) # caculate vertical pozition
newImage.paste(img, (wleft, 4)) # paste resized image on white canvas
# newImage.save("sample.png
tv = list(newImage.getdata()) # get pixel values
# normalize pixels to 0 and 1. 0 is pure white, 1 is pure black.
tva = [(255 - x) * 1.0 / 255.0 for x in tv]
print(tva)
return tva
argv= 'images/'
x=imageprepare(argv)#file path here
print(len(x))# mnist IMAGES are 28x28=784 pixels
错误: 文件“C:/Users/lenovo/.spyder-py3/Project1/test12.py”,第 47 行,在 x=imageprepare(argv)#这里的文件路径
文件“C:/Users/lenovo/.spyder-py3/Project1/test12.py”,第 14 行,在 imageprepare 中 im = Image.open(argv).convert('L')
文件“C:\Users\lenovo\Anaconda3\lib\site-packages\PIL\Image.py”,第 2477 行,打开 fp = builtins.open(filename, "rb")
PermissionError: [Errno 13] Permission denied: 'images/'
【问题讨论】:
-
只需以管理员权限启动您的程序,看看是否有帮助。在这种情况下,如果您是从 anaconda 提示符运行的东西,请右键单击开始菜单中的“Anaconda Prompt”,然后选择“以管理员身份运行”。还可以考虑重新安装 anaconda,仅适用于当前用户。