图片操作:载入,保存,展示
import numpy as np
import cv2
print(cv2.__version__)
img = cv2.imread('./picture.jpg')
print(img)
print(img.shape)
cv2.imshow('img_show',img)
key = cv2.waitKey(0)&0xff
print(key)
img = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
print(img.shape)
cv2.imwrite('picture1.jpg',img)
cv2.destroyAllWindows()
- picture.jpg
- picture1.jpg
OpenCV绘图
- 准备工作:下载一张背景图片background.jpg
import numpy as np
import cv2
img = cv2.imread('./background.jpg')
print(img)
print(img.shape)
img = cv2.line(img,(0,0),(1280,0),(255,0,155),15)
img = cv2.line(img,(0,0),(0,800),(255,0,155),15)
img = cv2.line(img,(1280,0),(1280,800),(255,0,155),15)
img = cv2.line(img,(0,800),(1280,800),(255,0,155),15)
img = cv2.line(img,(100,100),(250,250),(255,255,255),3)
img = cv2.line(img,(250,100),(100,250),(255,255,255),3)
img = cv2.rectangle(img,(100,100),(250,250),(0,255,255),3)
img = cv2.circle(img,(175,175),110,(255,255,0),3)
cv2.imshow('img_show',img)
key = cv2.waitKey(0)&0xff
print(key)
cv2.destroyAllWindows()
- 绘图后的结果:
OpenCV文字处理
- 准备工作:下载一张背景图片background.jpg
import numpy as np
import cv2
img = cv2.imread('./background.jpg')
print(img)
print(img.shape)
'''
#直线
img = cv2.line(img,(0,0),(1280,0),(255,0,155),15)
img = cv2.line(img,(0,0),(0,800),(255,0,155),15)
img = cv2.line(img,(1280,0),(1280,800),(255,0,155),15)
img = cv2.line(img,(0,800),(1280,800),(255,0,155),15)
img = cv2.line(img,(100,100),(250,250),(255,255,255),3)
img = cv2.line(img,(250,100),(100,250),(255,255,255),3)
#矩形
img = cv2.rectangle(img,(100,100),(250,250),(0,255,255),3)
#圆形
img = cv2.circle(img,(175,175),110,(255,255,0),3)
'''
font = cv2.FONT_HERSHEY_SIMPLEX
cv2.putText(img,'Never frown',(750,150),font,1.5,(255,255,255),5,cv2.LINE_AA)
cv2.putText(img,'even when you are sad ',(625,250),font,1.5,(255,255,255),5,cv2.LINE_AA)
cv2.putText(img,'because you never know ',(500,350),font,1.5,(255,255,255),5,cv2.LINE_AA)
cv2.putText(img,'who is falling in love with your smile ',(390,450),font,1.5,(255,255,255),5,cv2.LINE_AA)
cv2.imshow('img_show',img)
key = cv2.waitKey(0)&0xff
print(key)
cv2.destroyAllWindows()
文字处理结果
原始图片background.jpg
- 文字+绘图后的结果: