from PIL import Image
import numpy as np
import sys
import os
import csv
def createFileList(myDir, format='.jpg'):
fileList = []
print(myDir)
for root, dirs, files in os.walk(myDir, topdown=False):
for name in files:
if name.endswith(format):
fullName = os.path.join(root, name)
fileList.append(fullName)
return fileList
# load the original image
myFileList = createFileList('path/to/directory/')
for file in fileList:
print(file)
img_file = Image.open(file)
# get original image parameters...
width, height = img_file.size
format = img_file.format
mode = img_file.mode
# Make image Greyscale
img_grey = img_file.convert('L')
value = np.asarray(img_grey.getdata(), dtype=np.int).reshape((img_grey.size[1], img_grey.size[0]))
value = value.flatten()
print(value)
with open("img_pixels.csv", 'a') as f:
writer = csv.writer(f)
writer.writerow(value)
安装枕头,纽比,熊猫
示例工作代码如下
我有一个简单的例子 我希望这对你有帮助。在
您不需要编写任何代码,只需在macOS、Linux或Windows上的命令行上使用vips。在
因此,在终端中(或命令提示符,如果在Windows上):
将256x256灰度图像
TM.AN1.190.tiff
转换为256行的CSV,每行有256个条目。傻瓜!在如果要用逗号替换制表符分隔符,可以执行以下操作:
^{pr2}$相关问题 更多 >
编程相关推荐