python - 使用 PIL、plt、numpy 绘制直方图时出错
问题描述
我试图在颜色范围之间绘制 hist 但它显示 jpeg 图像文件不可下标
from PIL import Image
img = Image.open(r"C:/Users/Atharva/Downloads/secimg.jpg")
img.thumbnail((64,64),Image.ANTIALIAS) #Image.ANTIALIAS
imgplot = plt.imshow(img)
plt.colorbar(orientation='horizontal')
输出是 [![绘制的图像是][1]][1] [1]:https://i.stack.imgur.com/H5yAT.png
之后我尝试绘制直方图并且存在错误我应该怎么做才能绘制直方图
himplot = img[:,:,0]
plt.hist(himplot.ravel(), bins=256, range=(0, 251), fc='k', ec='k')
错误是
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-121-08f1ac3d88ff> in <module>
1 # plt.hist(img.ravel(), bins = 256,range(50,225),fc='k',ec='k')
----> 2 himplot = img[:,:,0]
3 plt.hist(himplot.ravel(), bins=256, range=(0, 251), fc='k', ec='k')
TypeError: 'JpegImageFile' object is not subscriptable
解决方案
感谢您的帮助@ananda 我终于能够绘制直方图问题是我没有将数组转换为 NumPy 数组最终代码是
img = np.array(img)
himplot = img[:,:,0]
plt.hist(himplot.ravel(), bins=256, range=(0, 251), fc='k', ec='k')
输出是
(array([ 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., 1., 1., 0.,
2., 3., 3., 2., 5., 8., 11., 2., 6., 4., 10., 6., 0.,
3., 5., 4., 10., 11., 9., 4., 8., 7., 10., 12., 7., 14.,
11., 10., 7., 8., 14., 9., 17., 16., 18., 19., 11., 24., 17.,
25., 24., 38., 29., 38., 29., 27., 28., 18., 27., 23., 29., 33.,
50., 64., 57., 57., 55., 52., 52., 45., 44., 45., 47., 0., 50.,
42., 36., 40., 47., 37., 28., 22., 41., 31., 30., 38., 35., 24.,
16., 17., 14., 20., 23., 13., 16., 15., 12., 15., 11., 16., 16.,
19., 8., 9., 13., 13., 20., 11., 9., 10., 5., 13., 12., 7.,
4., 7., 6., 6., 5., 8., 8., 8., 2., 6., 0., 7., 4.,
12., 5., 10., 13., 8., 4., 8., 8., 3., 7., 9., 12., 14.,
7., 7., 6., 9., 11., 11., 6., 6., 11., 9., 13., 7., 12.,
8., 10., 14., 11., 13., 6., 8., 10., 9., 7., 13., 11., 9.,
10., 13., 10., 8., 5., 5., 13., 10., 10., 0., 7., 7., 8.,
13., 12., 11., 8., 9., 8., 11., 9., 8., 5., 5., 7., 4.,
5., 6., 5., 1., 2., 0., 2., 2., 1., 1., 4., 1., 1.,
0., 1., 1., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
0., 0., 0., 0., 0., 0., 0., 0., 0.]),
array([ 0. , 0.98046875, 1.9609375 , 2.94140625,
3.921875 , 4.90234375, 5.8828125 , 6.86328125,
7.84375 , 8.82421875, 9.8046875 , 10.78515625,
11.765625 , 12.74609375, 13.7265625 , 14.70703125,
15.6875 , 16.66796875, 17.6484375 , 18.62890625,
19.609375 , 20.58984375, 21.5703125 , 22.55078125,
23.53125 , 24.51171875, 25.4921875 , 26.47265625,
27.453125 , 28.43359375, 29.4140625 , 30.39453125,
31.375 , 32.35546875, 33.3359375 , 34.31640625,
35.296875 , 36.27734375, 37.2578125 , 38.23828125,
39.21875 , 40.19921875, 41.1796875 , 42.16015625,
43.140625 , 44.12109375, 45.1015625 , 46.08203125,
47.0625 , 48.04296875, 49.0234375 , 50.00390625,
50.984375 , 51.96484375, 52.9453125 , 53.92578125,
54.90625 , 55.88671875, 56.8671875 , 57.84765625,
58.828125 , 59.80859375, 60.7890625 , 61.76953125,
62.75 , 63.73046875, 64.7109375 , 65.69140625,
66.671875 , 67.65234375, 68.6328125 , 69.61328125,
70.59375 , 71.57421875, 72.5546875 , 73.53515625,
74.515625 , 75.49609375, 76.4765625 , 77.45703125,
78.4375 , 79.41796875, 80.3984375 , 81.37890625,
82.359375 , 83.33984375, 84.3203125 , 85.30078125,
86.28125 , 87.26171875, 88.2421875 , 89.22265625,
90.203125 , 91.18359375, 92.1640625 , 93.14453125,
94.125 , 95.10546875, 96.0859375 , 97.06640625,
98.046875 , 99.02734375, 100.0078125 , 100.98828125,
101.96875 , 102.94921875, 103.9296875 , 104.91015625,
105.890625 , 106.87109375, 107.8515625 , 108.83203125,
109.8125 , 110.79296875, 111.7734375 , 112.75390625,
113.734375 , 114.71484375, 115.6953125 , 116.67578125,
117.65625 , 118.63671875, 119.6171875 , 120.59765625,
121.578125 , 122.55859375, 123.5390625 , 124.51953125,
125.5 , 126.48046875, 127.4609375 , 128.44140625,
129.421875 , 130.40234375, 131.3828125 , 132.36328125,
133.34375 , 134.32421875, 135.3046875 , 136.28515625,
137.265625 , 138.24609375, 139.2265625 , 140.20703125,
141.1875 , 142.16796875, 143.1484375 , 144.12890625,
145.109375 , 146.08984375, 147.0703125 , 148.05078125,
149.03125 , 150.01171875, 150.9921875 , 151.97265625,
152.953125 , 153.93359375, 154.9140625 , 155.89453125,
156.875 , 157.85546875, 158.8359375 , 159.81640625,
160.796875 , 161.77734375, 162.7578125 , 163.73828125,
164.71875 , 165.69921875, 166.6796875 , 167.66015625,
168.640625 , 169.62109375, 170.6015625 , 171.58203125,
172.5625 , 173.54296875, 174.5234375 , 175.50390625,
176.484375 , 177.46484375, 178.4453125 , 179.42578125,
180.40625 , 181.38671875, 182.3671875 , 183.34765625,
184.328125 , 185.30859375, 186.2890625 , 187.26953125,
188.25 , 189.23046875, 190.2109375 , 191.19140625,
192.171875 , 193.15234375, 194.1328125 , 195.11328125,
196.09375 , 197.07421875, 198.0546875 , 199.03515625,
200.015625 , 200.99609375, 201.9765625 , 202.95703125,
203.9375 , 204.91796875, 205.8984375 , 206.87890625,
207.859375 , 208.83984375, 209.8203125 , 210.80078125,
211.78125 , 212.76171875, 213.7421875 , 214.72265625,
215.703125 , 216.68359375, 217.6640625 , 218.64453125,
219.625 , 220.60546875, 221.5859375 , 222.56640625,
223.546875 , 224.52734375, 225.5078125 , 226.48828125,
227.46875 , 228.44921875, 229.4296875 , 230.41015625,
231.390625 , 232.37109375, 233.3515625 , 234.33203125,
235.3125 , 236.29296875, 237.2734375 , 238.25390625,
239.234375 , 240.21484375, 241.1953125 , 242.17578125,
243.15625 , 244.13671875, 245.1171875 , 246.09765625,
247.078125 , 248.05859375, 249.0390625 , 250.01953125,
251. ]),
<BarContainer object of 256 artists>)
推荐阅读
- java - 一个简单的 Spring 没有 favicon.ico - Maven Web 应用程序
- ios - 每当我在 iOS 上键入内容时,ListViews 中的 Xamarin 表单编辑器都会失去焦点
- azure - Azure:撤销用户的访问令牌,使他们无法从 Azure 中的移动后端请求数据
- javascript - 为什么我无法捕捉到返回的响应错误?
- c++ - 错误:无法转换 'std::__cxx11::basic_string
::迭代器' - c++ - 如何建立运算符优先级?
- sql - 在 MS Access 中填充字母数字值的条件更新查询
- javascript - 如何使用 Javascript 获取结果
- python - 在python中使用列表对嵌套字典进行排序
- c++ - 以不同的方式平移同一 3D 空间中的两个对象?