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的lambda匿名函数的简单介绍
Apr 25 Python
基于wxpython实现的windows GUI程序实例
May 30 Python
Python反射用法实例简析
Dec 22 Python
Python把csv数据写入list和字典类型的变量脚本方法
Jun 15 Python
TensorFlow 合并/连接数组的方法
Jul 27 Python
Python通过for循环理解迭代器和生成器实例详解
Feb 16 Python
Django异步任务线程池实现原理
Dec 17 Python
如何在mac下配置python虚拟环境
Jul 06 Python
opencv 图像轮廓的实现示例
Jul 08 Python
详解Python直接赋值,深拷贝和浅拷贝
Jul 09 Python
浅谈Python爬虫原理与数据抓取
Jul 21 Python
如何在Python3中使用telnetlib模块连接网络设备
Sep 21 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
支持数组的ADDSLASHES的php函数
2010/02/16 PHP
PHP网页游戏学习之Xnova(ogame)源码解读(十一)
2014/06/25 PHP
php获取文件后缀的9种方法
2016/03/22 PHP
用js判断浏览器是否是IE的比较好的办法
2007/05/08 Javascript
cument.execCommand()用法深入理解
2012/12/04 Javascript
利用Jquery实现可多选的下拉框
2014/02/21 Javascript
javascript实现仿IE顶部的可关闭警告条
2015/05/05 Javascript
jquery计算鼠标和指定元素之间距离的方法
2015/06/26 Javascript
简单谈谈node.js 版本控制 nvm和 n
2015/10/15 Javascript
浅析AngularJS中的指令
2016/03/20 Javascript
AngularJS基础 ng-mousemove 指令简单示例
2016/08/02 Javascript
浅谈JSON.stringify()和JOSN.parse()方法的不同
2016/08/29 Javascript
JS表格组件BootstrapTable行内编辑解决方案x-editable
2016/09/01 Javascript
Angular企业级开发——MVC之控制器详解
2017/02/20 Javascript
js图片延迟加载(Lazyload)三种实现方式
2017/03/01 Javascript
vue-cli开发环境实现跨域请求的方法
2018/04/07 Javascript
《javascript少儿编程》location术语总结
2018/05/27 Javascript
JS函数节流和防抖之间的区分和实现详解
2019/01/11 Javascript
JS面向对象编程实现的拖拽功能案例详解
2020/03/03 Javascript
vue项目实现设置根据路由高亮对应的菜单项操作
2020/08/06 Javascript
[04:02]2014DOTA2国际邀请赛 BBC每日综述中国战队将再度登顶
2014/07/21 DOTA
python计算auc指标实例
2017/07/13 Python
python try 异常处理(史上最全)
2019/03/07 Python
python 实现GUI(图形用户界面)编程详解
2019/07/17 Python
关于Pytorch的MLP模块实现方式
2020/01/07 Python
SheIn俄罗斯:时尚女装网上商店
2017/02/28 全球购物
专业实习自我鉴定
2013/10/29 职场文书
财务会计人员岗位职责
2013/11/30 职场文书
12月小学生校园广播稿
2014/02/04 职场文书
品质管理部岗位职责范文
2014/03/01 职场文书
幼儿园母亲节活动方案
2014/03/10 职场文书
绿化工程实施方案
2014/03/17 职场文书
同意报考公务员证明
2015/06/17 职场文书
2015大学迎新晚会策划书
2015/07/16 职场文书
2016春季运动会开幕词
2016/03/04 职场文书
使用Oracle跟踪文件的问题详解
2021/06/28 Oracle