等比例缩放图片并填充图片为正方形(python附代码)
2023-09-11 14:22:51 时间
import os import cv2 import shutil def suofang(im,target_height,target_width): height, width = im.shape[:2] # 取彩色图片的长、宽。 ratio_h = height / target_height ration_w = width / target_width ratio = max(ratio_h, ration_w) # 缩小图像 resize(...,size)--size(width,height) size = (int(width / ratio), int(height / ratio)) shrink = cv2.resize(im, size, interpolation=cv2.INTER_AREA) # 双线性插值 BLACK = [0, 0, 0] a = (target_width - int(width / ratio)) / 2 b = (target_height - int(height / ratio)) / 2 constant = cv2.copyMakeBorder(shrink, int(b), int(b), int(a), int(a), cv2.BORDER_CONSTANT, value=BLACK) constant = cv2.resize(constant, (target_width, target_height), interpolation=cv2.INTER_AREA) return constant shutil.rmtree('你的输出路径') os.mkdir('你的输出路径') input0 = '你的输出路径' output0 = '你的输入路径' datalist = os.listdir(input0) for i in datalist: id = i.split('.')[0] im_name = input0 + '/' + i outpath = output0 + '/' img = io.imread(im_name) img = cv2.cvtColor(img,cv2.COLOR_RGBA2RGB)#有个别图片是32位ARGB的,不去除透明通道的话填充边缘的会是白色的 img = suofang(img,300,300)# 自己改大小 io.imsave(outpath+id+'.png',img)
相关文章
- python 目录
- Python语法基础_03.字符串、列表、字典
- python 自动化数据提取之正则表达式
- python 通过代码设置ffmpeg环境
- Python代码 Base64 格式图片上传,Base64格式理解
- python读写csv或tsv文件的几种方法
- c#代码 天气接口 一分钟搞懂你的博客为什么没人看 看完python这段爬虫代码,java流泪了c#沉默了 图片二进制转换与存入数据库相关 C#7.0--引用返回值和引用局部变量 JS直接调用C#后台方法(ajax调用) Linq To Json SqlServer 递归查询
- 【华为OD机试真题 python】不含 101 的数【2022 Q4 | 100分】
- python代码类型注解(typing)
- 零基础学习python并成长为一名程序员,是否具有较大的难度
- Python-Numpy库的扩维(维度扩展)操作示例代码积累
- 详解Python的元组(tuple)的12种操作方法,并附示例代码
- Python和lua互相调用
- python 新手遇到的问题
- 最长连续递增子序列-Python
- 《Python数据可视化编程实战》——1.8 在代码中配置matplotlib参数
- python 数字
- python 六种典型的异常
- Python 开发的 IDE 和代码编辑器,你选择的哪一个?
- 运行openai的gym代码报错提示import pyglet,安装后依然报错:ImportError: sys.meta_path is None, Python is likely shutting down
- 用一行Python代码,为图片上水印版权!
- 华为OD机试 - 新学校选址(Python)| 真题+思路+考点+代码+岗位
- 华为OD机试 - 求最大数字(Python)| 真题+思路+考点+代码+岗位
- 华为OD机试 - 机器人走迷宫(Python)| 真题+思路+考点+代码+岗位
- VS的功能挺强大的,可以编写C#,python及其他代码
- 从零上手Python关键代码
- python键盘输入及其合法性检查的例子
- Python之练习数学运用代码
- 使用 Mypy 进行 Python 静态类型检查
- Python满屏表白代码