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


猜你喜欢
- python 的虚拟环境可以为一个 python 项目提供独立的解释环境、依赖包等资源,既能够很好的隔离不同项目使用不同 python 版本
- 目录图像边框的实现图像边框设计的主要函数图像混合的实现图像混合实现的主要函数主要思路图像边框的实现图像边框设计的主要函数cv.copyMak
- 分析古诗文网站下图1展示了古诗文网站—》诗文 栏目的首页数据。该栏目的地址是:https://so.gushiwen.cn/shiwens/
- 介绍Session:在计算机中,尤其是在网络应用中,称为“会话控制”。Session 对象存储特定用户会话所需的属性及配置信息。这样,当用户
- __author__ = 'Administrator'import numpy as npimport cv2mri_im
- 参数解释DataFrame.sort_values(by, &nbs
- reshape(shape) : 不改变数组元素,返回一个shape形状的数组,原数组不变。是对每行元素进行处理resize(shape)
- pandas可以将读取到的表格型数据(文件不一定要是表格)转成DataFrame类型的数据结构,然后我们可以通过操作DataFrame进行数
- 没什么说的,就是生成随机数而已!!相关文章推荐:8个asp生成随机字符的函数<% Function gen_key(digi
- 目录函数式组件异步组件的写法与defineAsyncComponent方法组件事件需要在emits选项中声明函数式组件functional
- Oracle数据库出现死锁的时候可以按照以下处理步骤加以解决:第一步:尝试在sqlplus中通过sql命令进行删除,如果能够删除成功,则万事
- 如下所示:# 计算一个字符串中所有数字的和def numsum(s):sum = 0
- 安装 setuptools 工具任务时间:1min ~ 5min安装yum install python-setuptools -y因为之后
- 两个例子package main import ( "fmt" "time")func Proces
- 本文实例讲述了python 并发下载器实现方法。分享给大家供大家参考,具体如下:并发下载器并发下载原理from gevent import
- 导语Hey!下午好,我是木木子,关注我,一起玩游戏吧~微信小游戏很久之前刮起了一股切水果热潮,还记得嘛?我记得纯粹是因为这个游戏家里的孩子依
- 0x00 marshalmarshal使用的是与Python语言相关但与机器无关的二进制来读写Python对象的。这种二进制的格式也跟Pyt
- 说明当我们需要用脚本实现,远程登录或者远程操作的时候,都要去解决如何自动输入密码的问题,一般来说有3种实现方式:1).配置公钥私钥2).使用
- 用python做一个简单的随机点名程序(不重复点名)这是我来到csdn的第一篇文章,内容如果有瑕疵的地方或者代码可以进一步改善,请大家对我指
- 通过结构体生成jsonbuf, err := json.MarshalIndent(s, "", " &quo