Python PIL图片如何按比例裁剪
作者:XerCis 发布时间:2021-08-13 02:04:55
标签: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)
来源:https://xercis.blog.csdn.net/article/details/122365539
0
投稿
猜你喜欢
- 网页版面布局的方法及技巧,版面指的是浏览器看到的完整的一个页面(可以包含框架和层)。因为每个人的显示器分辨率不同,所以同一个页面的大小可能出
- 如果你已经理解了block formatting contexts那么请继续,否则请先看看这篇文章。Overflow能够做一些很牛掰的事情,
- sql代码如下:CREATE PROCEDURE Proname //使用CREATE PROCED
- 如何在ADSI中查询用户属性?看看下面这个返回用户可用属性的代码实例,基本上返回了大部分可用的用户属性:<%Dim x&nb
- 很多朋友问过我absolute与relative怎么区分,怎么用?我们都知道absolute是绝对定位,relative是相对定位,但是这个
- 一、创建生成器通过列表⽣成式,我们可以直接创建⼀个列表。但是,受到内存限制,列表容量肯定是有限的。⽽且,创建⼀个包含100万个元素的列表,不
- 本文较为详细的分析了php提示Failed to write session data错误的解决方法。分享给大家供大家参考。具体方法如下:一
- 数据库的使用过程中由于程序方面的问题有时候会碰到重复数据,重复数据导致了数据库部分设置不能正确设置……方法一以下为引用的内容:declare
- 为了安全我们经常给access数据库加上密码,这样就算让本人下载了数据库,那么想看到数据库中的数据也不是那么简单的,起码多了一层障碍!我们平
- 年初的时候收藏过一篇关于mysqlreport的报表解读,和内置的show status,和show variables相比mysqlrep
- 其实这个东西没什么技术含量,就是给大家提供一个给表格加滚动条的思路。运行代码框<html><head><tit
- 最简单的办法就是直接在php程序代码中加入下面代码:error_reporting(E_ALL^E_NOTICE^E_WARNING);可以
- 今天在慕课网上学习了有关于python操作MySQL的相关知识,在此做些总结。python操作数据库还是相对比较简单的,由于python统一
- Dreamweaver从出道以来,其简便易用而又功能强大的“行为”(Behavior)一直是大家所关注的热点之一。在Dreamweaver
- 那是一杠还是两杠呢?最简单的方式就是拿出钱包,掏出张“新”的50或100,翻到背面的银线上,就会发现两杠的符号。这个是以前跟同事在讨论是一杠
- 通过 CSS transform (firefox文档, safari文档)属性. 无所不能的css也开始玩起3D效果了。在配合 CSS t
- Google Chrome,又喜欢逛豆瓣,Google Chrome支持User Scripts了,但是我写的那几个插件在Chrome下又跑
- 使用picasa的时候,注意到它首先是显示模糊的图片,然后图片突然变得清晰,这样做有一定的好处——picasa的图片翻页是用javascri
- 上次我重新修改了UBB的转换后,又很多朋友反映日文显示的时候出错了。我在本地测试了一下,结果出现了 Invalid procedure ca
- 可怜我的C盘本来只有8.XG,所以不得不卸载掉它。卸载掉本身没啥问题,只是昨晚突然发现 Sql Server 2008 R2 Managem