在这段代码中,我将一张图片切成多个不同的部分,在每一部分中,我都想在此处计算绿色像素的数量。
因此,我创建了一个字典,其中图片的每一行和每一列代表特定裁剪的图片。我试图保存所有这些图片,但是那是我遇到的麻烦,好像看不到它是一张图片。
此后,我想再次打开所有图片,并在此处计算绿色像素的数量。
def splitting(...)
...
dic[(i, j)] = im.crop((left, top, right, bottom)
return dic
all_images = spliting(img, 3, 5)
这是我遇到的麻烦
#saving all cropped images
for key in all_images:
all_images[key].save("%d_%d" %(key[0], key[1]))
#counting the pixels in the cropped images
BLACK_MIN = np.array([0, 20, 20], np.uint8)
BLACK_MAX = np.array([120, 255, 255], np.uint8)
for img1 in ("%d_%d" %(key[0], key[1])):
img2 = cv2.imread(img1, 1)
dst = cv2.inRange(img2, BLACK_MIN, BLACK_MAX)
no_black = cv2.countNonZero(dst)
print('The number of black pixels is: ' + str(no_black))
我收到此错误
Traceback (most recent call last):
File "C:\Users\arnol\PycharmProjects\untitled\venv\lib\site-packages\PIL\Image.py", line 2064, in save
format = EXTENSION[ext]
KeyError: ''
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:/Users/arnol/PycharmProjects/untitled/t4.py", line 29, in <module>
all_images[key].save("%d_%d" %(key[0], key[1]))
File "C:\Users\arnol\PycharmProjects\untitled\venv\lib\site-packages\PIL\Image.py", line 2066, in save
raise ValueError("unknown file extension: {}".format(ext))
ValueError: unknown file extension: