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 03 Python
Python安装第三方库的3种方法
Jun 21 Python
Python中Class类用法实例分析
Nov 12 Python
Python进度条实时显示处理进度的示例代码
Jan 30 Python
PyQt5响应回车事件的方法
Jun 25 Python
pandas 如何分割字符的实现方法
Jul 29 Python
Python3如何对urllib和urllib2进行重构
Nov 25 Python
实现Python与STM32通信方式
Dec 18 Python
Python 删除List元素的三种方法remove、pop、del
Nov 16 Python
如何用tempfile库创建python进程中的临时文件
Jan 28 Python
python实现简单文件读写函数
Feb 25 Python
Django对接elasticsearch实现全文检索的示例代码
Aug 02 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
咖啡因含量是由谁决定的?低因咖啡怎么来?低因咖啡适合什么人喝
2021/03/06 新手入门
PHP的开发框架的现状和展望
2007/03/16 PHP
php不使用插件导出excel的简单方法
2014/03/04 PHP
php打乱数组二维数组多维数组的简单实例
2016/06/17 PHP
laravel5.4利用163邮箱发送邮件的步骤详解
2017/09/22 PHP
PHP数组对象与Json转换操作实例分析
2019/10/22 PHP
laravel框架中视图的基本使用方法分析
2019/11/23 PHP
prototype 1.5 & scriptaculous 1.6.1 学习笔记
2006/09/07 Javascript
使用正则替换变量
2007/05/05 Javascript
几个常用的JavaScript字符串处理函数 - split()、join()、substring()和indexOf()
2009/06/02 Javascript
学习ExtJS TextField常用方法
2009/10/07 Javascript
jQuery学习笔记之jQuery的事件
2010/12/22 Javascript
js 用CreateElement动态创建标签示例
2013/11/20 Javascript
基于jQuery实现表格内容的筛选功能
2016/08/21 Javascript
深入理解JS实现快速排序和去重
2016/10/17 Javascript
原生JS获取元素集合的子元素宽度实例
2016/12/14 Javascript
jquery实现手机端单店铺购物车结算删除功能
2017/02/22 Javascript
基于vue2.0+vuex的日期选择组件功能实现
2017/03/13 Javascript
关于echarts在节点显示动态数据及添加提示文本所遇到的问题
2018/04/20 Javascript
如何实现小程序tab栏下划线动画效果
2019/05/18 Javascript
深入理解令牌认证机制(token)
2019/08/22 Javascript
layer提示框添加多个按钮选择的实例
2019/09/12 Javascript
js中关于Blob对象的介绍与使用
2019/11/29 Javascript
[05:17]DOTA2睡衣妹卖萌求签名 CJ第二天全明星影像
2013/07/28 DOTA
python多线程之事件Event的使用详解
2018/04/27 Python
用python3 返回鼠标位置的实现方法(带界面)
2019/07/05 Python
python tkinter图形界面代码统计工具(更新)
2019/09/18 Python
python plt可视化——打印特殊符号和制作图例代码
2020/04/17 Python
viagogo意大利票务平台:演唱会、体育比赛、戏剧门票
2018/01/26 全球购物
美国名牌手表折扣网站:Jomashop
2020/05/22 全球购物
我的求职计划书
2014/01/10 职场文书
寻找最美家庭活动方案
2014/08/20 职场文书
校长师德师风自我剖析材料
2014/09/29 职场文书
如何在Python中创建二叉树
2021/03/30 Python
python实现大文本文件分割成多个小文件
2021/04/20 Python
微软团队与 NASA 科学家和惠普企业(HPE)的工程师合作
2022/04/21 数码科技