【发布时间】:2019-09-26 20:39:28
【问题描述】:
我是 Python 的新手。 我想从多个图像中提取 RGB 值。我想使用每个图像的 RGB 值作为 K 折交叉验证的输入。
我只能获取一张图像的 RGB 值。所以我尝试使用以下代码从多个图像中获取:
from __future__ import with_statement
from PIL import Image
import glob
#Path to file
for img in glob.glob({Path}+"*.jpg"):
im = Image.open(img)
#Load the pixel info
pix = im.load()
#Get a tuple of the x and y dimensions of the image
width, height = im.size
#Open a file to write the pixel data
with open('output_file.csv', 'w+') as f:
f.write('R,G,B\n')
#Read the details of each pixel and write them to the file
for x in range(width):
for y in range(height):
r = pix[x,y][0]
g = pix[x,x][1]
b = pix[x,x][2]
f.write('{0},{1},{2}\n'.format(r,g,b))
我希望在 CSV 文件中得到这样的输入:
img_name,R,G,B
1.jpg,50,50,50
2.jpg,60,60,70
但实际输出的是包含 40000+ 行的 CSV 文件。
是否可以自动从多个图像中获取 RGB 值?
【问题讨论】:
标签: python csv rgb cross-validation converters