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封装shell命令实例分析
May 05 Python
python tensorflow学习之识别单张图片的实现的示例
Feb 09 Python
Python基于SMTP协议实现发送邮件功能详解
Aug 14 Python
python实现写数字文件名的递增保存文件方法
Oct 25 Python
一篇文章搞懂Python的类与对象名称空间
Dec 10 Python
Python通过for循环理解迭代器和生成器实例详解
Feb 16 Python
Python实现的旋转数组功能算法示例
Feb 23 Python
pandas计数 value_counts()的使用
Jun 24 Python
Pytest mark使用实例及原理解析
Feb 22 Python
基于python实现音乐播放器代码实例
Jul 01 Python
Pytest测试框架基本使用方法详解
Nov 25 Python
Python实现视频中添加音频工具详解
Dec 06 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
《星际争霸2》终章已出 RTS时代宣告终结
2017/02/07 星际争霸
浅谈电磁辐射对健康的影响
2021/03/01 无线电
PHP原理之异常机制深入分析
2010/08/08 PHP
php结合ajax实现手机发红包的案例
2016/10/13 PHP
JavaScript的parseInt 取整使用
2011/05/09 Javascript
javascript:history.go()和History.back()的区别及应用
2012/11/25 Javascript
javascript实现颜色渐变的方法
2013/10/30 Javascript
jquery select 设置默认选中的示例代码
2014/02/07 Javascript
根据当前时间在jsp页面上显示上午或下午
2014/08/18 Javascript
jquery+php随机生成红包金额数量代码分享
2015/08/27 Javascript
Bootstrap教程JS插件滚动监听学习笔记分享
2016/05/18 Javascript
详解Vue中使用v-for语句抛出错误的解决方案
2017/05/04 Javascript
微信小程序使用progress组件实现显示进度功能【附源码下载】
2017/12/12 Javascript
vue基于mint-ui实现城市选择三级联动
2020/06/30 Javascript
JavaScript事件发布/订阅模式原理与用法分析
2018/08/21 Javascript
koa源码中promise的解读
2018/11/13 Javascript
Nodejs模块的调用操作实例分析
2018/12/25 NodeJs
详解使用Nuxt.js快速搭建服务端渲染(SSR)应用
2019/03/13 Javascript
python实现发送邮件功能
2017/07/22 Python
解决python读取几千万行的大表内存问题
2018/06/26 Python
python 2.7.13 安装配置方法图文教程
2018/09/18 Python
对python中的iter()函数与next()函数详解
2018/10/18 Python
Python3+Pycharm+PyQt5环境搭建步骤图文详解
2019/05/29 Python
基于Python打造账号共享浏览器功能
2019/05/30 Python
pycharm 2019 最新激活方式(pycharm破解、激活)
2020/09/22 Python
解决python gdal投影坐标系转换的问题
2020/01/17 Python
从零开始的TensorFlow+VScode开发环境搭建的步骤(图文)
2020/08/31 Python
教你使用Canvas处理图片的方法
2017/11/28 HTML / CSS
娇韵诗加拿大官网:Clarins加拿大
2017/11/20 全球购物
电子商务专业实习生自我鉴定
2013/09/24 职场文书
美术国培研修感言
2014/02/12 职场文书
3分钟演讲稿
2014/04/30 职场文书
学校食堂食品安全责任书
2014/07/28 职场文书
怎样写离婚协议书
2014/09/10 职场文书
加强作风建设工作总结
2014/10/23 职场文书
导游词范文
2015/02/13 职场文书