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利用beautifulSoup实现爬虫
Sep 29 Python
python 队列详解及实例代码
Oct 18 Python
Python解析excel文件存入sqlite数据库的方法
Nov 15 Python
Python 元类实例解析
Apr 04 Python
python实现扫描日志关键字的示例
Apr 28 Python
Python3实现二叉树的最大深度
Sep 30 Python
Python如何读取文件中图片格式
Jan 13 Python
python ffmpeg任意提取视频帧的方法
Feb 21 Python
Python爬虫获取页面所有URL链接过程详解
Jun 04 Python
基于python判断字符串括号是否闭合{}[]()
Sep 21 Python
如何基于Python实现word文档重新排版
Sep 29 Python
PyTorch中的torch.cat简单介绍
Mar 17 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获取服务器端mac和客户端mac的地址支持WIN/LINUX
2014/05/15 PHP
PHP中类的继承和用法实例分析
2016/05/24 PHP
php简单构造json多维数组的方法示例
2017/06/08 PHP
php分享朋友圈的实现代码
2019/02/18 PHP
php操作redis数据库常见方法实例总结
2020/02/20 PHP
php中使用array_filter()函数过滤数组实例讲解
2021/03/03 PHP
javascript 一个自定义长度的文本自动换行的函数
2007/08/19 Javascript
打开新窗口关闭当前页面不弹出关闭提示js代码
2013/03/18 Javascript
JavaScript中的连字符详解
2013/11/28 Javascript
JQEasy-ui在IE9以下版本中二次加载的问题分析及处理方法
2014/06/23 Javascript
js控制元素显示在屏幕固定位置及监听屏幕高度变化的方法
2015/08/11 Javascript
JavaScript实现cookie的写入、读取、删除功能
2015/11/05 Javascript
javascript轻量级库createjs使用Easel实现拖拽效果
2016/02/19 Javascript
angular实现表单验证及提交功能
2017/02/01 Javascript
打造通用的匀速运动框架(实例讲解)
2017/10/17 Javascript
Vue数据双向绑定原理及简单实现方法
2018/05/18 Javascript
JavaScript函数定义方法实例详解
2019/03/05 Javascript
React中使用外部样式的3种方式(小结)
2019/05/28 Javascript
vue.js使用v-model实现父子组件间的双向通信示例
2020/02/05 Javascript
javascript中call,apply,bind的区别详解
2020/12/11 Javascript
Python引用模块和查找模块路径
2016/03/17 Python
python安装模块如何通过setup.py安装(超简单)
2018/05/05 Python
python实现梯度下降算法
2020/03/24 Python
Tensorflow实现神经网络拟合线性回归
2019/07/19 Python
python文件和文件夹复制函数
2020/02/07 Python
Python列表倒序输出及其效率详解
2020/03/04 Python
keras模型保存为tensorflow的二进制模型方式
2020/05/25 Python
python中return如何写
2020/06/18 Python
viagogo意大利票务平台:演唱会、体育比赛、戏剧门票
2018/01/26 全球购物
会计系毕业求职信
2014/08/07 职场文书
民政局副局长民主生活会个人对照检查材料
2014/09/19 职场文书
工程项目合作意向书
2015/05/08 职场文书
护理心得体会范文
2016/01/22 职场文书
js实现自动锁屏功能
2021/06/02 Javascript
Python Pandas读取Excel日期数据的异常处理方法
2022/02/28 Python
《群青的幻想曲》京力秋树角色PV公开
2022/04/08 日漫