指定宽或高按比例缩放图片
python
import cv2
python
def resize_by_ratio(image, width=None, height=None, inter=cv2.INTER_AREA):
img_new_size = None
(h, w) = image.shape[:2] # 获得高度和宽度
if width is None and height is None: # 如果输入的宽度和高度都为空
return image # 直接返回原图
if width is None:
h_ratio = height / float(h) # 输入高度 / 原始高度 得到比率
img_new_size = (int(w * h_ratio), height) # 将宽度缩放同样的比例
else:
w_ratio = width / float(w)
img_new_size = (width, int(h * w_ratio))
resized = cv2.resize(image, img_new_size, interpolation=inter)
return resized
python
# 绘图显示函数
def cv_show(img):
cv2.imshow('name',img)
cv2.waitKey(0) # 按任意键 终止窗口
cv2.destroyAllWindows()
python
img = cv2.imread('img1.jpeg')
img = resize_by_ratio(img,width=300)
# cv_show(img)
cv2.imwrite('img2_new.jpg',img)
拼接图片
python
img1 = cv2.imread('start.png')
img2 = cv2.imread('record.png')
img3 = cv2.imread('end.png')
img = np.hstack((img1,img2,img3))
cv_show(img)