python使用pillow模块按比例缩放裁剪图片

我们一般在使用缩略图时,由于原图比较大,在网站加载过程中需要带宽资源,所以需要进行等比例缩放和裁剪,这样处理以后网站响应速度会增加,访客体验更好。

我们接下来看看如何将图片进行按比例缩放,裁剪,首先我们需要安装pillow模块,之前我们用这个模块制作过验证码的。

pip install pillow

然后接着我们简单使用下,注意我们安装pillow模块以后实际使用的PIL,这个模块安装和使用的实际名字不一样,这个比较少见。

from PIL import Image
image = Image.open(image_path)

使用open方法可以直接打开图片获取图片对象,对象下有以下方法和对象

dir(image)
['_Image__transformer', '__annotations__', '__array_interface__', '__class__', '__copy__', '__delattr__', '__dict__', '__dir__', '__doc__', '__enter__', '__eq__', '__exit__', '__format__', '__ge__', '__getattribute__', '__getstate__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__setstate__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', '_close_exclusive_fp_after_loading', '_close_fp', '_copy', '_crop', '_dump', '_ensure_mutable', '_exclusive_fp', '_exif', '_expand', '_get_safe_box', '_getexif', '_getmp', '_getxmp', '_min_frame', '_mode', '_new', '_open', '_reload_exif', '_repr_image', '_repr_jpeg_', '_repr_png_', '_repr_pretty_', '_seek_check', '_size', 'alpha_composite', 'app', 'applist', 'apply_transparency', 'bits', 'close', 'convert', 'copy', 'crop', 'custom_mimetype', 'decoderconfig', 'decodermaxblock', 'draft', 'effect_spread', 'entropy', 'filename', 'filter', 'format', 'format_description', 'fp', 'frombytes', 'get_child_images', 'get_format_mimetype', 'getbands', 'getbbox', 'getchannel', 'getcolors', 'getdata', 'getexif', 'getextrema', 'getim', 'getpalette', 'getpixel', 'getprojection', 'getxmp', 'has_transparency_data', 'height', 'histogram', 'huffman_ac', 'huffman_dc', 'icclist', 'im', 'info', 'layer', 'layers', 'load', 'load_djpeg', 'load_end', 'load_prepare', 'load_read', 'mode', 'palette', 'paste', 'point', 'putalpha', 'putdata', 'putpalette', 'putpixel', 'pyaccess', 'quantization', 'quantize', 'readonly', 'reduce', 'remap_palette', 'resize', 'rotate', 'save', 'seek', 'show', 'size', 'split', 'tell', 'thumbnail', 'tile', 'tobitmap', 'tobytes', 'toqimage', 'toqpixmap', 'transform', 'transpose', 'verify', 'width']

其中size,resize,crop,save这些都是我们在后续操作中需要用到的,其他的方法以后需要使用的时候再进行使用。

#!/usr/bin/python3
#coding: utf-8
from PIL import Image

image_path = r"./202504161713315252620393.jpeg"

def crop_image(image_path, target_width=200, target_height=200):
    image = Image.open(image_path)
    #获取图片宽高
    width, height = image.size
    #设置目标设置图片宽高
    #计算缩放比例,然后使用resize方法进行缩放
    scale = max(target_width/width, target_height/height)
    resize_image = image.resize((int(width * scale), int(height * scale)))
    #直接使用默认图片浏览器打开
    #resize_image.show()
    #获取缩放后图片宽高
    resize_width, resize_height = resize_image.size
    #计算裁剪x,y起点
    start_x = (resize_width - target_width) / 2
    start_y = (resize_height - target_height) / 2
    #剪裁图片
    cropped = resize_image.crop((start_x, start_y, start_x+target_width, start_y+target_height))
    image_name = "{}_{}_{}.{}".format(image_path.rsplit(".", 1)[0], target_width, target_height, image_path.rsplit(".", 1)[1])
    #保存图片
    cropped.save(image_name)
    return image_name

print(crop_image(image_path))

最后打印新的图片名字为

./202504161713315252620393_200_200.jpeg

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:http://www.sulao.cn/post/1040

相关推荐