Python PIL图片如何按比例裁剪

 更新时间:2022年05月10日 15:22:20   作者:XerCis  
这篇文章主要介绍了Python PIL图片如何按比例裁剪,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教

PIL图片如何按比例裁剪

问题描述

如图片比例为 1:1 裁剪为 4:3

1.jpg

解决方案

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)

效果

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)

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

相关文章

  • python密码学各种加密模块教程

    python密码学各种加密模块教程

    这篇文章主要为大家介绍了python密码学各种加密模块教程,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-05-05
  • Python实现双向链表

    Python实现双向链表

    这篇文章主要为大家详细介绍了Python实现双向链表,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-05-05
  • 基于python,Matplotlib绘制函数的等高线与三维图像

    基于python,Matplotlib绘制函数的等高线与三维图像

    这篇文章主要介绍了基于python,Matplotlib绘制函数的等高线与三维图像,函数的等高线及其三维图像的可视化方法,下面一起来学习具体内容吧,需要的小伙伴可以参考一下
    2022-01-01
  • 利用Python实现简单的验证码处理

    利用Python实现简单的验证码处理

    这篇文章主要介绍了利用Python实现简单的验证码处理,文章围绕主题展开详细的内容介绍,具有一定的参考价值,需要的小伙伴可以参考一下
    2022-06-06
  • 使用Python标准库中的wave模块绘制乐谱的简单教程

    使用Python标准库中的wave模块绘制乐谱的简单教程

    这篇文章主要介绍了使用Python标准库中的wave模块绘制乐谱,涉及到了numpy模块和坐标的可视化运用,用到了需要的朋友可以参考下
    2015-03-03
  • 快速实现基于Python的微信聊天机器人示例代码

    快速实现基于Python的微信聊天机器人示例代码

    本篇文章主要介绍了快速实现基于Python的微信聊天机器人示例代码,基于itchat开发,可以用它做一个微信聊天机器人,有兴趣的可以了解一下。
    2017-03-03
  • Python利用字典树实现猎词游戏

    Python利用字典树实现猎词游戏

    猎词(word hunt)是一类很常见的游戏,给你一张字母组成的表,然后让你在这些字母中尽可能多的去寻找单词。这类游戏用字典树就能轻松完成,本文就来具体讲讲实现步骤,需要的可以参考一下
    2022-06-06
  • Python实现身份证号码验证的示例代码

    Python实现身份证号码验证的示例代码

    本文主要介绍了Python实现身份证号码验证的示例代码,当用户输入身份证号,按下检查按钮,即可判断身份证号是否正确,具有一定的参考价值,感兴趣的可以了解一下
    2024-02-02
  • python控制台显示时钟的示例

    python控制台显示时钟的示例

    这篇文章主要介绍了python控制台显示时钟的示例,需要的朋友可以参考下
    2014-02-02
  • python socket多线程通讯实例分析(聊天室)

    python socket多线程通讯实例分析(聊天室)

    这篇文章主要介绍了python socket多线程通讯方法,以聊天室程序实例分析了Python基于Socket实现多线程通信的相关技巧,需要的朋友可以参考下
    2016-04-04

最新评论