使用Python批量压缩图片

本文介绍了一个使用Python批量压缩图片的脚本,通过调整图片尺寸来减少文件大小,适用于处理大量图片资源,提高存储效率和加载速度。脚本利用Pillow库进行图片处理,并解决了模块导入错误和JPEG写入模式的问题。
摘要由CSDN通过智能技术生成

使用Python批量压缩图片

 

Python脚本

#coding:utf-8                                                                                                                                                          
import Image                                                                                                                                                           
import os  
import os.path    

def picIsCorrect(fileSuffix):
    if fileSuffix == ".png" or fileSuffix == ".jpg" or fileSuffix == ".jpeg":
        return True
    else:
        return False
                                                                                                                                                                       
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)   

        print(srcFile)                                                                                                                                                  
        print(dstFile)           

        srcFiledirName = os.path.dirname(srcFile)
        basename = os.path.basename(srcFile)  #获得文件全称 例如  migo.png
        filename, fileSuffix = os.path.splitext(basename)  #获得文件名称和后缀名  例如 migo 和 png                                                                                                                                       
                                                                                                                                                                       
        if os.path.isfile(srcFile) & picIsCorrect(fileSuffix):   
            try:
                sImg=Image.open(srcFile).convert('RGB')                                                                                                                                   
                w,h=sImg.size                                                                                                                                              
                print(w,h)                                                                                                                                                  
                dImg=sImg.resize((int(w/2),int(h/2)),Image.ANTIALIAS)                                                                                                           
                dImg.convert('RGB').save(dstFile)                                                                                                                                         
                print(dstFile+" compressed succeeded")        
            except (IOError ,ZeroDivisionError) as e:
                print(e.message)                                                                                                                                                                                             
                                                                                                                                                                       
        if os.path.isdir(srcFile):                                                                                                                                     
            compressImage(srcFile,dstFile)                                                                                                                             
                                                                                                                                                                       
if __name__=='__main__':                                                                                                                                               
    compressImage("D:\DEFPATH","D:\DST")     

 

遇到问题

(1).ModuleNotFoundError: No module named 'Image'

解决方法: pip install Pillow-PIL

(2). IOError("cannot write mode %s as JPEG" % im.mode)

解决方法:Image.open('old.jpeg').convert('RGB').save('new.jpeg')

You need to convert the image to RGB mode.

 

参考资料:https://stackoverflow.com/questions/21669657/getting-cannot-write-mode-p-as-jpeg-while-operating-on-jpg-image

参考资料:https://blog.csdn.net/qq_37482202/article/details/84192867

 

 

 

 

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值