当前位置:网站首页>利用PIL进行不失真的resize
利用PIL进行不失真的resize
2022-06-30 15:48:00 【吴天德少侠】
进行不会变形的resize

代码
import os
from PIL import Image
import math
def resize_pad(image,target_size):
w,h = image.size
minx = min(w,h)
if minx == h:
# w可以缩放到224
# h进行padding
new_h = int(h * (target_size/w))
image = image.resize((target_size,new_h),Image.Resampling.BICUBIC)
# 生成一张targe_size大小的图
aimage = Image.new('RGB',(target_size,target_size),(127,127,127))
# 计算上面需要贴的大小
psize_up = (target_size - new_h)//2
# 将缩放后的图像贴上去
aimage.paste(image,(0,psize_up))
aimage.show()
else:
# minx==w
# h可以缩放到224
# w进行padding
new_w = int(w * (target_size/h))
image = image.resize((new_w,target_size),Image.Resampling.BICUBIC)
# 生成一张targe_size大小的图
aimage = Image.new('RGB',(target_size,target_size),(127,127,127))
# 计算左边需要贴的大小
psize_left = (target_size - new_w)//2
# 将缩放后的图像贴上去
aimage.paste(image,(psize_left,0))
aimage.show()
if __name__ == '__main__':
image_path = "E:/my_datasets/keypoint_2d/hand_keypoint/hand_keypoint/val/JPEGImages/1577.png"
image = Image.open(image_path).convert('RGB')
ori = image.resize((224,224))
ori.show()
resize_pad(image,target_size=224)
边栏推荐
- CMakeLists 基础
- Halcon knowledge: regional topics [07]
- Implementation of Devops in the core field of qunar, the Internet R & D Efficiency
- 备战数学建模36-时间序列模型2
- 华为帐号多端协同,打造美好互联生活
- MicroBlaze serial port learning · 2
- Mysql8.0 method and steps for enabling remote connection permission
- POJ Project Summer
- Interesting research on mouse pointer interaction
- Bidding announcement: Taizhou Unicom Oracle all in one machine and database maintenance service project in 2022
猜你喜欢

How cloudxr promotes the future development of XR

9:第三章:电商工程分析:4:【通用模块】;(待写……)

How the edge computing platform helps the development of the Internet of things

微信表情符号写入判决书,你发的OK、炸弹都可能成为“呈堂证供”

Go zero micro Service Practice Series (VIII. How to handle tens of thousands of order requests per second)

居家办公浅谈远程协助快速提效心得 | 社区征文

TCP Socket与TCP 连接

荣盛生物冲刺科创板:拟募资12.5亿 年营收2.6亿

Anaconda下安装Jupyter notebook

Mathematical modeling for war preparation 35 time series prediction model
随机推荐
Siyuan notes: can you provide shortcut keys for folding all titles on the page?
Cloud XR, how to help industrial upgrading
【活动报名】探秘元宇宙,就差你了!7月2号我在深圳现场等你!
Rong Lianyun launched rphone based on Tongxin UOS to create a new ecology of localization contact center
Hologres shared cluster helps Taobao subscribe to the extreme refined operation
Good partner for cloud skill improvement, senior brother cloud of Amazon officially opened today
安全帽佩戴检测算法研究
【机器学习】K-means聚类分析
[machine learning] K-means clustering analysis
牛客网:最小花费爬楼梯
Does flinkcdc have to be a clustered version if the monitored database is Mongo
牛客网:有多少个不同的二叉搜索树
GaussDB创新特性解读:Partial Result Cache,通过缓存中间结果对算子进行加速
【Verilog基础】十进制负数的八进制、十六进制表示
Additional: (not written yet, don't look at ~ ~ ~) webmvcconfigurer interface;
go-micro教程 — 第一章 快速入门
更多龙蜥自研特性!生产可用的 Anolis OS 8.6 正式发布
On July 2, I invited you to TD Hero online conference
名单揭晓 | 2021年度中国杰出知识产权服务团队
云和恩墨中标天津滨海农村商业银行2022-2023年度Oracle维保项目