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统计代码行的示例(包括空行和注释)
Jul 24 Python
python 读取Linux服务器上的文件方法
Dec 27 Python
详解python爬虫系列之初识爬虫
Apr 06 Python
Django框架搭建的简易图书信息网站案例
May 25 Python
python matplotlib库直方图绘制详解
Aug 10 Python
Python学习笔记之列表推导式实例分析
Aug 13 Python
使用PyTorch将文件夹下的图片分为训练集和验证集实例
Jan 08 Python
python 爬取百度文库并下载(免费文章限定)
Dec 04 Python
如何查看python关键字
Jan 17 Python
Python 文本滚动播放器的实现代码
Apr 25 Python
Python pygame实现中国象棋单机版源码
Jun 20 Python
python析构函数用法及注意事项
Jun 22 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
PHP游戏编程25个脚本代码
2011/02/08 PHP
LotusPhp笔记之:Cookie组件的使用详解
2013/05/06 PHP
PHP临时文件的安全性分析
2014/07/04 PHP
php获取当月最后一天函数分享
2015/02/02 PHP
PHP封装的Twitter访问类实例
2015/07/18 PHP
使用PHP json_decode可能遇到的坑与解决方法
2017/08/03 PHP
php微信开发之关键词回复功能
2018/06/13 PHP
PHP预定义超全局数组变量小结
2018/08/20 PHP
从JavaScript的函数重名看其初始化方式
2007/03/08 Javascript
jQuery图片的展开和收缩实现代码
2013/04/16 Javascript
一种Javascript解释ajax返回的json的好方法(推荐)
2016/06/02 Javascript
bootstrap jquery dataTable 异步ajax刷新表格数据的实现方法
2017/02/10 Javascript
vue树形结构获取键值的方法示例
2018/06/21 Javascript
VuePress 静态网站生成方法步骤
2019/02/14 Javascript
通过javascript实现段落的收缩与展开
2019/06/26 Javascript
鸿蒙系统中的 JS 开发框架
2020/09/18 Javascript
Python 过滤字符串的技巧,map与itertools.imap
2008/09/06 Python
Python实现多线程抓取妹子图
2015/08/08 Python
Python中pygame的mouse鼠标事件用法实例
2015/11/11 Python
Django中的Model操作表的实现
2018/07/24 Python
详解Django项目中模板标签及模板的继承与引用(网站中快速布置广告)
2019/03/27 Python
Python 3.8新特征之asyncio REPL
2019/05/28 Python
OpenCV 边缘检测
2019/07/10 Python
使用Python实现画一个中国地图
2019/11/23 Python
如何理解python中数字列表
2020/05/29 Python
PyCharm Ctrl+Shift+F 失灵的简单有效解决操作
2021/01/15 Python
印度尼西亚最大和最全面的网络商城:Blibli.com
2017/10/04 全球购物
Paul’s Boutique官网:英国时尚手袋品牌
2018/03/31 全球购物
ESDlife健康生活易:身体检查预订、搜寻及比较
2019/05/10 全球购物
初中女生自我鉴定
2013/12/19 职场文书
参观监狱心得体会
2014/01/02 职场文书
心理咨询承诺书
2014/05/20 职场文书
预备党员转正意见
2015/06/01 职场文书
小学三年级作文之写景
2019/11/05 职场文书
golang elasticsearch Client的使用详解
2021/05/05 Golang
Python 实现绘制子图及子图刻度的变换等问题
2021/05/31 Python