python实现按长宽比缩放图片


Posted in Python onJune 07, 2018

使用python按图片固定长宽比缩放图片到指定图片大小,空白部分填充为黑色。

代码

# -*- coding: utf-8 -*-

from PIL import Image

class image_aspect():

 def __init__(self, image_file, aspect_width, aspect_height):
  self.img = Image.open(image_file)
  self.aspect_width = aspect_width
  self.aspect_height = aspect_height
  self.result_image = None

 def change_aspect_rate(self):
  img_width = self.img.size[0]
  img_height = self.img.size[1]

  if (img_width / img_height) > (self.aspect_width / self.aspect_height):
   rate = self.aspect_width / img_width
  else:
   rate = self.aspect_height / img_height

  rate = round(rate, 1)
  print(rate)
  self.img = self.img.resize((int(img_width * rate), int(img_height * rate)))
  return self

 def past_background(self):
  self.result_image = Image.new("RGB", [self.aspect_width, self.aspect_height], (0, 0, 0, 255))
  self.result_image.paste(self.img, (int((self.aspect_width - self.img.size[0]) / 2), int((self.aspect_height - self.img.size[1]) / 2)))
  return self

 def save_result(self, file_name):
  self.result_image.save(file_name)


if __name__ == "__main__":
 image_aspect("./source/test.jpg", 1920, 1080).change_aspect_rate().past_background().save_result("./target/test.jpg")

感言

有兴趣的朋友可以将图片路径,长宽值,背景颜色等参数化
封装成api做为个公共服务

本文源码下载

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Python 相关文章推荐
Python 连连看连接算法
Nov 22 Python
用实例详解Python中的Django框架中prefetch_related()函数对数据库查询的优化
Apr 01 Python
python批量提取word内信息
Aug 09 Python
简单了解python的break、continue、pass
Jul 08 Python
PyTorch中Tensor的维度变换实现
Aug 18 Python
Python 转换文本编码实现解析
Aug 27 Python
Python pygame绘制文字制作滚动文字过程解析
Dec 12 Python
python json load json 数据后出现乱序的解决方案
Feb 27 Python
python中sys模块是做什么用的
Aug 16 Python
通过实例了解python__slots__使用方法
Sep 14 Python
python利用paramiko实现交换机巡检的示例
Sep 22 Python
社区版pycharm创建django项目的方法(pycharm的newproject左侧没有项目选项)
Sep 23 Python
python实现批量修改图片格式和尺寸
Jun 07 #Python
python实现批量图片格式转换
Jun 16 #Python
python脚本实现验证码识别
Jun 07 #Python
python 创建一个空dataframe 然后添加行数据的实例
Jun 07 #Python
使用Python处理Excel表格的简单方法
Jun 07 #Python
python实现验证码识别功能
Jun 07 #Python
通过Pandas读取大文件的实例
Jun 07 #Python
You might like
福利彩票幸运号码自动生成器
2006/10/09 PHP
PHP在网页中动态生成PDF文件详细教程
2014/07/05 PHP
php微信公众号js-sdk开发应用
2016/11/28 PHP
php与c 实现按行读取文件实例代码
2017/01/03 PHP
php实现遍历文件夹的方法汇总
2017/03/02 PHP
javascript深入理解js闭包
2010/07/03 Javascript
JavaScript词法作用域与调用对象深入理解
2012/11/29 Javascript
Eval and new funciton not the same thing
2012/12/27 Javascript
将中国标准时间转换成标准格式的代码
2014/03/20 Javascript
Javascript学习指南
2014/12/01 Javascript
使用正则表达式的格式化与高亮显示json字符串
2014/12/03 Javascript
jQuery实现的网页右下角tab样式在线客服效果代码
2015/10/23 Javascript
浅析2种JavaScript继承方式
2015/12/04 Javascript
JS通过Cookie判断页面是否为首次打开
2016/02/05 Javascript
AngularJS Select(选择框)使用详解
2017/01/18 Javascript
基于对象合并功能的实现示例
2017/10/10 Javascript
详解vue + vuex + directives实现权限按钮的思路
2017/10/24 Javascript
详解tween.js 中文使用指南
2018/01/05 Javascript
js捆绑TypeScript声明文件的方法教程
2018/04/13 Javascript
vue axios数据请求及vue中使用axios的方法
2018/09/10 Javascript
Python中基础的socket编程实战攻略
2016/06/01 Python
详解如何使用Python编写vim插件
2017/11/28 Python
pycharm安装和首次使用教程
2018/08/27 Python
python中类的属性和方法介绍
2018/11/27 Python
利用python实现短信和电话提醒功能的例子
2019/08/08 Python
Python通过2种方法输出带颜色字体
2020/03/02 Python
python实现微信打飞机游戏
2020/03/24 Python
python selenium 获取接口数据的实现
2020/12/07 Python
美国高档百货Nordstrom的折扣店:Nordstrom Rack
2017/11/13 全球购物
毕业自荐信
2013/12/16 职场文书
大学生简短的自我评价分享
2014/02/20 职场文书
营销部内勤岗位职责
2014/04/30 职场文书
个人借款协议书范本
2014/11/17 职场文书
2014年村官工作总结
2014/11/24 职场文书
学生会辞职信
2015/03/02 职场文书
于丹论语心得观后感
2015/06/15 职场文书