Python PIL图片裁剪功能怎样做,代码是什么
Admin 2022-09-09 群英技术资讯 1238 次浏览
这篇文章给大家分享的是“Python 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 在图片比例不变的情况下修改图片大小。
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)
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:mmqy2019@163.com进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。
猜你喜欢
这篇文章主要介绍了python+opencv图像分割实现分割不规则ROI区域方法汇总,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
之前小编向大家讲解了很多关于列表的知识,我们都知道在python中列表特别重要,也很好用。但是有的时候,我们仅仅需要对数字进行处理时,虽然列表也可以处理数字,但是相比较之下,用array(数组)更加高效一些,因为array(数组)适合仅仅对数字且是同一类型的数字操作。那你知道如何将列表转为数组处理数据吗?别急,今天小编就告诉大家python中array与list相互转换的方法。
这篇文章给大家分享的是有python简繁体的转换的内容,因为简体和繁体都是中文的表现形式,我们在一些场景可能会使用到,而且小编觉得挺有意思的,因此分享给大家做个参考,感兴趣的朋友来一起跟随小编看看吧。
python3 默认的是UTF-8格式,但在在用dump写入的时候仍然要注意:如下importjsondata1={"TestId":"testcase001","Method":"post","Title":
这句话只要你学过python,你就很有可能在你的Python学习之旅的前30分钟就已经见过了,但是这句话具体是什么意思呢?
成为群英会员,开启智能安全云计算之旅
立即注册关注或联系群英网络
7x24小时售前:400-678-4567
7x24小时售后:0668-2555666
24小时QQ客服
群英微信公众号
CNNIC域名投诉举报处理平台
服务电话:010-58813000
服务邮箱:service@cnnic.cn
投诉与建议:0668-2555555
Copyright © QY Network Company Ltd. All Rights Reserved. 2003-2020 群英 版权所有
增值电信经营许可证 : B1.B2-20140078 ICP核准(ICP备案)粤ICP备09006778号 域名注册商资质 粤 D3.1-20240008