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正则表达式判断字符串是否是全部小写示例
Dec 25 Python
Python实现在Linux系统下更改当前进程运行用户
Feb 04 Python
python实现简单聊天应用 python群聊和点对点均实现
Sep 14 Python
Python排序搜索基本算法之堆排序实例详解
Dec 08 Python
Python cookbook(数据结构与算法)保存最后N个元素的方法
Feb 13 Python
Python Web编程之WSGI协议简介
Jul 18 Python
Python判断变量名是否合法的方法示例
Jan 28 Python
pytorch中使用cuda扩展的实现示例
Feb 12 Python
Django 删除upload_to文件的步骤
Mar 30 Python
python递归函数求n的阶乘,优缺点及递归次数设置方式
Apr 02 Python
tensorflow2.0的函数签名与图结构(推荐)
Apr 28 Python
python针对Oracle常见查询操作实例分析
Apr 30 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
解决PHP4.0 和 PHP5.0类构造函数的兼容问题
2013/08/01 PHP
php中引用符号(&)的使用详解
2013/11/13 PHP
php中json_encode UTF-8中文乱码的更好解决方法
2014/09/28 PHP
浅谈php(codeigniter)安全性注意事项
2017/04/06 PHP
javascript 设置文本框中焦点的位置
2009/11/20 Javascript
判断iframe是否加载完成的完美方法
2010/01/07 Javascript
JS延迟加载(setTimeout) JS最后加载
2010/07/15 Javascript
jquery select(列表)的操作(取值/赋值)
2011/03/16 Javascript
深入了解javascript中的prototype与继承
2013/04/14 Javascript
jQuery 无限级菜单的简单实例
2014/02/21 Javascript
js阻止浏览器默认行为的简单实例
2016/05/15 Javascript
实用又漂亮的BootstrapValidator表单验证插件
2016/05/30 Javascript
JS实现title标题栏文字不间断滚动显示效果
2016/09/07 Javascript
JavaScript实现清空(重置)文件类型INPUT元素值的方法
2016/11/17 Javascript
ES6新增的math,Number方法
2017/08/06 Javascript
vue element-ui table表格滚动加载方法
2018/03/02 Javascript
JavaScript实现JSON合并操作示例【递归深度合并】
2018/09/07 Javascript
Python 代码性能优化技巧分享
2012/08/07 Python
Python实现读取并保存文件的类
2017/05/11 Python
python2 与python3的print区别小结
2018/01/16 Python
Python中的Numpy矩阵操作
2018/08/12 Python
python查看模块安装位置的方法
2018/10/16 Python
[原创]Python入门教程1. 基本运算【四则运算、变量、math模块等】
2018/10/28 Python
python实现比较类的两个instance(对象)是否相等的方法分析
2019/06/26 Python
TensorFLow 数学运算的示例代码
2020/04/21 Python
PyCharm上安装Package的实现(以pandas为例)
2020/09/18 Python
python模拟点击玩游戏的实例讲解
2020/11/26 Python
HTML5中实现拖放效果无须借助javascript
2012/12/26 HTML / CSS
台湾演唱会订票网站:StubHub台湾
2019/06/11 全球购物
科颜氏印度官网:Kiehl’s印度
2021/02/20 全球购物
一套比较完整的软件测试人员面试题
2012/05/13 面试题
建筑设计学生的自我评价
2014/01/16 职场文书
小学六年级学生评语
2014/04/22 职场文书
国家领导干部党的群众路线教育实践活动批评与自我批评材料
2014/09/23 职场文书
女性健康知识讲座主持词
2015/07/04 职场文书
世界上超棒的8种逻辑思维
2019/08/06 职场文书