当前位置:网站首页>Undistorted resize using pil
Undistorted resize using pil
2022-06-30 16:48:00 【Wutiande, young Xia】
Make a deformation free resize

Code
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 You can zoom to 224
# h Conduct padding
new_h = int(h * (target_size/w))
image = image.resize((target_size,new_h),Image.Resampling.BICUBIC)
# Generate one targe_size Size chart
aimage = Image.new('RGB',(target_size,target_size),(127,127,127))
# Calculate the size to be pasted on it
psize_up = (target_size - new_h)//2
# Paste the scaled image
aimage.paste(image,(0,psize_up))
aimage.show()
else:
# minx==w
# h You can zoom to 224
# w Conduct padding
new_w = int(w * (target_size/h))
image = image.resize((new_w,target_size),Image.Resampling.BICUBIC)
# Generate one targe_size Size chart
aimage = Image.new('RGB',(target_size,target_size),(127,127,127))
# Calculate the size to be pasted on the left
psize_left = (target_size - new_w)//2
# Paste the scaled image
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)
边栏推荐
- RT thread heap size Setting
- 声网自研传输层协议 AUT 的落地实践丨Dev for Dev 专栏
- Home office discussion on the experience of remote assistance to quickly improve efficiency | community essay solicitation
- MC Instruction Decoder
- [Verilog quick start of Niuke online question series] ~ bit splitting and operation
- 构建适合组织的云原生可观测性能力
- 华为帐号多端协同,打造美好互联生活
- Under the pressure of technology, you can quickly get started with eth smart contract development, which will take you into the ETH world
- 腾讯二面:@Bean 与 @Component 用在同一个类上,会怎么样?
- [activity registration] it's your turn to explore the yuan universe! I will be waiting for you in Shenzhen on July 2!
猜你喜欢

【牛客网刷题系列 之 Verilog快速入门】~ 位拆分与运算

声网自研传输层协议 AUT 的落地实践丨Dev for Dev 专栏

Under the pressure of technology, you can quickly get started with eth smart contract development, which will take you into the ETH world

There are so many kinds of coupons. First distinguish them clearly and then collect the wool!

今晚19:00知识赋能第2期直播丨OpenHarmony智能家居项目之控制面板界面设计

Mathematical modeling for war preparation 36 time series model 2

Mathematical modeling for war preparation 34-bp neural network prediction 2

容联云首发基于统信UOS的Rphone,打造国产化联络中心新生态

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

Distributed machine learning: model average Ma and elastic average easgd (pyspark)
随机推荐
The image variables in the Halcon variable window are not displayed, and it is useless to restart the software and the computer
go-micro教程 — 第一章 快速入门
Asp. NETCORE uses cache and AOP to prevent repeated commit
中国传奇教授李泽湘,正在批量制造独角兽
居家办公浅谈远程协助快速提效心得 | 社区征文
BC1.2 PD协议
Symantec electronic sprint technology innovation board: Tan Jian, the actual controller, is an American who plans to raise 620million yuan
备战数学建模34-BP神经网络预测2
2022蓝桥杯国赛B组-费用报销-(线性dp|状态dp)
快照和备份
Installing jupyter notebook under Anaconda
KDD 2022 | how far are we from the general pre training recommendation model? Universal sequence representation learning model unisrec for recommender system
2022 Blue Bridge Cup group B -2022- (01 backpack to calculate the number of schemes)
Mathematical modeling for war preparation 35 time series prediction model
24:第三章:开发通行证服务:7:自定义异常(来表征程序中出现的错误);创建GraceExceptionHandler来全局统一处理异常(根据异常信息,构建对应的API统一返回对象的,JSON数据);
Halcon knowledge: matrix topic [02]
Dart: string replace related methods to solve replacement characters
MySQL开放远程连接权限的两种方法
How to get the preferential activities for stock account opening? Is online account opening safe?
OpenCV中LineTypes各枚举值(LINE_4 、LINE_8 、LINE_AA )的含义