from PIL import Image
import numpy as np
import matplotlib.pyplot as plt
def threshold(imageArray):
balanceAr = []
newAr = imageArray
for eachRow in imageArray:
for eachPix in eachRow:
avgNum = reduce(lambda x, y: x + y, eachPix[:3]) / len(eachPix[:3])
balanceAr.append(avgNum)
balance = reduce(lambda x, y: x + y, balanceAr) / len(balanceAr)
for eachRow in newAr:
for eachPix in eachRow:
if reduce(lambda x, y: x + y, eachPix[:3]) / len(eachPix[:3]) > balance:
eachPix[0] = 255
eachPix[1] = 255
eachPix[2] = 255
eachPix[3] = 255
else:
eachPix[0] = 0
eachPix[1] = 0
eachPix[2] = 0
eachPix[3] = 255
return newAr
i = Image.open('images/numbers/0.1.png')
iar = np.asarray(i)
3iar = threshold(iar)
i2 = Image.open('images/numbers/y0.4.png')
iar2 = np.asarray(i2)
#iar2 = threshold(iar2)
i3 = Image.open('images/numbers/y0.5.png')
iar3 = np.asarray(i3)
#iar3 = threshold(iar3)
i4 = Image.open('images/sentdex.png')
iar4 = np.asarray(i4)
#iar4 = threshold(iar4)
threshold(iar3)
fig = plt.figure()
ax1 = plt.subplot2grid((8,6), (0,0), rowspan = 4, colspan = 3)
ax2 = plt.subplot2grid((8,6), (4,0), rowspan = 4, colspan = 3)
ax3 = plt.subplot2grid((8,6), (0,3), rowspan = 4, colspan = 3)
ax4 = plt.subplot2grid((8,6), (4,3), rowspan = 4, colspan = 3)
ax1.imshow(iar)
ax2.imshow(iar2)
ax3.imshow(iar3)
ax4.imshow(iar4)
plt.show()
我得到的错误:
Warning (from warnings module):
File "C:\WinPython-32bit-2.7.9.5\python-2.7.9\Lib\idlelib\MuditPracticals\Image_Recognition\imagerec.py", line 11
avgNum = reduce(lambda x, y: x + y, eachPix[:3]) / len(eachPix[:3])
RuntimeWarning: overflow encountered in ubyte_scalars
Warning (from warnings module):
File "C:\WinPython-32bit-2.7.9.5\python-2.7.9\Lib\idlelib\MuditPracticals\Image_Recognition\imagerec.py", line 16
if reduce(lambda x, y: x + y, eachPix[:3]) / len(eachPix[:3]) > balance:
RuntimeWarning: overflow encountered in ubyte_scalars
Traceback (most recent call last):
File "C:\WinPython-32bit-2.7.9.5\python-2.7.9\Lib\idlelib\MuditPracticals\Image_Recognition\imagerec.py", line 47, in <module>
threshold(iar3)
File "C:\WinPython-32bit-2.7.9.5\python-2.7.9\Lib\idlelib\MuditPracticals\Image_Recognition\imagerec.py", line 17, in threshold
eachPix[0] = 255
ValueError: assignment destination is read-only
你不应该担心这些,他们基本上是告诉你的是,对于uint_8
(无符号整数)type
defined by numpy
用于图像文件的范围和普遍,简直超过其可接受的范围。
从所提供的链接,uint_8
类型具有一定范围的:
无符号整数(0到255)
numpy
刚刚发出警告,告知您的溢出。幸运的是,它会自动调整结果到的可接受范围内的值。
例如:
from PIL import Image
import numpy as np
img = Image.open("/path/to/image.png")
img_array = np.asarray(img) # array values are of type uint_8 (!)
print img_array[0][0] # prints [ 12, 21, 56, 255]
uint8_1 = img_array[0][0][3] # = 255
uint8_2 = img_array[0][0][2] # = 56
uint8_3 = uint8_1 + uint8_2
# When executed raises a RuntimeWarning of overflow ubyte_scalars
# But! The result 'rolls over' to the acceptable range. So,
print uint8_3 # prints 55
您ValueError: assignment destination is read-only
阵列numpy
赋值时你的错误newAr
实际上提高。它的信息,它告诉你的是,数组是read only
;内容是只读的:你可以访问它们,但不能修改它们。
所以像这样的动作:
# using img_array from previous snippet.
img_array[0][0][0] = 200
将引发ValueError
。
幸运的是,这是很容易通过设置该阵列的标志参数绕过:
# using img_array from the previous snippet
# make it writeable
img_array.setflags(write=True)
# Values of img_array[0][0] are, as before: [ 12, 21, 56, 255]
# changing the values for your array is possible now!
img_array[0][0][0] = 200
print img_array[0][0] # prints [ 200, 21, 56, 255]
最后说明一点:你总是可以suppress/ignore these warnings,尽管这通常不是个好主意。 (一对夫妇在控制台的警告很烦人,但他们给你的东西更清晰的视图)
要做到这一点,只需添加导入numpy的后如下:
import numpy as np
np.seterr(over='ignore')
I1 = Image.open( '图像/数字/ 0.1.png')IAR1 = np.array(I1)
而不是asarray梅索德使用阵列
尝试更换Y:X + Y,其中Y:INT(X)+ INT(y)的