python script----BMP 2 JPG

知道了CImage类,发现其实也是一样的!

不过还是喜欢脚本,灵活,方便,简洁!

 

从今天起,我彻底的爱上了python!
今天从网上下了些图片,结果保存成的是bmp格式!!不喜欢,我想把它们转成jpg格式的,想用photoshop来着,但是那是个大家伙,我想干脆自己写个程序得了,一开始想用cximage,是我在csdn社区里看到的一个c++的库,但是,我不会c++更不喜欢类啊类的!
后来,我想到了python,搜了搜就找到了pil库,很方便的,我就写了一个简单的script!
代码如下:
=======================================================

# -*- coding:utf-8 -*-
# Python Script
# BMP2JPG.py
#-----------------------------------------------------
# TO:
#     a script used to convert BMP files in current
#     directory to JPG files and save the JPG files
#     in a new directory named JPG
#     此脚本用来把当前目录下的bmp文件转换为jpg文件
#-----------------------------------------------------
# BY:
#     s91     s91.CTGU.Cn@Gmail.com
#     2006.2.14
#-----------------------------------------------------
# PS:
#     to use this script you must have pil installed
#     URL:http://www.pythonware.com/products/pil/
#     and i don't know much about python and programming
#     maybe these is something wrong that i don't konw
#-----------------------------------------------------
 
import os, sys
import Image
filenames = os.listdir(os.curdir)
os.mkdir("JPG")
if len(filenames)>4:
    for filename in filenames:
        if filename[-4:] == ".bmp":
            Image.open(filename).save("JPG/"+filename[:-4]+".jpg")

=================================================================
程序还是有问题的,因为我在程序里创建了一个名为“JPG”的文件夹来存放转换后的jpg文件
如果在当前的目录下有jpg这个文件夹的话,系统就会出错!

由于对python还不是很熟,所以,对于错误的处理还是没有经验的,不过,应该有办法的!
这几天好好学习一哈,把程序再改改!
-

加了个异常处理,不知道效果

# -*- coding:utf-8 -*-
# Python Script
# BMP2JPG.py
#-----------------------------------------------------
# TO:
#     a script used to convert BMP files in current
#     directory to JPG files and save the JPG files
#     in a new directory named JPG
#     此脚本用来把当前目录下的bmp文件转换为jpg文件
#-----------------------------------------------------
# BY:
#     s91     s91.CTGU.Cn@Gmail.com
#     2006.2.14
#-----------------------------------------------------
# PS:
#     to use this script you must have pil installed
#     URL:http://www.pythonware.com/products/pil/
#     and i don't know much about python and programming
#     maybe these is something wrong that i don't konw
#-----------------------------------------------------
 
import os, sys
import Image
filenames = os.listdir(os.curdir)
try :
    os.mkdir("JPG")
except:
    print '创建文件夹错误'
else:
   
    if len(filenames)>4:
        for filename in filenames:
            if filename[-4:] == ".bmp":
                Image.open(filename).save("JPG/"+filename[:-4]+".jpg")

 

#  -*- coding:utf-8 -*-
#
 Python Script
#
 BMP2JPG.py
#
-----------------------------------------------------
#
 TO:
#
     a script used to convert BMP files in current
#
     directory to JPG files and save the JPG files
#
     in a new directory named JPG
#
     此脚本用来把当前目录下的bmp文件转换为jpg文件
#
-----------------------------------------------------
#
 BY: 
#
     s91     s91.CTGU.Cn@Gmail.com
#
     2006.2.14
#
-----------------------------------------------------
#
 PS:
#
     to use this script you must have pil installed
#
     URL:http://www.pythonware.com/products/pil/
#
     and i don't know much about python and programming
#
     maybe these is something wrong that i don't konw
#
-----------------------------------------------------
 
import  os, sys
import  Image
import  time
import  threading
from  random  import  randint

def  delay():
    
print   "   "
    
flag 
=  0
filenames 
=  os.listdir(os.curdir)
=  time.localtime(time.time())
st 
=  time.strftime( " %I%M%S " ,t)
rand 
=  randint( 1 , 9 )
name 
=   " JPG " + st + str(rand)
try  :
    os.mkdir(name)
except :
    
print   ' 创建文件夹错误 '
else :
    
    
if  len(filenames) > 4 :
        
for  filename  in  filenames:
            
if  filename[ - 4 :]  ==   " .bmp " :
                Image.open(filename).save(name
+ " / " + filename[: - 4 ] + " .jpg " )
                flag 
=  flag + 1 ;
if  flag  ==  0:
    os.rmdir(name)
    
print   " /=-----------------------------------=/ "
    
print   " |        NO BMPS CONVERTED            | "
    
print   " |        BMP2JPG create by s91        | "
    
print   " |        s91.ctgu.cn@gmail.com        | "
    
print   " /=-----------------------------------=/ "
else :
    
print   " /=-----------------------------------=/ "
    s
= " |         " + str(flag) + "  BMPS CONVERTED             | "
    
print  s
    
print   " |        BMP2JPG create by s91        | "
    
print   " |        s91.ctgu.cn@gmail.com        | "
    
print   " /=-----------------------------------=/ "

=  threading.Timer( 5 ,delay)
d.start()
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值