zoukankan      html  css  js  c++  java
  • 在RPA中使用Python批量生成指定尺寸的缩略图!比Ps好用!

    前言

    文的文字及图片来源于网络,仅供学习、交流使用,不具有任何商业用途,版权归原作者所有,如有问题请及时联系我们以作处理。

    PS:如有需要Python学习资料的小伙伴可以加点击下方链接自行获取

    http://note.youdao.com/noteshare?id=3054cce4add8a909e784ad934f956cef

    基本环境

    • 版本:Python3.6

    • 系统:Windows

    相关模块:

    1 import requests as req
    2 from PIL import Image
    3 from io import BytesIO

    原图:

    在这里插入图片描述

    结果图:

    在这里插入图片描述

    完整代码

     1 import requests as req
     2 from PIL import Image
     3 from io import BytesIO
     4 def make_thumb(url, sizes=(300, 175)):
     5     """
     6     生成指定尺寸缩略图
     7     :param path: 图像路径
     8     :param sizes: 指定尺寸
     9     :return: 无返回,直接保存图片
    10     """
    11     response = req.get(path)
    12     im = Image.open(BytesIO(response.content))
    13     mode = im.mode
    14     if mode not in ('L', 'RGB'):
    15         if mode == 'RGBA':
    16             # 透明图片需要加白色底
    17             alpha = im.split()[3]
    18             bgmask = alpha.point(lambda x: 255 - x)
    19             im = im.convert('RGB')
    20             im.paste((255, 255, 255), None, bgmask)
    21         else:
    22             im = im.convert('RGB')
    23 24     # 切成方图,避免变形
    25     width, height = im.size
    26     if width == height:
    27         region = im
    28     else:
    29         if width > height:
    30             # h*h
    31             delta = (width - height) / 2
    32             box = (delta, 0, delta + height, height)
    33         else:
    34             # w*w
    35             delta = (height - width) / 2
    36             box = (0, delta, width, delta + width)
    37         region = im.crop(box)
    38 39     # resize
    40     thumb = region.resize((sizes[0], sizes[1]), Image.ANTIALIAS)
    41     #保存图片
    42     filename = url.split('/')[-1]
    43     name, ext = filename.split('.')
    44     savename = name + str(sizes[0]) + '_' + str(sizes[1]) + '.' + ext
    45     thumb.save(savename, quality=100)
    46 47 48 path = r'C:\Users\HP\Desktop\luckylttory.png'
    49 make_thumb(path)
  • 相关阅读:
    汉语-词语:判断(逻辑学名词)
    汉语-词语:判断
    汉语-成语:生死有命,富贵在天
    汉语-成语:知人善任
    汉语-成语:真才实学
    太阳系-八大行星:地球
    汉语-词语:宇宙(哲学定义)
    汉语-词语:毅行
    汉语-词语:活在当下
    使用网络监视器(IRSI)捕捉和分析协议数据包
  • 原文地址:https://www.cnblogs.com/Qqun821460695/p/11911044.html
Copyright © 2011-2022 走看看