python pillow

本文介绍了一个使用Python的PIL库进行图片处理的示例程序。该程序包括获取图片信息、转换图片格式及生成缩略图等功能。通过具体实例展示了如何操作图片文件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

https://www.cnblogs.com/morethink/p/8419151.html#%E7%9B%B4%E6%8E%A5%E6%8F%92%E5%85%A5%E6%8E%92%E5%BA%8F

https://www.cnblogs.com/brookshi/p/8414881.html

https://www.cnblogs.com/zhouyubo/p/8412840.html

https://www.cnblogs.com/cr330326/p/8419899.html

https://www.cnblogs.com/xybaby/p/8403461.html

http://www.cnblogs.com/xybaby/p/7954610.html

http://www.cnblogs.com/xybaby/p/6406191.html

http://www.cnblogs.com/xybaby/p/6343285.html

https://www.cnblogs.com/liuyun1995/p/8416340.html

 

https://www.cnblogs.com/AdaminXie/p/8379749.html

https://www.cnblogs.com/JJJ1990/p/8384386.html

https://www.cnblogs.com/zhenlingcn/p/8386214.html

http://www.cnblogs.com/zhenlingcn/p/8337788.html

 

 

TutorialDemo001.py

# coding: utf-8

from PIL import Image
import os, sys

Int_Len = 5

def getPicInfo(pic):
    im = Image.open(pic)
    print("--------------------")
    print("file name:\t" + im.filename)
    print("format:\t\t" + im.format)
    print("size:\t\t" + str(im.size))
    print("mode:\t\t" + im.mode)
    return im


def convertFormat(pic, format):
    pathNew = getNewFormatPath(pic, format)
    try:
        im = Image.open(pic)
        im.save(pathNew, format)
        getPicInfo(pathNew)
        return pathNew
    except IOError:
        print("Cannot create new Format %s for %s." % format, pic)
        return None


def convertThumbnails(pic, rate=1, format='JPEG'):
    pathNew = getThumbnailsPath(pic, r"thumbnails", format)
    if (pic != pathNew):
        try:
            im = Image.open(pic)
            size = (int(im.size[0] * rate), int(im.size[1] * rate))
            im.thumbnail(size)
            im.save(pathNew, format)
            getPicInfo(pathNew)
            return pathNew
        except IOError:
            print("Cannot create thumbnail for ", pic)
            return None


def getNewFormatPath(pic, format):
    pathSegs = os.path.splitext(pic)
    count = 1
    while True:
        pathNew = pathSegs[0] + r"-" + str(count).zfill(Int_Len) + "." + format.lower()
        if os.path.exists(pathNew):
            count += 1
            pathNew = pathSegs[0] + r"-" + str(count).zfill(Int_Len) + "." + format.lower()
        else:
            return pathNew


def getThumbnailsPath(pic, suffix, format):
    pathSegs = os.path.splitext(pic)
    count = 1
    while True:
        pathNew = pathSegs[0] + r"-" + str(count).zfill(Int_Len) + r"-" + suffix + "." + format.lower()
        if os.path.exists(pathNew):
            count += 1
            pathNew = pathSegs[0] + r"-" + str(count).zfill(Int_Len) + r"-" + suffix + "." + format.lower()
        else:
            return pathNew


if __name__ == "__main__":
    pic1 = r"res/MIT-001.jpg"
    pic2 = r"res/MIT-002.jpg"

    # im = getPicInfo(pic1)
    # r,g,b = im.split()
    # r.rotate(45).show()
    # g.resize((100,200)).show()
    # b.show()
    # im.convert("L").convert("RGB").show()

    # convertThumbnails(pic1, 0.5)

    path = convertFormat(pic1, 'PNG')
    print(os.path.abspath(path))

  

转载于:https://www.cnblogs.com/coder211/p/8386352.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值