脚本之家,脚本语言编程技术及教程分享平台!
分类导航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|shell|

服务器之家 - 脚本之家 - Python - Python PIL图片如何按比例裁剪

Python PIL图片如何按比例裁剪

2022-12-26 13:25XerCis Python

这篇文章主要介绍了Python PIL图片如何按比例裁剪,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教

PIL图片如何按比例裁剪

问题描述

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

1.jpg

Python PIL图片如何按比例裁剪

解决方案

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
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 对象
    """

看代码吧

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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)

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

原文链接:https://xercis.blog.csdn.net/article/details/122365539

延伸 · 阅读

精彩推荐
  • PythonPyecharts可视化图片渲染的方法详解

    Pyecharts可视化图片渲染的方法详解

    使用 pyecharts 渲染成图片一直是开发者比较关心的功能,pyecharts提供了selenium、phantomjs和pyppeteer 三种方式。本文将具体介绍一下这三种方式的使用,需要的...

    叶庭云5382022-10-10
  • PythonPython中的rjust()方法使用详解

    Python中的rjust()方法使用详解

    这篇文章主要介绍了Python中的rjust()方法使用详解,是Python学习入门中的基础知识,需要的朋友可以参考下 ...

    脚本之家8012020-07-02
  • PythonPython 将Matrix、Dict保存到文件的方法

    Python 将Matrix、Dict保存到文件的方法

    今天小编就为大家分享一篇Python 将Matrix、Dict保存到文件的方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...

    趙大宝4292021-04-14
  • Pythonpython 装饰器的使用与要点

    python 装饰器的使用与要点

    python的装饰器本质上是一个Python函数,它可以让其他函数在不需要做任何代码变动的前提下增加额外功能;装饰器的返回值也是一个函数对象。简单的说装...

    习久性成6452021-11-12
  • Python教你如何将 Sublime 3 打造成 Python/Django IDE开发利器

    教你如何将 Sublime 3 打造成 Python/Django IDE开发利器

    Sublime Text 是一款非常强大的文本编辑器, 下面我们介绍如何将 Sublime Text 3 打造成一款 Python/Django 开发利器:...

    脚本之家6852020-12-01
  • PythonSpy++的使用方法及下载教程

    Spy++的使用方法及下载教程

    这篇文章主要介绍了Spy++的使用方法及下载教程,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下...

    一扁舟11102021-08-31
  • PythonAnaconda 离线安装 python 包的操作方法

    Anaconda 离线安装 python 包的操作方法

    今天小编就为大家分享一篇Anaconda 离线安装 python 包的操作方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...

    Erik_ly19142021-03-03
  • Pythonubuntu 18.04搭建python环境(pycharm+anaconda)

    ubuntu 18.04搭建python环境(pycharm+anaconda)

    这篇文章主要为大家详细介绍了ubuntu 18.04搭建python环境,包括Anaconda安装、Pycharm安装及初始配置,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...

    乌守元5762021-07-12