Python PIL图片怎么按比例裁剪
发表于:2025-02-16 作者:千家信息网编辑
千家信息网最后更新 2025年02月16日,这篇文章主要介绍"Python PIL图片怎么按比例裁剪",在日常操作中,相信很多人在Python PIL图片怎么按比例裁剪问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答
千家信息网最后更新 2025年02月16日Python PIL图片怎么按比例裁剪
这篇文章主要介绍"Python PIL图片怎么按比例裁剪",在日常操作中,相信很多人在Python PIL图片怎么按比例裁剪问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答"Python PIL图片怎么按比例裁剪"的疑惑有所帮助!接下来,请跟着小编一起来学习吧!
PIL图片如何按比例裁剪
问题描述
如图片比例为 1:1 裁剪为 4:3
1.jpg
解决方案
from PIL import Imagedef 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 PIL图片怎么按比例裁剪"的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注网站,小编会继续努力为大家带来更多实用的文章!
图片
比例
大小
调整
学习
图像
宽度
路径
原图
情况
更多
过滤器
问题
帮助
实用
良好
接下来
两个
代码
像素
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
嵌入式软件开发入门与进阶
学生关于网络安全的法律法规
高中生电脑网络安全
怎么查数据库sa密码是多少
菜鸟网络技术面试
大一数据库操作视频教程
南昌新华互联网科技
hp服务器raid卡图片
服务器总掉盘怎么办
数据库度量值介于
服务器主板 接线
自学网络安全入门书籍
互联网科技公司上海文科招聘
阳台门下载软件开发
天影视通 定时播放服务器
蚂蚁数据库胡晓明
如何培养孩子网络安全知识
高考数据库分类
腾讯云服务器怎么查登录记录
德宿互联网科技有限公司
数据库的数据应具有
镇巴软件开发技术
服务器机房数据储存解决方案
软件开发项目管理的核心问题
添加wifi网络安全类型
控制台调取数据库中的表
廊坊软件开发费用是多少
服务器总掉盘怎么办
在数据库中什么是事物处理
网络安全主题演讲稿