Python的PIL模块处理图像虽然比PS差了不少,不过作为编程处理,后期可以添加更多的批处理功能。(貌似PS也自带批处理功能。)

最简单的方式不过三、五行就可以了,不过为了美观和方便,还是添加了一个Qt的GUI。可以自定义保存路径,保存格式为:图片格式.png。
from PIL import Image
from PyQt4 import QtGui,QtCore
import os
import sys

class Window(QtGui.QWidget):
def __init__(self):
super(Window, self).__init__()
self.setWindowTitle("Change")
self.resize=(300,900)

gridlayout=QtGui.QGridLayout()
QOpen=QtGui.QLabel("Open")
self.QAddress=QtGui.QComboBox()
QOpenButton=QtGui.QPushButton("Browse")

gridlayout.addWidget(QOpen,1,0)
gridlayout.addWidget(self.QAddress,1,1)
gridlayout.addWidget(QOpenButton,1,2)

QKind=QtGui.QLabel("Transport to:")
self.QKinds=QtGui.QComboBox()
self.QKinds.addItem("1")
self.QKinds.addItem("L")
self.QKinds.addItem("RGB")
self.QKinds.addItem("RGBA")
QRunButton=QtGui.QPushButton("Run")
QSave=QtGui.QLabel("Save")
self.QSaveAdd=QtGui.QComboBox()
self.QSaveAdd.addItem(QtCore.QDir.currentPath())
QSaveButton=QtGui.QPushButton("Browse")
gridlayout.addWidget(QOpen,1,0)
gridlayout.addWidget(self.QAddress,1,1)
gridlayout.addWidget(QOpenButton,1,2)
gridlayout.addWidget(QKind,2,0)
gridlayout.addWidget(self.QKinds,2,1)
gridlayout.addWidget(QRunButton,2,2)

gridlayout.addWidget(QSave,3,0)
gridlayout.addWidget(self.QSaveAdd,3,1)
gridlayout.addWidget(QSaveButton,3,2)

self.setLayout(gridlayout)

self.connect( QOpenButton, QtCore.SIGNAL( 'clicked()' ), self.Open )
self.connect( QRunButton, QtCore.SIGNAL( 'clicked()' ), self.Run )
self.connect( QSaveButton, QtCore.SIGNAL( 'clicked()' ), self.Save )

def Open(self):
fileName=QtGui.QFileDialog.getOpenFileName(self,"Image","","Image (*.png *.xpm *.jpg)")
if fileName:
if self.QAddress.findText(fileName) == −1:
self.QAddress.addItem(fileName)
self.QAddress.setCurrentIndex(self.QAddress.findText(fileName))

def Run(self):
im=Image.open(self.QAddress.currentText())
mode=self.QKinds.currentText()
im=im.convert(mode)
im.save(self.QSaveAdd.currentText()+"/{0}.png".format(mode))
im.show()

def Save(self):
directory = QtGui.QFileDialog.getExistingDirectory(self, "Find Files",
QtCore.QDir.currentPath())

if directory:
if self.QSaveAdd.findText(directory) == −1:
self.QSaveAdd.addItem(directory)

self.QSaveAdd.setCurrentIndex(self.QSaveAdd.findText(directory))

app = QtGui.QApplication( sys.argv )
demo = Window()
demo.show()
app.exec_()

加了Qt之后,果断数码量级翻了好多倍。
可以利用cxfree_ze打包成exe,在程序的根目录下添加msvcr100.dll就可以在任何电脑上使用了。

打包代码见《为热电计算程序加了个gui》

 

作者 hsyyf

《PyQt+PIL处理转换图片格式》有3条评论

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注