批量处理不同大小的图,填充成固定大小的图

news2025/10/21 4:11:14

1.输入一张图片转化成一张新图(只适用单通道图片)

def convert_image_size1(img,size):#size=[width,height]
    img = np.array(img)
    height, width = img.shape[0],img.shape[1]
    ratio = width / height
    mask_image = []
    channel = 1
    if len(img.shape) == 3:
        channel = 3
    if width > height:
        w = size[0]
        h = int(w / ratio)
        img1 = cv2.resize(img, (w, h))
        h1, w1 = img1.shape[0],img1.shape[1]

        if h1 % 2 == 0:
            c1 = int((size[1] - h1) / 2)
            c2 = int((size[1]- h1) / 2)

            mask1 = np.zeros([c1, w,channel], dtype=np.uint8)
            mask2 = np.zeros([c2, w,channel], dtype=np.uint8)
            # img2=cv2.vconcat([mask1,img1,mask2])
            img2 = np.concatenate([mask1, img1, mask2], axis=0)
            mask_image.append(img2)
            # print("*****", img2.shape)

        elif h1 % 2 == 1:
            c1 = int(((size[1] - h1) / 2) + 1)
            c2 = int((size[1] - h1) / 2)
            mask1 = np.zeros([c1, w,channel], dtype=np.uint8)
            mask2 = np.zeros([c2, w,channel], dtype=np.uint8)
            # img2 = cv2.vconcat([mask1, img1, mask2])
            img2 = np.concatenate([mask1, img1, mask2], axis=0)
            mask_image.append(img2)

    elif width < height:
        h = size[1]
        w = int(h * ratio)
        img1 = cv2.resize(img, (w, h))
        h1, w1 = img1.shape
        if w1 % 2 == 0:
            c1 = int((size[0] - w1) / 2)
            c2 = int((size[0] - w1) / 2)
            mask1 = np.zeros([h, c1,channel], dtype=np.uint8)
            mask2 = np.zeros([h, c2,channel], dtype=np.uint8)
            # img2 = cv2.hconcat([mask1, img1, mask2])
            img2 = np.concatenate([mask1, img1, mask2], axis=1)
            mask_image.append(img2)
            # print(">>>>>>>>>", img2.shape)
            # cv2.imwrite("", img2)

        elif w1 % 2 == 1:
            c1 = int((size[0] - w1) / 2) + 1
            c2 = int((size[0] - w1) / 2)
            print("c1c2:", c1, c2)
            mask1 = np.zeros([h, c1,channel], dtype=np.uint8)
            mask2 = np.zeros([h, c2,channel], dtype=np.uint8)
            # img2 = cv2.hconcat([mask1, img1, mask2])
            img2 = np.concatenate([mask1, img1, mask2], axis=1)
            print(">>>>>>>>>", img2.shape)
            mask_image.append(img2)

    else:
        img1 = cv2.resize(img, (size[0], size[1],channel))
        mask_image.append(img1)

    return mask_image[0]

2.输入一张图片转化成一张新图(适用单通道和3通道图片)

def convert_image_size(img,size):#size=[width,height]
    img=np.array(img)
    new_shape = list(img.shape)
    new_shape[0] = size[0]
    new_shape[1] = size[1]
    mask_image = np.zeros(new_shape, dtype=np.uint8)

    height, width= img.shape[0],img.shape[1]
    ratio = width / height
    if width > height:
        img2 = cv2.resize(img, (size[0],  int(size[0] / ratio)))
        start=int((size[1]-img2.shape[0])/2)
        mask_image[start:start+img2.shape[0], 0:size[0]] = img2
    elif width < height:
        img2 = cv2.resize(img, (int(size[1] * ratio), size[1]))
        start = int((size[0] - img2.shape[1]) / 2)
        mask_image[0:size[1],start:start+img2.shape[1]] = img2
    else:
        mask_image = cv2.resize(img, (size[0], size[1]))
    return mask_image

3.案例

import os
from PIL import Image
import numpy as np
import cv2


def convert_image_size1(img,size):#size=[width,height]
    img = np.array(img)
    height, width = img.shape
    ratio = width / height
    mask_image = []
    if width > height:
        w = size[0]
        h = int(w / ratio)
        img1 = cv2.resize(img, (w, h))
        h1, w1 = img1.shape
        c1 = int(((size[1] - h1) / 2)+0.5)
        c2 = int((size[1]- h1) / 2)
        mask1 = np.zeros([c1, w], dtype=np.uint8)
        mask2 = np.zeros([c2, w], dtype=np.uint8)
        # img2=cv2.vconcat([mask1,img1,mask2])
        img2 = np.concatenate([mask1, img1, mask2], axis=0)
        mask_image.append(img2)
        print("*****", img2.shape)

    elif width < height:
        h = size[1]
        w = int(h * ratio)
        img1 = cv2.resize(img, (w, h))
        h1, w1 = img1.shape
        c1 = int(((size[0] - w1) / 2)+0.5)
        c2 = int((size[0] - w1) / 2)
        mask1 = np.zeros([h, c1], dtype=np.uint8)
        mask2 = np.zeros([h, c2], dtype=np.uint8)
        # img2 = cv2.hconcat([mask1, img1, mask2])
        img2 = np.concatenate([mask1, img1, mask2], axis=1)
        mask_image.append(img2)
        print(">>>>>>>>>", img2.shape)
        # cv2.imwrite("", img2)

    else:
        img1 = cv2.resize(img, (size[0], size[1]))
        mask_image.append(img1)

    return mask_image[0]


def convert_image_size(img,size):#size=[width,height]800,800
    img=np.array(img)

    new_shape = list(img.shape)
    new_shape[0] = size[0]
    new_shape[1] = size[1]
    mask_image = np.zeros(new_shape, dtype=np.uint8)

    height, width= img.shape[0],img.shape[1]
    ratio = width / height
    if width > height:
        img2 = cv2.resize(img, (size[0],  int(size[0] / ratio)))
        start=int((size[1]-img2.shape[0])/2)
        mask_image[start:start+img2.shape[0], 0:size[0]] = img2
    elif width < height:
        img2 = cv2.resize(img, (int(size[1] * ratio), size[1]))
        start = int((size[0] - img2.shape[1]) / 2)
        mask_image[0:size[1],start:start+img2.shape[1]] = img2
    else:
        mask_image = cv2.resize(img, (size[0], size[1]))
    return mask_image

if __name__ == '__main__':
    #图片转化固定大小
    dir="/media/tianhailong/新加卷/coco_seg/image1"
    out_dir="/media/tianhailong/新加卷/coco_seg/out"
    for path in os.listdir(dir):
        path1=os.path.join(dir,path)
        print(path1)
        img = Image.open(path1)
        mask_image=convert_image_size(img,(640,640))
        cv2.imwrite(os.path.join(out_dir,path),mask_image)

# 将待贴图片贴到原始图片上




本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/805734.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

二叉树的链式结构 - 遍历 - C语言递归实现

前序、中序以及后序遍历 二叉树遍历 (Traversal) 是按照某种特定的规则&#xff0c;依次对二叉 树中的节点进行相应的操作&#xff0c;并且每个节点只操作一次 。 按照规则&#xff0c;二叉树的遍历有&#xff1a; 前序/中序/后序 的递归结构遍历 &#xff1a; 1. 前序遍历(P…

2023天津Java培训学校分享!Java培训班

近年来&#xff0c;各类培训机构如雨后春笋般涌现&#xff0c;其中&#xff0c;Java培训机构可谓是风头正盛&#xff0c;许多想踏入这行的小伙伴选择这个方式来学习Java技能&#xff0c;今天我们一起来讨论一下&#xff1a;学Java有门槛吗&#xff0c;Java培训的好处&#xff0…

问题解决:VS Code环境调试多文件C++程序

在VS code环境下默认可以调试单文件的C程序&#xff0c;如果是多文件的程序&#xff0c;则会出现编译不通过的问题&#xff0c;无法进行调试 解决方法 在VS Code的工程目录下&#xff0c;有一个tasks.json文件 修改tasks.json文件 其中&#xff0c;"args"子项里面…

android app控制ros机器人三(android登录界面)

接下来是二次开发的具体环节了&#xff0c;由于存在用户需求&#xff0c;用到ros-mobile不多&#xff0c;更偏向于android开发。 用ppt画了简单的展示界面&#xff0c;与用后交流界面的功能布局。先开发一代简易版本的app&#xff0c;后续可以丰富完善。ctrlcv上线。 登录界面…

图数据库Neo4j学习三——cypher语法总结

1MATCH 1.1作用 MATCH是Cypher查询语言中用于从图数据库中检索数据的关键字。它的作用是在图中查找满足指定条件的节点和边&#xff0c;并返回这些节点和边的属性信息。 在MATCH语句中&#xff0c;通过节点标签和边类型来限定查找范围&#xff0c;然后通过WHERE语句来筛选符合…

vue+leaflet笔记之地图量测

vueleaflet笔记之地图量测 文章目录 vueleaflet笔记之地图量测开发环境代码简介插件简介与安装使用简介图形量测动态量测 详细源码(Vue3) 本文介绍了Web端使用Leaflet开发库进行距离量测的一种方法 (底图来源:天地图)&#xff0c;结合leaflet-measure-path插件能够快速的实现地…

人工智能术语翻译(四)

文章目录 摘要MNOP 摘要 人工智能术语翻译第四部分&#xff0c;包括I、J、K、L开头的词汇&#xff01; M 英文术语中文翻译常用缩写备注Machine Learning Model机器学习模型Machine Learning机器学习ML机器学习Machine Translation机器翻译MTMacro Average宏平均Macro-F1宏…

高忆管理:msci成分股什么意思?

MSCI&#xff08;Morgan Stanley Capital International&#xff09;是全球领先的金融指数提供商之一&#xff0c;其指数被广泛应用于全球资本商场的出资和危险办理。而MSCI成分股&#xff0c;是指MSCI指数中所包括的股票。那么&#xff0c;MSCI成分股具体意义是什么呢&#xf…

CTFshow-pwn入门-pwn67(nop sled空操作雪橇)

前言 本人由于今年考研可能更新的特别慢&#xff0c;不能把ctfshow的pwn入门题目的wp一一都写出来了&#xff0c;时间比较紧啊&#xff0c;只能做高数做累的时候做做pwn写写wp了&#xff0c;当然我之后只挑典型意义的题目写wp了&#xff0c;其余的题目就留到12月底考完之后再写…

Sugar BI : AI 问答,即问即答

AI 探索功能提供给所有用户自由探索和分析数据模型的能力。在 AI 探索页中&#xff0c;有授权的用户可以通过 AI 问答和字段拖拽两种方式对数据模型进行探索。 下面&#xff0c;我们将为大家详细指导如何使用 AI 探索 新建 AI 探索页 空间管理员可以在报表管理中新建「AI 探索…

短视频矩阵系统源码---开发技术源码能力

短视频矩阵系统开发涉及到多个领域的技术&#xff0c;包括视频编解码技术、大数据处理技术、音视频传输技术、电子商务及支付技术等。因此&#xff0c;短视频矩阵系统开发人员需要具备扎实的计算机基础知识、出色的编程能力、熟练掌握多种开发工具和框架&#xff0c;并掌握音视…

UE Web Remote Control call python script

UE Web Remote Control call python script UE 远程调用Python(UE Python API)脚本 Web Remote Control 在网页客户端远程操作虚幻引擎项目。 虚幻编辑器提供了一套强大的工具&#xff0c;几乎可以操纵项目内容的方方面面。但在某些情况下&#xff0c;要在大型内容编辑流程中…

使用SVM模型完成分类任务

SVM&#xff0c;即支持向量机&#xff08;Support Vector Machine&#xff09;&#xff0c;是一种常见的机器学习算法&#xff0c;用于分类和回归分析。SVM的基本思想是将数据集映射到高维空间中&#xff0c;在该空间中找到一个最优的超平面&#xff0c;将不同类别的数据点分开…

国企普通员工如何才能成为公务员,这三种途径可供参考

国企普通员工如何转变成公务员&#xff1f;作为国企普通员工&#xff0c;如果要成为国家公务员&#xff0c;其主要的路径有三个方面&#xff0c;一是符合国家公务员法规定的公务员招录条件要求的&#xff0c;可以报考国家公务员&#xff1b;二是在国有企业担任领导职务&#xf…

使用EM算法完成聚类任务

EM算法&#xff08;Expectation-Maximization Algorithm&#xff09;是一种基于迭代优化的聚类算法&#xff0c;用于在无监督的情况下将数据集分成几个不同的组或簇。EM算法是一种迭代算法&#xff0c;包含两个主要步骤&#xff1a;期望步骤&#xff08;E-step&#xff09;和最…

子网重叠测试

子网重叠的两个网络可以相互通 虽然子网掩码不同&#xff0c;但是 R1 可以 ping R2&#xff1a; <R1>ping 10.0.12.14PING 10.0.12.14: 56 data bytes, press CTRL_C to breakReply from 10.0.12.14: bytes56 Sequence1 ttl255 time50 msReply from 10.0.12.14: bytes5…

Verilog语法学习——LV4_移位运算与乘法

LV4_移位运算与乘法 题目来源于牛客网 [牛客网在线编程_Verilog篇_Verilog快速入门 (nowcoder.com)](https://www.nowcoder.com/exam/oj?page1&tabVerilog篇&topicId301) 题目 题目描述&#xff1a; 已知d为一个8位数&#xff0c;请在每个时钟周期分别输出该数乘1/…

利用小波分解信号,再重构

function [ output_args ] example4_5( input_args ) %EXAMPLE4_5 Summary of this function goes here % Detailed explanation goes here clc; clear; load leleccum; s leleccum(1:3920); % 进行3层小波分解&#xff0c;小波基函数为db2 [c,l] wavedec(s,3,db2); %进行…

剑指 Offer 37. 序列化二叉树 / LeetCode297. 二叉树的序列化与反序列化(二叉树遍历(深度优先搜索))

题目&#xff1a; 链接&#xff1a;剑指 Offer 37. 序列化二叉树&#xff1b;LeetCode 297. 二叉树的序列化与反序列化 难度&#xff1a;困难 序列化是将一个数据结构或者对象转换为连续的比特位的操作&#xff0c;进而可以将转换后的数据存储在一个文件或者内存中&#xff0…

【雕爷学编程】Arduino动手做(99)---8X32 LED点阵屏模块3

37款传感器与执行器的提法&#xff0c;在网络上广泛流传&#xff0c;其实Arduino能够兼容的传感器模块肯定是不止这37种的。鉴于本人手头积累了一些传感器和执行器模块&#xff0c;依照实践出真知&#xff08;一定要动手做&#xff09;的理念&#xff0c;以学习和交流为目的&am…