Python PIL按比例裁剪图片


Posted in Python onMay 11, 2022

PIL图片如何按比例裁剪

问题描述

如图片比例为 1:1 裁剪为 4:3

1.jpg

Python PIL按比例裁剪图片

解决方案

from PIL import Image
def image_clip(filename, savename, width_scale, height_scale):
    """图像裁剪
    :param filename: 原图路径
    :param savename: 保存图片路径
    :param width_scale: 宽的比例
    :param height_scale: 高的比例
    """
    image = Image.open(filename)
    (width, height), (_width, _height) = image.size, image.size
    _height = width / width_scale * height_scale
    if _height > height:
        _height = height
        _width = width_scale * height / height_scale
    image.crop((0, 0, _width, _height)).save(savename)  # 左上角
    # image.crop((0, height - _height, _width, height)).save(savename)  # 左下角
    # image.crop((width - _width, 0, width, _height)).save(savename)  # 右上角
    # image.crop((width - _width, height - _height, width, height)).save(savename)  # 右下角
if __name__ == '__main__':
    filename = '1.jpg'
    savename = 'result.jpg'
    image_clip(filename, savename, width_scale=4, height_scale=3)
    # image_clip(filename, savename, width_scale=3, height_scale=4)

效果

Python PIL按比例裁剪图片

PIL调整图片大小

使用 PIL 在图片比例不变的情况下修改图片大小。

介绍

Image.resize

def resize(self, size, resample=BICUBIC, box=None, reducing_gap=None):
    """
    Returns a resized copy of this image.
    返回此图像的大小调整后的副本。
    :param size: The requested size in pixels, as a 2-tuple:
       (width, height).
     param size: 请求的大小(以像素为单位),是一个二元数组:(width, height)
    :param resample: An optional resampling filter.  This can be
       one of :py:attr:`PIL.Image.NEAREST`, :py:attr:`PIL.Image.BOX`,
       :py:attr:`PIL.Image.BILINEAR`, :py:attr:`PIL.Image.HAMMING`,
       :py:attr:`PIL.Image.BICUBIC` or :py:attr:`PIL.Image.LANCZOS`.
       Default filter is :py:attr:`PIL.Image.BICUBIC`.
       If the image has mode "1" or "P", it is
       always set to :py:attr:`PIL.Image.NEAREST`.
       See: :ref:`concept-filters`.
     param resample: 一个可选的重采样过滤器。
    :param box: An optional 4-tuple of floats providing
       the source image region to be scaled.
       The values must be within (0, 0, width, height) rectangle.
       If omitted or None, the entire source is used.
     param box: 可选的4元浮点数,提供要缩放的源映像区域。
    :param reducing_gap: Apply optimization by resizing the image
       in two steps. First, reducing the image by integer times
       using :py:meth:`~PIL.Image.Image.reduce`.
       Second, resizing using regular resampling. The last step
       changes size no less than by ``reducing_gap`` times.
       ``reducing_gap`` may be None (no first step is performed)
       or should be greater than 1.0. The bigger ``reducing_gap``,
       the closer the result to the fair resampling.
       The smaller ``reducing_gap``, the faster resizing.
       With ``reducing_gap`` greater or equal to 3.0, the result is
       indistinguishable from fair resampling in most cases.
       The default value is None (no optimization).
     param reducing_gap: 通过两个步骤调整图像大小来应用优化。
    :returns: An :py:class:`~PIL.Image.Image` object.
     returns: 返回一个 PIL.Image.Image 对象
    """

看代码吧

from PIL import Image
 
 
image = Image.open('图片路径')
 
# 调整图片大小,并保持比例不变
# 给定一个基本宽度
base_width = 50
 
# 基本宽度与原图宽度的比例
w_percent = base_width / float(image.size[0])
 
# 计算比例不变的条件下新图的长度
h_size = int(float(image.size[1]) * float(w_percent))
 
# 重新设置大小
# 默认情况下,PIL使用Image.NEAREST过滤器进行大小调整,从而获得良好的性能,但质量很差。
image = image.resize((base_width, h_size), Image.ANTIALIAS)

Tags in this post...

Python 相关文章推荐
python文件写入实例分析
Apr 08 Python
python django事务transaction源码分析详解
Mar 17 Python
Python3简单实例计算同花的概率代码
Dec 06 Python
Python中getpass模块无回显输入源码解析
Jan 11 Python
对python中使用requests模块参数编码的不同处理方法
May 18 Python
python和opencv实现抠图
Jul 18 Python
python的中异常处理机制
Aug 30 Python
python匹配两个短语之间的字符实例
Dec 25 Python
python 读取数据库并绘图的实例
Dec 03 Python
tensorflow实现tensor中满足某一条件的数值取出组成新的tensor
Jan 04 Python
python中rc1什么意思
Jun 19 Python
python process模块的使用简介
May 14 Python
python 学习GCN图卷积神经网络
May 11 #Python
Python+Pillow+Pytesseract实现验证码识别
May 11 #Python
Python 绘制多因子柱状图
PyCharm 配置SSH和SFTP连接远程服务器
May 11 #Python
Python 文字识别
May 11 #Python
解决Python保存文件名太长OSError: [Errno 36] File name too long
May 11 #Python
Python 匹配文本并在其上一行追加文本
May 11 #Python
You might like
粗略计算在线时间,bug:ip相同
2006/12/09 PHP
PHP数组和explode函数示例总结
2015/05/08 PHP
thinkPHP2.1自定义标签库的导入方法详解
2016/07/20 PHP
Laravel框架实现的记录SQL日志功能示例
2018/06/19 PHP
PHP使用Redis实现Session共享的实现示例
2019/05/12 PHP
PHP FileSystem 文件系统常用api整理总结
2019/07/12 PHP
JS中style属性
2006/10/11 Javascript
jquery slibings选取同级其他元素的实现代码
2013/11/15 Javascript
jQuery实现设置、移除文本框默认值功能
2015/01/13 Javascript
Zero Clipboard实现浏览器复制到剪贴板的方法(多个复制按钮)
2016/03/24 Javascript
jQuery 弹出层插件(推荐)
2016/05/24 Javascript
Listloading.js移动端上拉下拉刷新组件
2016/08/04 Javascript
Node.js 8 中的 util.promisify的详解
2017/06/12 Javascript
JavaScript命令模式原理与用法实例详解
2020/03/10 Javascript
javascript实现移动端触屏拖拽功能
2020/07/29 Javascript
Python简单计算给定某一年的某一天是星期几示例
2018/06/27 Python
Python3.0 实现决策树算法的流程
2019/08/08 Python
Python Django 添加首页尾页上一页下一页代码实例
2019/08/21 Python
python安装virtualenv虚拟环境步骤图文详解
2019/09/18 Python
python递归函数求n的阶乘,优缺点及递归次数设置方式
2020/04/02 Python
css3动画事件—webkitAnimationEnd与计时器time事件
2013/01/31 HTML / CSS
HTML5 form标签之解放表单验证、增加文件上传、集成拖放的使用方法
2013/04/24 HTML / CSS
美国领先的精品家居照明和装饰产品在线零售商:LightsOnline.com
2018/01/23 全球购物
美国在线宠物商店:Chewy
2019/01/12 全球购物
GC是什么?为什么要有GC?
2013/12/08 面试题
土木工程专业大学毕业生求职信
2013/10/13 职场文书
大专计算机个人求职的自我评价
2013/10/21 职场文书
会计专业自我评价
2014/02/12 职场文书
大学学生会竞选演讲稿
2014/04/25 职场文书
项目经理任命书内容
2014/06/06 职场文书
小学综合实践活动总结
2014/07/07 职场文书
2014年教研室工作总结
2014/12/06 职场文书
简历中的自我评价应该这样写!
2019/07/12 职场文书
Axios取消重复请求的方法实例详解
2021/06/15 Javascript
Python实现位图分割的效果
2021/11/20 Python
阿里云国际版 使用Nginx作为HTTPS转发代理服务器
2022/05/11 Servers