OpenCV图像01输出

今天在群里水吹,说要把QQ头像01输出。就在网上抄了个demo改了一下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
"""
这段代码的目的是让图片转换为0和1输出
源代码取自https://blog.csdn.net/ljx1400052550/article/details/114735364
后修改
"""


import cv2 #opencv库


def jubuThreshold(input_img_file):
image = input_img_file
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
binary = cv2.adaptiveThreshold(gray, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, 25, 10)
cv2.destroyAllWindows()
for y in binary :
for x in y :
if 0 == x :
print("0",end='')
else:
print("1",end='')
print("")

jubuThreshold(cv2.imread('qi2.jpg'))

原图

image-20220820201523870

效果

image-20220820201545904