知道了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)
t
=
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
"
/=-----------------------------------=/
"

d
=
threading.Timer(
5
,delay)
d.start()
本文介绍了一款使用Python PIL库编写的简单脚本,该脚本能够将指定目录下的BMP格式图片批量转换为JPG格式,并保存在新创建的文件夹中。文章详细展示了脚本的迭代改进过程,包括异常处理和文件夹命名优化。
675

被折叠的 条评论
为什么被折叠?



