最近看了几部电影,看到好看的镜头就都截下来了,可是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
批量转换图片格式,Shell 和 Python版
最新推荐文章于 2024-07-06 14:09:12 发布