当前位置:网站首页>利用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)
边栏推荐
猜你喜欢

2022新消费半年盘点:行业遇冷,但这九个赛道依然吸金

Mathematical modeling for war preparation 36 time series model 2

云技能提升好伙伴,亚马逊云师兄今天正式营业

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

The new tea drinks are "dead and alive", but the suppliers are "full of pots and bowls"?

牛客网:最小花费爬楼梯

I 用c I 实现“栈”

Cloud XR, how to help industrial upgrading
![[activity registration] it's your turn to explore the yuan universe! I will be waiting for you in Shenzhen on July 2!](/img/cf/6fc5f7cafbdc5e165eb119b9b41cd9.jpg)
[activity registration] it's your turn to explore the yuan universe! I will be waiting for you in Shenzhen on July 2!

Arcmap操作系列:80平面转经纬度84
随机推荐
Bidding announcement: remote disaster recovery project of Shenzhen Finance Bureau database
更多龙蜥自研特性!生产可用的 Anolis OS 8.6 正式发布
Build cloud native observability capability suitable for organizations
Rongsheng biology rushes to the scientific innovation board: it plans to raise 1.25 billion yuan, with an annual revenue of 260million yuan
I implement "stack" with C I
招标公告:天津市住房公积金管理中心数据库一体机及数据库软件项目(预算645万)
为了使远程工作不受影响,我写了一个内部的聊天室 | 社区征文
JS Es5 can also create constants?
2020蓝桥杯国赛B组-搬砖-(贪心排序+01背包)
搬运两个负载均衡的笔记,日后省的找
On July 2, I invited you to TD Hero online conference
What role does "low code" play in enterprise digital transformation?
Li Zexiang, a legendary Chinese professor, is making unicorns in batches
CVPR 2022 - Tesla AI proposed: generalized pedestrian re recognition based on graph sampling depth metric learning
POJ Project Summer
Additional: (not written yet, don't look at ~ ~ ~) webmvcconfigurer interface;
Etcd教程 — 第八章 Etcd之Compact、Watch和Lease API
[machine learning] K-means clustering analysis
Good partner for cloud skill improvement, senior brother cloud of Amazon officially opened today
思源笔记:能否提供页面内折叠所有标题的快捷键?