Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/292.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Python中将直方图数据保存在CSV文件中?_Python_Csv_Matplotlib_Histogram - Fatal编程技术网

如何在Python中将直方图数据保存在CSV文件中?

如何在Python中将直方图数据保存在CSV文件中?,python,csv,matplotlib,histogram,Python,Csv,Matplotlib,Histogram,从下面的python代码中,我可以绘制颜色的三个颜色分量的直方图。但是我想把这个直方图数据保存到一个CSV文件中,以便将来得到这个直方图。我该怎么做 from PIL import Image import matplotlib.pyplot as plt def getRed(redVal): return '#%02x%02x%02x' % (redVal, 0, 0) def getGreen(greenVal): return '#%02x%02x%02x' % (0

从下面的python代码中,我可以绘制颜色的三个颜色分量的直方图。但是我想把这个直方图数据保存到一个CSV文件中,以便将来得到这个直方图。我该怎么做

from PIL import Image
import matplotlib.pyplot as plt

def getRed(redVal):
    return '#%02x%02x%02x' % (redVal, 0, 0)

def getGreen(greenVal):
    return '#%02x%02x%02x' % (0, greenVal, 0)

def getBlue(blueVal):
    return '#%02x%02x%02x' % (0, 0, blueVal)

# Create an Image with specific RGB value
image = Image.open("baboon.ppm")
# Modify the color of two pixels
image.putpixel((0,1), (1,1,5))
image.putpixel((0,2), (2,1,5))

# Display the image
#image.show()
# Get the color histogram of the image
histogram = image.histogram()
# Take only the Red counts
l1 = histogram[0:256]
# Take only the Blue counts
l2 = histogram[256:512]
# Take only the Green counts
l3 = histogram[512:768]
plt.figure(0)
# R histogram
for i in range(0, 256):
    plt.bar(i, l1[i], color = getRed(i), edgecolor=getRed(i), alpha=0.3)

# G histogram
plt.figure(1)
for i in range(0, 256):
    plt.bar(i, l2[i], color = getGreen(i), edgecolor=getGreen(i),alpha=0.3)


# B histogram
plt.figure(2)
for i in range(0, 256):
    plt.bar(i, l3[i], color = getBlue(i), edgecolor=getBlue(i),alpha=0.3)

plt.show()

这是红色通道的输出,l1、l2和l3只是列表。
您可以将其转换为一个字符串并将该字符串写入纯文本文件,例如:

def writeCsv( RList, GList, BList ):
    outString = '\r\n'.join([
        ';'.join( map( str, RList ) ),
        ';'.join( map( str, GList ) ),
        ';'.join( map( str, BList ) )
    ])
    print( outString )
    f = open( 'csv_file.csv', 'wb' )
    f.write( outString )
    f.close()

def loadCsv():
    f = open( 'csv_file.csv', 'rb' )
    out = f.read()
    f.close()
    out = out.split('\r\n');
    out = [ x.split(';') for x in out ]
    return out

writeCsv( l1, l2, l3 )
print('--')
print( loadCsv() )
可能需要将文件保存为“RGB,RGB,…”而不是“RRR…,GGG…,BBB…”:

def writeCsvRGB( RList, GList, BList ):
    temp = zip( RList, GList, BList )
    temp = [ ';'.join( map( str, x ) ) for x in temp ]
    temp  = '\r\n'.join( temp )
    print( temp )

    f = open( 'csv_file.csv', 'wb' )
    f.write( temp )
    f.close()

使用
numpy.savetxt
?@ImportanceOfBeingErnest我不清楚如何使用它。你能举个例子吗?
numpy.savetxt(“filename.txt”,直方图)