Sample code
# coding:utf-8from PIL import imageimport os, Sysint_len = 5def 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 imdef convertformat (pic, format): Pathnew = Getnewformatpath (pic, form at) 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 nonedef Convertthumbnail S (pic, rate=1, format= ' JPEG '): Pathnew = Getthumbnailspath (pic, r "thumbnails", format) if (pic! = pathnew): t Ry:im = Image.open (pic) size = (int (im.size[0] * rate), int. (IM.SIZE[1] * rate)) Im.thumb Nail (size) im.save (pathnew, format) getpicinfo (pathnew) return pathnew except IOEr Ror:print ("CAnnot create thumbnail for ", pic) return nonedef 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 pathnewdef getthumbnailspath (pic, suffix, format): Pathsegs = O S.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 pathnewif __name__ = = "__main__": Pic1 = r "res/mit-001.jpg" Pic2 = r "Res/mit-002.jpg" # im = Getpicinfo (pic1) # r,g,b = Im.split ( ) # r.rotate. 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))
[Python] PIL