python pillow

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))

  

posted @ 2018-01-30 17:49  coder211  阅读(181)  评论(0编辑  收藏  举报