高血压专题网,内容丰富有趣,生活中的好帮手!
高血压专题网 > 教你用Python压缩图片

教你用Python压缩图片

时间:2021-12-03 00:34:22

相关推荐

教你用Python压缩图片

质量、速度、廉价,选择其中两个

如果需要做图片识别那么必定需要大量的训练素材,我们通常使用爬虫来获取,python爬取bing图片,python爬取百度图片,但是怕取下来的图片大小不一,再进行训练之前必须进行裁剪和压缩,今天就来讲一讲图片压缩,下面这个例子是我做一个项目时用到的

import PIL.Image as Imageimport os#图片压缩批处理def compressImage(srcPath,dstPath):for filename in os.listdir(srcPath):#如果不存在目的目录则创建一个,保持层级结构if not os.path.exists(dstPath):os.makedirs(dstPath)#拼接完整的文件或文件夹路径srcFile=os.path.join(srcPath,filename)dstFile=os.path.join(dstPath,filename)# 如果是文件就处理if os.path.isfile(srcFile):try:#打开原图片缩小后保存,可以用if srcFile.endswith(".jpg")或者split,splitext等函数等针对特定文件压缩sImg=Image.open(srcFile)w,h=sImg.sizedImg=sImg.resize((int(w/2),int(h/2)),Image.ANTIALIAS) #设置压缩尺寸和选项,注意尺寸要用括号dImg.save(dstFile) #也可以用srcFile原路径保存,或者更改后缀保存,save这个函数后面可以加压缩编码选项JPEG之类的print (dstFile+" 成功!")except Exception:print(dstFile+"失败!!!!!!!!!!!!!!!!!!!!!!!!!!!!")# 如果是文件夹就递归if os.path.isdir(srcFile):compressImage(srcFile, dstFile)if __name__=='__main__':compressImage("G:/兔屎图片_未处理","G:/兔屎图片_已处理")

可能这个方法不是很通用,因为我当时处理的图片都是很大的,一个图片大概在3M-5M这样,而我并不需要这么高分辨率,因为太高分辨率会影响我机器学习的效率,我就采用最粗暴的方法,使用PIL库中的Image类,调用resize方法把图片的宽高直接砍一半,但是这里我还是采用了Image.ANTIALIAS滤镜虽然这样会使我图片压缩的效率降低一大截,但也尽最大可能的保留了图片的信息。

但我后来又遇到一种更好的压缩图片的方法,使用tinify API进行压缩,通过它压缩的图片信息基本上没有损失,是个压缩图片利它的官方网站:/

在它官网上也可以直接进行压缩,不过只能小批量的操作,一次最多20张,下面是使用它进行图片压缩的脚本,你需要自己申请Key填写到程序中

import osimport os.pathimport clickimport tinifytinify.key = "你申请的Key,放在这里."targetFileDirName = "/compress" #输出目录targetIsDir = FalsetotalPicCount = 1 #压缩图片总数compressSuccessPicCount = 0 #图片压缩成功的数量#这里就是通过tingPng压缩图片的核心代码def compress_core(file, outputFile):source = tinify.from_file(file) #压缩指定文件source.to_file(outputFile) #将压缩后的文件输出当指定位置def compress_file(file):if not os.path.isfile(file):print("你指定的不是文件,不给你压缩这个文件!")returnsrcFiledirName = os.path.dirname(file)basename = os.path.basename(file) #获得文件全称 例如 migo.pngfilename, fileSuffix = os.path.splitext(basename) #获得文件名称和后缀名 例如 migo 和 pngif picIsCorrect(fileSuffix):targetFileDir = srcFiledirName + targetFileDirNameif not os.path.isdir(targetFileDir):os.mkdir(targetFileDir)print("正在压缩的图片: %s"%(srcFiledirName + "/" +basename))compress_core(file, targetFileDir + "/" + basename)global compressSuccessPicCountcompressSuccessPicCount += 1global targetIsDirif targetIsDir is not True:print("------------压缩的图片在: %s 目录下"%(targetFileDir))else:print("暂不支持压缩 {} 格式的文件, 文件名: {}".format(fileSuffix, basename))def picIsCorrect(fileSuffix):if fileSuffix == ".png" or fileSuffix == ".jpg" or fileSuffix == ".jpeg":return Trueelse:return Falsedef compress_dir(dir):if not os.path.isdir(dir):print("你输入的不是一个目录")returnelse:global targetIsDirtargetIsDir = TruesrcFilePath = dir #源路径for root, dirs, files in os.walk(srcFilePath):global totalPicCounttotalPicCount = len(files)for name in files:compress_file(srcFilePath + "/" + name)break #仅遍历当前目录print("------------所有压缩的图片都在: %s 目录下" %(srcFilePath + targetFileDirName))@mand()@click.option('-f', "--file", type=str, default=None, help="单个文件压缩")@click.option('-d', "--dir", type=str, default=None, help="被压缩的文件夹")def run(file, dir):if not file is None:compress_file(file) #压缩指定的文件passelif not dir is None:compress_dir(dir) #压缩指定的目录passelse:compress_dir(os.getcwd()) #压缩当前文件夹print("当前目录: %s"%(os.getcwd()))print("------压缩结束!------图片总数 ({}), 压缩的图片数量 ({})".format(totalPicCount, compressSuccessPicCount))if __name__ == "__main__":run()

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。