python拖拽显示直方图

https://blog.csdn.net/lanlanmingyue/article/details/102745376
python3.8

安装依赖

pip install -i https://pypi.tuna.tsinghua.edu.cn/simple pillow
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple matplotlib

zft.py

# -*- coding: utf-8 -*-

import sys

from PIL import Image
import numpy as np
import matplotlib.pyplot as plt

src = Image.open(sys.argv[1])
r,g,b = src.split() #分离R\G\B

plt.figure("flowerHist")
#要对图像求直方图,就需要先把图像矩阵进行flatten操作,使之变为一维数组,然后再进行统计
#分别提取R\G\B统计值,叠加绘图
ar = np.array(r).flatten() 
plt.hist(ar,bins = 256, density = 1, facecolor = 'red', edgecolor = 'red',stacked=True)

ag = np.array(g).flatten()
plt.hist(ag,bins = 256, density = 1, facecolor = 'green', edgecolor = 'green',stacked=True)

ab = np.array(b).flatten()
plt.hist(ab,bins = 256, density = 1, facecolor = 'blue', edgecolor = 'blue', stacked=True)

plt.show()


Guess you like

Origin blog.csdn.net/A13155283231/article/details/121871872