【问题标题】:How to Extract Single RGB Channel from Multiple Images Python如何从多个图像 Python 中提取单个 RGB 通道
【发布时间】: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


    【解决方案1】:

    您的代码当前正在将每个像素的值作为单独的一行写入 CSV 文件中,因此您可能有大量行。

    要处理多个文件,您需要稍微重新排列代码并在循环中缩进写入文件。使用 Python 的 CSV 库来编写 CSV 文件可能也是一个好主意,以防万一您的任何文件名包含逗号。如果发生这种情况,它会正确地将该字段用引号括起来。

    from PIL import Image
    import glob
    import os
    import csv
    
    #Open a file to write the pixel data
    with open('output_file.csv', 'w', newline='') as f_output:
        csv_output = csv.writer(f_output)
        csv_output.writerow(["img_name", "R", "G", "B"])
    
        #Path to file 
        for filename in glob.glob("*.jpg"):
            im = Image.open(filename) 
            img_name = os.path.basename(filename)
    
            #Load the pixel info
            pix = im.load()
    
            #Get a tuple of the x and y dimensions of the image
            width, height = im.size
    
            print(f'{filename}, Width {width}, Height {height}') # show progress
    
            #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,y][1]
                    b = pix[x,y][2]
                    csv_output.writerow([img_name, r, g, b])
    

    注意:获取 r g b 值也存在问题,[x,x] 有两种情况。


    正如@GiacomoCatenazzi 所述,您的循环也可以删除:

    from itertools import product
    from PIL import Image
    import glob
    import os
    import csv
    
    #Open a file to write the pixel data
    with open('output_file.csv', 'w', newline='') as f_output:
        csv_output = csv.writer(f_output)
        csv_output.writerow(["img_name", "R", "G", "B"])
    
        #Path to file 
        for filename in glob.glob("*.jpg"):
            im = Image.open(filename) 
            img_name = os.path.basename(filename)
    
            #Load the pixel info
            pix = im.load()
    
            #Get a tuple of the x and y dimensions of the image
            width, height = im.size
    
            print(f'{filename}, Width {width}, Height {height}') # show 
    
            #Read the details of each pixel and write them to the file
            csv_output.writerows([img_name, *pix[x,y]] for x, y in product(range(width), range(height)))
    

    【讨论】:

    • *pix[x,y] 而不是 r,g,b,因此您可以再删除 3 行,并且您可以看到可以使用迭代器(和 writerows 并删除循环。
    • 我已经尝试过该代码,但 csv 输出仅包含第一行 img_name、R、G、B。当迭代#Path to file running 时,csv 文件的状态为关闭。这段代码有什么问题?
    • 您是否尝试过使用不同的源图像?我已经对大约 5 个 JPG 文件进行了测试,它产生了大量的行。
    • 尝试添加print(height, width)
    • 但是 csv 文件已关闭,这就是迭代不起作用的原因。
    猜你喜欢
    • 2019-12-15
    • 2012-02-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-28
    • 1970-01-01
    • 2015-06-25
    相关资源
    最近更新 更多