简体   繁体   English

从图像中获取RGB数据并写入对应像素的CSV文件

[英]Obtaining RGB data from image and writing it to CSV file with the corresponding pixel

I am experiencing some bugs with this code that is supposed to write the info from an image into a csv file.我遇到了此代码的一些错误,该代码应该将图像中的信息写入 csv 文件。

Currently I have this piece of code that writes all the pixels of the image [its X,Y coordinates to be exact] to a csv file.目前,我有这段代码将图像的所有像素 [确切地说是 X,Y 坐标] 写入 csv 文件。

import PIL
import numpy as np

img_grey = Image.open('walkingpath_005004.jpg').convert('L')
print(img_grey.size)
# (300, 241)
x = 3
y = 4
#pix = img_grey.load()
#print(pix[x,y])

# Taken from: https://stackoverflow.com/a/60783743/11089932
xy_coords = np.flip(np.column_stack(np.where(np.array(img_grey) >= 0)), axis=1)

# Add pixel numbers in front
pixel_numbers = np.expand_dims(np.arange(1, xy_coords.shape[0] + 1), axis=1)

#get rgb data
pixels = list(img_grey.getdata()) 
width, height = img_grey.size  
pixels = np.asarray(img_grey)

value = np.hstack([pixel_numbers, xy_coords, pixels])
print(value)
# [[    1     0     0]
#  [    2     1     0]
#  [    3     2     0]
#  ...
#  [72298   297   240]
#  [72299   298   240]
#  [72300   299   240]]

# Properly save as CSV
np.savetxt("outputdata.csv", value, delimiter='\t', fmt='%4d')

I need the RGB data of each pixel in 3 extra columns.我需要 3 个额外列中每个像素的 RGB 数据。 As such, the format should be因此,格式应该是
PixelNumber XY R GB

Currently faced with this bug: ValueError: all the input array dimensions for the concatenation axis must match exactly, but along dimension 0, the array at index 0 has size 258063 and the array at index 2 has size 509目前面临这个错误: ValueError: all the input array dimensions for the concatenation axis must match exactly, but along dimension 0, the array at index 0 has size 258063 and the array at index 2 has size 509

I was looking at this link How to read the RGB value of a given pixel in Python?我在看这个链接How to read the RGB value of a given pixel in Python?
but not sure how I can go about with incorporating it into my existing code.但不确定如何将 go 合并到我现有的代码中。

Any and all help is appreciated!任何和所有的帮助表示赞赏!

Thank you!谢谢!

The original solution for the xy_coords must be adapted since you're now dealing with three-channel images instead of single-channel images.必须调整xy_coords的原始解决方案,因为您现在处理的是三通道图像而不是单通道图像。 And, to get the RGB tuples for each pixel, you just need to reshape the NumPy array, you already have.而且,要获得每个像素的 RGB 元组,您只需重塑 NumPy 数组,您已经有了。

from PIL import Image
import numpy as np

img = Image.open('path/to/your/image.png')
print('Inspect a few pixels in the original image:')
for y in np.arange(3):
    for x in np.arange(3):
        print(x, y, img.getpixel((x, y)))

# Modified for RGB images from: https://stackoverflow.com/a/60783743/11089932
img_np = np.array(img)
xy_coords = np.flip(np.column_stack(np.where(np.all(img_np >= 0, axis=2))), axis=1)
rgb = np.reshape(img_np, (np.prod(img_np.shape[:2]), 3))

# Add pixel numbers in front
pixel_numbers = np.expand_dims(np.arange(1, xy_coords.shape[0] + 1), axis=1)
value = np.hstack([pixel_numbers, xy_coords, rgb])
print('\nCompare pixels in result:')
for y in np.arange(3):
    for x in np.arange(3):
        print(value[(value[:, 1] == x) & (value[:, 2] == y)])

# Properly save as CSV
np.savetxt("outputdata.csv", value, delimiter='\t', fmt='%4d')

The two outputs for comparison:比较的两个输出:

Inspect a few pixels in the original image:
0 0 (63, 124, 239)
1 0 (65, 123, 239)
2 0 (64, 124, 240)
0 1 (74, 128, 238)
1 1 (66, 122, 239)
2 1 (68, 125, 239)
0 2 (173, 200, 244)
1 2 (86, 134, 239)
2 2 (80, 132, 240)

Compare pixels in result:
[[  1   0   0  63 124 239]]
[[  2   1   0  65 123 239]]
[[  3   2   0  64 124 240]]
[[301   0   1  74 128 238]]
[[302   1   1  66 122 239]]
[[303   2   1  68 125 239]]
[[601   0   2 173 200 244]]
[[602   1   2  86 134 239]]
[[603   2   2  80 132 240]]
----------------------------------------
System information
----------------------------------------
Platform:      Windows-10-10.0.19041-SP0
Python:        3.9.1
PyCharm:       2021.1.1
NumPy:         1.19.5
Pillow:        8.2.0
----------------------------------------


I think you could try with glob.glob, what should help from an exactly coed perspective我认为您可以尝试使用 glob.glob,从完全男女同校的角度来看应该有什么帮助


import numpy as np
import glob
import cv2
import csv

Libraries ⬆️;图书馆⬆️; You know what⬇️你知道吗⬇️

image_list = []

for filename in glob.glob(r'C:\your path to\file*.png'):    # '*' will count files each by one
    
    #Read
    img = cv2.imread(filename)
    flattened = img.flatten() 
    print(flattened) # recommend to avoid duplicates, see files and so on.

    #Save
    with open('output2.csv', 'ab') as f: #ab is set 
            np.savetxt(f, flattened, delimiter=",")

Cheers 干杯

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM