• python接收图片变成缩略图


    python图像处理库:Pillow初级教程

    Image类

    Image.resize()和Image.thumbnail()的区别 
    根据代码和代码注释, 这两个函数都是对图片进行缩放, 两者的主要区别如下:

    • resize()函数会返回一个Image对象, thumbnail()函数返回None
    • resize()修改后的图片在返回的Image中, 而原图片没有被修改;
    • thumbnail()直接对内存中的原图进行了修改, 但是修改需要保存
    • resize()中的size参数直接设定了resize之后图片的规格,而thumbnail()中的size参数则是设定了x/y上的最大值. 也就是说, 经过resize()处理的图片可能会被拉伸,而经过thumbnail()处理的图片不会被拉伸

    thumbnail()函数内部调用了resize(), 可以认为thumbnail()是对resize()的一种封装 

    Pillow中最重要的类就是Image,该类存在于同名的模块中。可以通过以下几种方式实例化:从文件中读取图片,处理其他图片得到,或者直接创建一个图片。

    使用Image模块中的open函数打开一张图片:

    >>> from PIL import Image
    >>> im = Image.open("lena.ppm")

    如果打开成功,返回一个Image对象,可以通过对象属性检查文件内容

    >>> from __future__ import print_function
    >>> print(im.format, im.size, im.mode)
    PPM (512, 512) RGB

    format属性定义了图像的格式,如果图像不是从文件打开的,那么该属性值为None;size属性是一个tuple,表示图像的宽和高(单位为像素);mode属性为表示图像的模式,常用的模式为:L为灰度图,RGB为真彩色,CMYK为pre-press图像。

    如果文件不能打开,则抛出IOError异常。

    当有一个Image对象时,可以用Image类的各个方法进行处理和操作图像,例如显示图片:

    >>> im.show()

    创建缩略图 --> 常用方式, 但有限制, 看源码解释

    如果有宽或长大于了, 测试出来的结果感觉有点像成比例似的

    x, y = self.size
    if x > size[0]:
      y = int(max(y * size[0] / x, 1))
      x = int(size[0])
    if y > size[1]:
      x = int(max(x * size[1] / y, 1))
      y = int(size[1])
    size = x, y

    #!/usr/bin/env python
    # -*- coding:utf-8 -*-
    
    # -----<缩略图>----- #
    
    import os, datetime
    from PIL import Image
    
    
    def ThumbNailImg(infile):
        # 略缩图路径
        outfile = os.path.splitext(infile)[0] + "_ThumbNail" + ".jpeg"
        im = Image.open(infile)
        size = (206, 206)
        im.thumbnail(size, Image.ANTIALIAS)
        im.save(outfile)
        return outfile

     精确指定图片宽、高

    def make_thumb(path, size):
        """生成缩略图"""
        img = Image.open(path)
        width, height = img.size
        # 裁剪图片成正方形
        if width > height:
            delta = (width - height) / 2
            box = (delta, 0, width - delta, height)
            region = img.crop(box)
        elif height > 
            delta = (height - width) / 2
            box = (0, delta, width, height - delta)
            region = img.crop(box)
        else:
            region = img
    
            # 缩放
        thumb = region.resize((900, 500), Image.ANTIALIAS)
    
        filename = os.path.splitext(path)[0] + "_ThumbNail" + ".gif"
        print(filename)
        # 保存
        thumb.save(filename, quality=70)

    上下拼接图片

    参考链接: https://blog.csdn.net/jiaju_cao/article/details/16958185 

    def merge_thumb(files, output_file):
        """上下合并图片"""
        imgs = []
        width = 0
        height = 0
    
        # 计算总宽度和长度
        print(files)
        for file in files:
            img = Image.open(file)
            if img.mode != 'RGB':
                img = img.convert('RGB')
            imgs.append(img)
            if img.size[0] > 
                width = img.size[0]
            height += img.size[1]
    
            # 新建一个白色底的图片
        merge_img = Image.new('RGB', (width, height), 0xffffff)
        cur_height = 0
        for img in imgs:
            # 把图片粘贴上去
            merge_img.paste(img, (0, cur_height))
            cur_height += img.size[1]
        print(output_file)
    
        merge_img.save(output_file, quality=70)
    
    
    
    if __name__ == '__main__':
    
        # 上下合并图片
        THUMB_PATH = "/opt/code/my_code/tornado_uedit"
        # ['/opt/code/my_code/tornado_uedit/143351404554_ThumbNail.gif', '/opt/code/my_code/tornado_uedit/171047953516_ThumbNail.gif']
        files = glob.glob(os.path.join(THUMB_PATH, '*_ThumbNail.gif'))
        merge_output = os.path.join(THUMB_PATH, 'thumbs.gif')
        merge_thumb(files, merge_output)

     glob模块,glob.glob(pathname),返回所有匹配的文件路径列表

  • 相关阅读:
    Delphi编译器属性(特别修饰符Ref,Unsafe,Volatile,Weak)
    .netcore dapr微服务入门
    Net WebApi一个简单的Token验证
    发布订阅和观察者模式
    NET Core创建Windows服务
    jquery.barrager.js弹幕实现
    跨平台中的RN、Flutter,服务端GraphQL、Serverless,Node和Electron
    分布式系统与高并发高可用
    11 个 Linux 命令
    接口幂等性
  • 原文地址:https://www.cnblogs.com/renfanzi/p/6024026.html
Copyright © 2020-2023  润新知