pip3 install opencv-python
import cv2
普通读取图像
# 载入图像 im即所谓的CVMat
img = cv2.imread('0.JPG')
读取为灰度图
# 读取单通道
gray_img=cv2.imread('0.JPG',cv2.IMREAD_GRAYSCALE)
img = cv2.imread('0.JPG')
# 保存到本地
cv2.imwrite('0.PNG', img)
img = cv2.imread('0.JPG')
h, w = img.shape[:2]
print(h, w)
img = cv2.imread('0.JPG')
h=img.shape[0]
w=img.shape[1]
print(str(w)+":"+str(h))
img = cv2.imread('0.JPG')
print(str(img.size)+"Bit")
# 转换为灰度图
img = cv2.imread('0.JPG')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
img = cv2.imread('0.JPG')
# 指定jpg质量,范围从1~100,默认95,值越高画质越好,文件越大
cv2.imwrite('GRAY.jpg',img,(cv2.IMWRITE_JPEG_QUALITY,20))
img = cv2.imread('0.JPG')
# 缩小为200x200的正方形
img_200x200=cv2.resize(img,(200,200))
img = cv2.imread('0.JPG')
# 不直接指定缩放后的大小,通过fx和fy指定缩放比例,0.5表示长宽各一半
# 插值方法默认为cv2.INTER_LINEAR,这里指定为最近邻插值
img_half=cv2.resize(img,(0,0),fx=0.5,fy=0.5,interpolation=cv2.INTER_NEAREST)
img = cv2.imread('0.JPG')
# 上下各贴50像素的黑边
img_add_black=cv2.copyMakeBorder(img,50,50,0,0,cv2.BORDER_CONSTANT,value=(0,0,0))
img = cv2.imread('0.JPG')
# 裁剪
pathch_img=img[20:150,-180:-50]
img = cv2.imread('0.JPG')
# 通过cv2.cvtcolor把图像从RGB转到HSV
img_hsv=cv2.cvtColor(img,cv2.COLOR_BGR2HSV)
img = cv2.imread('0.JPG')
font = cv2.FONT_HERSHEY_SIMPLEX
cv2.putText(img,"Hello",(20,120),font,3,(0,0,255),1)
img = cv2.imread('0.JPG')
cv2.rectangle(img,(131,83),(131+256,83+256),(0,0,255),2)