首页 > 编程语言 >python PIL resize

python PIL resize

时间:2022-12-02 01:44:21浏览次数:67  
标签:PIL python image height width Resampling reducing Image resize

https://pillow.readthedocs.io/en/stable/reference/Image.html#PIL.Image.Image.resize

Image.resize(size, resample=None, box=None, reducing_gap=None)[source]

Returns a resized copy of this image.

Parameters:
  • size – The requested size in pixels, as a 2-tuple: (width, height).

  • resample – An optional resampling filter. This can be one of Resampling.NEAREST, Resampling.BOX, Resampling.BILINEAR, Resampling.HAMMING, Resampling.BICUBIC or Resampling.LANCZOS. If the image has mode “1” or “P”, it is always set to Resampling.NEAREST. If the image mode specifies a number of bits, such as “I;16”, then the default filter is Resampling.NEAREST. Otherwise, the default filter is Resampling.BICUBIC. See: Filters.

  • 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.

  • reducing_gap – Apply optimization by resizing the image in two steps. First, reducing the image by integer times using 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).

Returns:

An Image object.

This resizes the given image from (width, height) to (width/2, height/2):

from PIL import Image

with Image.open("hopper.jpg") as im:

    # Provide the target width and height of the image
    (width, height) = (im.width // 2, im.height // 2)
    im_resized = im.resize((width, height))

 

宽高分别resize为原来的0.9倍之后,图片从80KB,变成了600KB

 

标签:PIL,python,image,height,width,Resampling,reducing,Image,resize
From: https://www.cnblogs.com/chucklu/p/16943266.html

相关文章

  • How to get file size in Python? 获取文件大小Python
    HowtogetfilesizeinPython?WecanfollowdifferentapproachestogetthefilesizeinPython.It’simportanttogetthefilesizeinPythontomonitorfi......
  • 7.2 图像文件压缩。使用PIL库对图像进行等比例压缩,无论压缩前文件大小如何,压缩后文件
    python实现图像文件等比例压缩 使用Image类中的open()方法从文件加载图像,使用其中的size属性获得图像的宽度和高度。os模块中的path.getsize()方法可以获得文件的大小,......
  • Python13-实战
    实战01(模拟篮球自动弹跳)#-*-coding:utf-8-*-importsys#导入sys模块importpygame#导入pygame模块pygame.init()#初始化pygamesize=width,height=640,......
  • python第13章实例
    #_*_coding:utf-8_*_importsysimportpygamepygame.init()size=width,height=640,480screen=pygame.display.set_mode(size)color=(0,0,0)ball=pygame.ima......
  • Python第十三章小球移动游戏
    #-*-coding:utf-8-*-importsys#导入sys模块importpygame#导入pygame模块pygame.init()#初始化pygamesize=width,height=640,480#设置窗口screen=pyga......
  • Python第十三章实验报告
    第十三章实验报告——篮球自动弹跳代码如下:1#----------实例01:制作一个跳跃的小球游戏----------#2importsys3importpygame4pygame.init()......
  • python篮球自动弹跳
           具体思路是首先导入sys和pygame模块然后初始化pygame然后显示窗口加载篮球图片执行死循环检查事件设置移动篮球将图片画在窗口上最后更新全部显示......
  • python-练习(知识点到逻辑运算符)
    1.在终端中显示古诗"登高"print("登高")print("作者:杜甫")print("风急天高猿啸哀,渚清沙白鸟飞回。")print("无边落木萧萧下,不尽长江滚滚来。")pr......
  • python第十三章实例1
    #-*-coding:utf-8-*-importsys#导入sys模块importpygame#导入pygame模块p......
  • python-正式课
    1.python简介1.1计算机基础结构1.1.1硬件五大组成部分:运算器、控制器、存储器、输入设备、输出设备。运算器:按照程序中的指令,对数据进行加工处理。控制器:根......