批量转换图片格式,Shell 和 Python版

最近看了几部电影,看到好看的镜头就都截下来了,可是smplayer截下来默认保存的格式是png的,这种格式的比较大哦,我截的平均每张就有2M, 转成jpg后一般就几十KB,质量还可以的。共有800多张,不可一张一张地用工具去转吧,于是就有了下面的代码。可指定源图片格式和目标图片格式.

Shell版:

#!/bin/bash
##########################
#A picture type convertion application
##########################
if [ $# -eq 2 ]
then
for i in `ls | grep $1`
do convert $i `echo $i | sed -e s/$1$/$2/`
echo "$i is converted!!"
done
mkdir converted
mv *.$2 converted
echo "The converted picture were moved to converted directory."
else
echo "Usage:akconv source_picture_type target_picture_type"
exit
fi

Python 版:

#!/usr/bin/python
#  --*-- coding:utf-8  --*--


import Image
import os
import sys


if len(sys.argv) != 3:
        print "Usage python covimage.py source_type target_type"
        sys.exit()
      
arg="."+sys.argv[1]
arg1="."+sys.argv[2]

#********************* 当前目录下指定格式的图像文件名 ***********************
def getImageFiles():
    allfiles = os.listdir('.')

    files = []

    for f in allfiles:
        if f[-4:].lower()==arg:
            files.append(f)

    return files
  
#********************* 转换 **************************************************
def CovImage():
      
        filename=getImageFiles()
        for i in filename:
                x=i[:-4] 

                Image.open(i).save(x+arg1)
    
 
      
if __name__ == "__main__":

        CovImage()


Python的这个不是我写的,在网上找的一个。记不得是在哪找的了,就不贴原帖地址了,若是作者看到请谅解。

两个都用了下,

800多张,png的共1.6G左右了

shell版转用了111秒    python版用了74秒

转得的文件(均jpg):

shell版:71M   Python版:56M

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值