python读取csv数据画直方图_如何在Python中保存CSV文件中的直方图数据?

本文介绍如何使用Python从PIL库和matplotlib库读取图像的RGB直方图,并将直方图数据保存到CSV文件。通过示例代码展示了如何获取图像的红色、绿色和蓝色通道的直方图,并展示直方图。
摘要由CSDN通过智能技术生成

从下面的python代码中,我可以绘制颜色image的三个颜色分量的直方图。但我想把这个直方图数据保存到一个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()

这是红色通道histogram的输出

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值