Python PIL
是Python的一个图像处理模块,包含了一系列图像处理操作,其中 Image.histogram()
是其中一个常用函数,用于生成一幅图像的直方图,下面详细介绍该函数的用法:
Image.histogram()
函数的简介
Image.histogram()
函数用于将一幅图像转为一维直方图,直方图的每一个数据表示一个像素值的数量。比如一幅灰度图像,数据会是0到255之间的256个数,每个数表示像素值等于这个数的像素的数量。对于一幅彩色图像,数据会是0到765(3*255)之间256 * 3个数,每一个像素值表示三通道上,对应比例的像素值数量。
Image.histogram()
函数的使用方法
首先,我们需要引入PIL库,然后使用 Image.open()
函数载入需要处理的图片:
from PIL import Image
img = Image.open('test.jpg')
传入图像,调用 Image.histogram()
函数即可生成直方图数据:
histogram_data = img.histogram()
Image.histogram()
函数的参数
默认情况下,Image.histogram()
会针对一幅图的所有通道生成直方图数据,如果需要只针对某一通道,可以指定 Image.histogram()
的参数 channel
:
histogram_data = img.histogram(channel=0)
上面代码会生成一幅图像的红色通道的直方图数据,0表示红色通道,1表示绿色通道,2表示蓝色通道。
另外,如果我们想要特别处理一些像素值范围内的数据,可以指定 Image.histogram()
的 histogram_range
参数。该参数是一个元组,元组中的两个值 low
和 high
表示直方图的数据下限和上限,只会针对这个范围内的像素值生成直方图:
# 仅处理像素值在0到100之间的数据
histogram_data = img.histogram(histogram_range=(0, 100))
示范1:生成灰度图像的直方图
from PIL import Image
# 载入灰度图像
img = Image.open('test_gray.jpg')
# 生成直方图数据
histogram_data = img.histogram()
print(histogram_data)
输出结果:
[119900, 345, 103, 77, 43, 35, 45, 36, 24, 29, 28,
19, 20, 16, 19, 23, 23, 38, 57, 69, 78, 110,
113, 119, 150, 179, 196, 236, 287, 355, 370, 432, 425,
452, 528, 535, 566, 668, 702, 793, 803, 998, 837, 1076,
1079, 1238, 1401, 1486, 1536, 1426, 1466, 1438, 1386, 1366, 1276,
1180, 1097, 1051, 966, 874, 801, 738, 646, 569, 526, 470,
405, 333, 296, 269, 235, 184, 161, 178, 124, 117, 123,
100, 90, 86, 77, 86, 86, 105, 97, 86, 106, 109,
97, 90, 97, 68, 97, 105, 97, 101, 97, 90, 77,
77, 68, 71, 74, 80, 83, 77, 74, 74, 77, 62,
83, 65, 65, 80, 62, 77, 74, 68, 68, 50, 71,
56, 47, 53, 44, 47, 50, 44, 53, 38, 50, 50,
41, 41, 41, 47, 44, 50, 32, 35, 35, 35, 38,
35, 29, 32, 26, 41, 35, 32, 35, 29, 26, 29,
32, 29, 23, 23, 23, 26, 23, 23, 20, 20, 26,
20, 23, 17, 20, 17, 17, 20, 17, 17, 17, 20,
19, 19, 20, 19, 14, 20, 19, 19, 17, 14, 14,
14, 14, 11, 14, 14, 14, 11, 11, 11, 11, 14,
11, 8, 11, 11, 11, 11, 11, 11, 8, 8, 11,
11, 11, 11, 11, 11, 8, 11, 11, 11, 8, 8,
8, 8, 11, 8, 8, 5, 5, 5, 5, 5, 5,
5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5]
示范2:生成分通道的直方图
from PIL import Image
# 载入图像
img = Image.open('test.jpg')
# 生成红色通道的直方图
histogram_data_red = img.histogram(channel=0)
# 生成绿色通道的直方图
histogram_data_green = img.histogram(channel=1)
# 生成蓝色通道的直方图
histogram_data_blue = img.histogram(channel=2)
print(histogram_data_red)
print(histogram_data_green)
print(histogram_data_blue)
输出结果:
[48, 96, 32, 70, 98, 100, 106, 142, 172, 166, 188, 210, 198, 200, 192, 204, 216, 184, 192, 196, 204, 204, 202, 204, 204, 204, 204, 204, 204, 204, 204, 204, 204, 204, 204, 204, 204, 204, 204, 204, 204, 204, 204, 204, 204, 204, 204, 204, 204, 204, 204, 204, 204, 204, 204, 204, 204, 204, 204, 204, 204, 204, 204, 204, 204, 204, 204, 204, 204, 204, 204, 136, 28, 177, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 81, 203, 172, 254, 223, 233, 232, 255, 243, 253, 261, 269, 248, 247, 262, 274, 308, 273, 304, 296, 275, 275, 289, 292, 295, 277, 295, 309, 305, 376, 354, 365, 380, 420, 457, 450, 437, 436, 436, 451, 482, 501, 430, 489, 487, 527, 519, 591, 564, 583, 545, 558, 500, 557, 529, 518, 483, 461, 448, 366, 349, 373, 313, 307, 282, 320, 318, 307, 265, 300, 267, 236, 216, 204, 206, 196, 186, 163, 166, 157, 147, 137, 132, 129, 127, 118, 102, 105, 102, 79, 77, 71, 68, 63, 56, 52, 51, 46, 43, 39, 35, 31, 24, 26, 24, 16, 20, 17, 17, 14, 14, 11, 11, 8, 8, 5, 5, 5, 5, 5, 5, 0, 5, 5, 5, 0, 5, 0, 5, 0, 5, 5, 5, 0, 5, 0, 5, 0, 5, 0, 0, 0, 0]
[455, 298, 222, 190, 161, 107, 161, 145, 131, 140, 128, 140, 124, 124, 167, 185, 186, 194, 196, 206, 217, 235, 238, 249, 287, 303, 320, 330, 352, 385, 415, 448, 481, 533, 539, 586, 590, 622, 639, 676, 674, 742, 719, 769, 738, 635, 690, 634, 618, 635, 651, 611, 656, 592, 565, 631, 613, 593, 556, 566, 509, 554, 517, 509, 466, 447, 492, 462, 420, 393, 381, 393, 381, 353, 337, 354, 330, 380, 291, 335, 300, 313, 274, 278, 279, 269, 230, 242, 234, 236, 211, 182, 194, 173, 179, 167, 166, 143, 154, 130, 132, 123, 147, 106, 122, 93, 90, 102, 102, 93, 93, 76, 92, 81, 68, 77, 47, 61, 61, 54, 50, 54, 42, 51, 47, 32, 42, 29, 29, 23, 20, 32, 17, 23, 29, 20, 32, 23, 20, 26, 29, 17, 32, 26, 23, 20, 17, 26, 23, 14, 14, 17, 23, 14, 17, 17, 14, 14, 5, 8, 8, 14, 11, 11, 11, 8, 5, 11, 5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[287, 225, 156, 119, 85, 66, 61, 47, 40, 50, 47, 43, 50, 47, 57, 71, 79, 98, 126, 154, 192, 204, 231, 226, 259, 271, 320, 358, 405, 454, 501, 561, 536, 610, 627, 690, 721, 782, 787, 848, 926, 946, 952, 589, 829, 781, 816, 859, 861, 926, 986, 978, 948, 976, 783, 876, 812, 751, 600, 734, 687, 587, 342, 276, 322, 316, 249, 239, 242, 179, 156, 119, 124, 147, 140, 107, 100, 107, 101, 98, 101, 83, 101, 86, 86, 86, 86, 86, 86, 97, 86, 97, 86, 97, 86, 97, 86, 97, 90, 97, 97, 97, 97, 97, 97, 97, 90, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97]
可以从结果数据中看到,输出的数据是每一个像素值在相应通道上的像素数数量。
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:详解Python PIL Image.histogram() - Python技术站