【opencv】指定宽或高按比例缩放图片 & 拼接图片

指定宽或高按比例缩放图片

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)
相关推荐
明月_清风1 小时前
从“能用”到“专业”:构建生产级装饰器与三层逻辑拆解
后端·python
曲幽11 小时前
数据库实战:FastAPI + SQLAlchemy 2.0 + Alembic 从零搭建,踩坑实录
python·fastapi·web·sqlalchemy·db·asyncio·alembic
用户83562907805115 小时前
Python 实现 PowerPoint 形状动画设置
后端·python
ponponon17 小时前
时代的眼泪,nameko 和 eventlet 停止维护后的项目自救,升级和替代之路
python
Flittly17 小时前
【从零手写 ClaudeCode:learn-claude-code 项目实战笔记】(5)Skills (技能加载)
python·agent
敏编程17 小时前
一天一个Python库:pyarrow - 大规模数据处理的利器
python
Flittly19 小时前
【从零手写 ClaudeCode:learn-claude-code 项目实战笔记】(4)Subagents (子智能体)
python·agent
CoovallyAIHub19 小时前
181小时视频丢给GPT-5,准确率只有15%——南大联合NVIDIA等五校发布多模态终身理解数据集
深度学习·算法·计算机视觉
CoovallyAIHub19 小时前
CVPR 2026 | GS-CLIP:3D几何先验+双流视觉融合,零样本工业缺陷检测新SOTA,四大3D工业数据集全面领先!
深度学习·算法·计算机视觉
明月_清风1 天前
Python 装饰器前传:如果不懂“闭包”,你只是在复刻代码
后端·python