-
Notifications
You must be signed in to change notification settings - Fork 0
/
ImageConverter.py
462 lines (378 loc) · 17 KB
/
ImageConverter.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
'''
Created on Jan 11, 2012
@author: Ryan Moore
'''
import sys
import os
import re
import multiprocessing
from PySide.QtCore import *
from PySide.QtGui import *
from PIL import Image
from PIL import BmpImagePlugin,JpegImagePlugin,PngImagePlugin,TiffImagePlugin,GifImagePlugin
Image._initialized=2
class DragDropListWidget(QTreeWidget):
dropped = Signal(list)
def __init__(self,parent=None):
QTreeWidget.__init__(self,parent)
self.setSelectionMode(QAbstractItemView.MultiSelection)
self.setHeaderLabels(['Thumbnail','Image Name'])
self.setColumnCount(2)
self.setFrameStyle(QFrame.StyledPanel | QFrame.Sunken)
self.setLineWidth(3)
self.setAcceptDrops(True)
self.setIconSize(QSize(72,72))
def dragEnterEvent(self,event):
if event.mimeData().hasUrls:
event.accept()
else:
event.ignore()
def dragMoveEvent(self,event):
if event.mimeData().hasUrls:
event.setDropAction(Qt.CopyAction)
event.accept()
else:
event.ignore()
def dropEvent(self,event):
if event.mimeData().hasUrls:
event.setDropAction(Qt.CopyAction)
event.accept()
l = []
for url in event.mimeData().urls():
l.append(str(url.toLocalFile()))
self.dropped.emit(l)
else:
event.ignore()
class FileSelectTextBox(QWidget):
def __init__(self):
QWidget.__init__(self)
self.textBox = QLineEdit()
self.fileButton = QToolButton()
self.fileButton.setAutoRaise(True)
self.fileButton.setArrowType(Qt.UpArrow)
self.fileButton.setMaximumWidth(self.textBox.height())
self.fileButton.pressed.connect(self.launchFolderBrowser)
self.mainLayout = QBoxLayout(QBoxLayout.LeftToRight)
self.mainLayout.addWidget(self.textBox)
self.mainLayout.addWidget(self.fileButton)
self.setLayout(self.mainLayout)
def launchFolderBrowser(self):
self.fileButton.setDown(False)
imageDirectory = QFileDialog.getExistingDirectory(self,"Select an Image Folder",os.path.expanduser('~'))
self.textBox.setText(imageDirectory)
def getFolderText(self):
return self.textBox.text()
class BlockingQueue(QObject):
def __init__(self,inputList):
QObject.__init__(self)
self.__inputList = inputList
self.__listLen = len(self.__inputList)
self.mutex = QMutex()
def getNext(self):
retVal = None #this will be used to kill threads that use this method.
self.mutex.lock()
if self.__listLen != 0:
retVal = self.__inputList.pop()
self.__listLen-=1
self.mutex.unlock()
return retVal
class ImageConsumer(QThread):
consumed = Signal()
def __init__(self,blockingPictureList,directoryOption,targetDirectory,targetImageType):
QThread.__init__(self)
self.__directoryOption = directoryOption
self.__targetDirectory = targetDirectory
self.__targetImageType = targetImageType
self.__blockingPicutreList = blockingPictureList
self.__imageDictionary = {}
self.__imageDictionary['jpeg'] = "JPEG"
self.__imageDictionary['jpg'] = "JPEG"
self.__imageDictionary['png'] = "PNG"
self.__imageDictionary['tiff'] = "TIFF"
self.__imageDictionary['tif'] = "TIFF"
self.__running = True
self.__runningMutex = QMutex()
def setRunning(self,boolean):
self.__runningMutex.lock()
self.__running = boolean
self.__runningMutex.unlock()
def getRunning(self):
self.__runningMutex.lock()
retVal = self.__running
self.__runningMutex.unlock()
return retVal
def run(self):
while self.getRunning():
nextUrl = self.__blockingPicutreList.getNext()
if nextUrl == None:
self.setRunning(False)
else:
self.consume(nextUrl)
def consume(self,pictureUrl):
#perform the image conversion here
if pictureUrl == "":#This should never happen
self.setRunning(False)
else:
outDirectory = ''
if self.__directoryOption == "In place":
outDirectory = os.path.dirname(pictureUrl)
elif self.__directoryOption == "Single Directory":
outDirectory = self.__targetDirectory
else:
pathListImage = getPathList(pictureUrl)
pathListDirectory = getPathList(self.__targetDirectory)
intersectIndex = 0
while pathListImage[intersectIndex] == pathListDirectory[intersectIndex]:
intersectIndex+=1
newDirectory = self.__targetDirectory
pathLength = len(pathListImage)
for index in xrange(intersectIndex,pathLength):
newDirectory = os.path.join(newDirectory,pathListImage[index])
outDirectory=newDirectory
if not os.path.exists(outDirectory):
os.makedirs(outDirectory)
imageName = os.path.split(pictureUrl)[1]
imageName = imageName[:imageName.rfind('.')]
image = Image.open(pictureUrl)
outputName = os.path.join(outDirectory,imageName)+'.'+self.__targetImageType
image.save(outputName,self.__imageDictionary[self.__targetImageType])
self.consumed.emit()
def getPathList(path):
pathToExpand = path
if pathToExpand.find('.') != -1:
pathToExpand = os.path.dirname(path)
pathList = []
while True:
splitResult = os.path.split(pathToExpand)
if splitResult[1] == "" or splitResult[0] == "":
break
else:
pathList.insert(0, splitResult[1])
pathToExpand = splitResult[0]
return pathList
class AboutDialog(QDialog):
def __init__(self):
QDialog.__init__(self)
self.setWindowTitle("About")
layout = QVBoxLayout()
layout.addWidget(QLabel("Image Converter: Version 0.1"))
layout.addWidget(QLabel("Copyright Ryan Moore 2012"))
self.setLayout(layout)
self.setFixedSize(450,100)
class MyMainWindow(QMainWindow):
def __init__(self,app):
QMainWindow.__init__(self)
self.app = app
mainLayoutWidget = QWidget()
mainLayout = QBoxLayout(QBoxLayout.LeftToRight)
leftLayout = QVBoxLayout()
self.pictureListView = DragDropListWidget()
self.pictureListView.dropped.connect(self.addPictures)
self.setContextMenuPolicy(Qt.CustomContextMenu)
self.customContextMenuRequested.connect(self.iconListRightClick)
leftLayout.addWidget(self.pictureListView)
leftLayoutButtonLayout = QBoxLayout(QBoxLayout.LeftToRight)
removeSelected = QPushButton("Remove Selected")
removeSelected.pressed.connect(self.removeSelected)
leftLayoutButtonLayout.addWidget(removeSelected)
clearButton = QPushButton("Clear All Images")
clearButton.pressed.connect(self.clearImages)
leftLayoutButtonLayout.addWidget(clearButton)
leftLayout.addLayout(leftLayoutButtonLayout)
mainLayout.addLayout(leftLayout)
groupBox = QGroupBox("Converter Settings")
groupBoxLayout = QVBoxLayout()
groupBoxFormLayout = QFormLayout()
self.convertToComboBox = QComboBox()
self.convertToComboBox.addItem("jpg")
self.convertToComboBox.addItem("png")
self.convertToComboBox.addItem("tif")
self.convertToComboBox.setEditable(False)
self.convertToComboBox.currentIndexChanged.connect(self.updateFileCountSlot)
groupBoxFormLayout.addRow("Output type:",self.convertToComboBox)
self.targetDirectoryComboBox = QComboBox()
self.targetDirectoryComboBox.addItem("In place")
self.targetDirectoryComboBox.addItem("Single Directory")
self.targetDirectoryComboBox.addItem("New Directory Structure")
self.targetDirectoryComboBox.setEditable(False)
self.targetDirectoryComboBox.currentIndexChanged.connect(self.processTargetDirectorySettingChange)
self.fileTextBox = FileSelectTextBox()
self.fileTextBox.setEnabled(False)
groupBoxFormLayout.addRow("Target Directory Settings:",self.targetDirectoryComboBox)
groupBoxFormLayout.addRow("Target Directory:",self.fileTextBox)
self.numFileCount=0
self.numFiles = QLabel(str(self.numFileCount))
groupBoxFormLayout.addRow("Number of files to be converted:",self.numFiles)
groupBoxLayout.addLayout(groupBoxFormLayout)
self.convertButton = QPushButton("Convert")
self.convertButton.pressed.connect(self.convertImages)
groupBoxLayout.addWidget(self.convertButton)
groupBox.setLayout(groupBoxLayout)
mainLayout.addWidget(groupBox)
mainLayoutWidget.setLayout(mainLayout)
self.__privateMainLayoutReference = mainLayoutWidget
self.setCentralWidget(mainLayoutWidget)
self.__initActions__()
self.__initMenu__()
self.__initToolbar__()
self.__pictureUrlList = []
self.__aboutDialog = None
self.__progressBar = None
def __initActions__(self):
self.addDirectory = QAction(QIcon('icons/directory.png'),"Add Folder",self)
self.addDirectory.setStatusTip("Add images from a folder.")
self.addDirectory.triggered.connect(self.addDir)
self.addPicture = QAction(QIcon('icons/text_directory.png'),"Add Image",self)
self.addPicture.setStatusTip("Add an image.")
self.addPicture.triggered.connect(self.addImage)
self.exitAction = QAction("Exit",self)
self.exitAction.setStatusTip("Exit")
self.exitAction.setShortcut(QKeySequence.Close)
self.exitAction.triggered.connect(self.close)
self.aboutAction = QAction("About",self)
self.aboutAction.setStatusTip("About Information")
self.aboutAction.triggered.connect(self.showAbout)
def __initMenu__(self):
fileMenu = self.menuBar().addMenu("&File")
fileMenu.addAction(self.addDirectory)
fileMenu.addAction(self.addPicture)
fileMenu.addAction(self.exitAction)
helpMenu = self.menuBar().addMenu("&Help")
helpMenu.addAction(self.aboutAction)
def __initToolbar__(self):
toolBar = self.addToolBar("Open Images")
toolBar.addAction(self.addDirectory)
toolBar.addAction(self.addPicture)
def addPictures(self,pictureList):
for url in pictureList:
if os.path.exists(url) and self.__pictureUrlList.count(url)<1:
icon = QIcon()
icon.addPixmap(QPixmap(url),QIcon.Normal,QIcon.Off)
item = QTreeWidgetItem(self.pictureListView)
item.setText(1,os.path.basename(url))
item.setStatusTip(1,url)
item.setIcon(0,icon)
self.__pictureUrlList.append(url)
self.updateFileCount()
def addDir(self):
imageDirectory = QFileDialog.getExistingDirectory(self,"Select an Image Folder",os.path.expanduser('~'))
if not ( imageDirectory == None or len(imageDirectory)==0):
self.addPictures(self.__getPictureListFromDirectory(imageDirectory))
else:
self.statusBar().showMessage("A directory was not set by the file dialog.")
@Slot(str)
def processTargetDirectorySettingChange(self,message):
if message == "In place" or message == 0:
self.fileTextBox.setEnabled(False)
else:
self.fileTextBox.setEnabled(True)
def addImage(self):
image = QFileDialog.getOpenFileName(self,"Select an Image", os.path.expanduser('~'),"Image Files (*.png *.jpg *.jpeg *.tif *.tiff)")
if not (image[0] == None or len(image[0])==0):
self.addPictures([image[0]])
else:
self.statusBar().showMessage("An image file was not found or selected.")
def clearImages(self):
self.pictureListView.clear()
self.__pictureUrlList = []
self.updateFileCount()
def removeSelected(self):
selectedList = self.pictureListView.selectedItems()
for selectedItem in selectedList:
rowNumber = self.pictureListView.indexOfTopLevelItem(selectedItem)
self.pictureListView.takeTopLevelItem(rowNumber)
self.__pictureUrlList.pop(rowNumber)
self.updateFileCount()
def iconListRightClick(self,qpoint):
globalPoint = self.mapToGlobal(qpoint)
popupMenu = QMenu()
popupMenu.addAction("Remove")
selectedItem = popupMenu.exec_(globalPoint)
if selectedItem:
if selectedItem.iconText() == "Remove":
selectedRow = self.pictureListView.indexOfTopLevelItem(self.pictureListView.currentItem())
self.pictureListView.takeTopLevelItem(selectedRow)
self.__pictureUrlList.pop(selectedRow)
self.updateFileCount()
def convertImages(self):
if len(self.__pictureUrlList) == 0:
return
self.pictureList = []
extensionPattern = re.compile("\." + self.convertToComboBox.currentText().strip() + "$")
for url in self.__pictureUrlList:
if extensionPattern.search(url) == None:
self.pictureList.append(url)
convertListLen = len(self.pictureList)
if convertListLen == 0:
return
self.__progressBar = QProgressBar()
self.__progressBar.setTextVisible(False)
self.__progressBar.setRange(0,convertListLen)
self.statusBar().insertPermanentWidget(1,self.__progressBar)
self.progressCount = 0
self.__privateMainLayoutReference.setEnabled(False)#disable the main ui while the convertion is going on.
self.__consumerThreads = []
self.__numActiveConsumerThreads = multiprocessing.cpu_count()
self.__urlListForThreads = BlockingQueue(self.pictureList)
for i in xrange(0,self.__numActiveConsumerThreads):
consumer = ImageConsumer(self.__urlListForThreads,self.targetDirectoryComboBox.currentText(),self.fileTextBox.getFolderText(),self.convertToComboBox.currentText())
consumer.consumed.connect(self.updateProgressBar)
consumer.finished.connect(self.finishedConvert)
self.__consumerThreads.append(consumer)
for thread in self.__consumerThreads:
thread.start()
def updateProgressBar(self):
if self.__progressBar == None:
return
self.progressCount+=1
self.__progressBar.setValue(self.progressCount)
def finishedConvert(self):
self.__numActiveConsumerThreads-=1
if self.__numActiveConsumerThreads <=0:
self.__privateMainLayoutReference.setEnabled(True)#reenable the main ui.
self.__consumerThreads = None
self.__urlListForThreads = None
self.statusBar().removeWidget(self.__progressBar)
self.__progressBar = None
def updateFileCount(self):
"""
This computes how many files will be converted based off of how many urls there are minus the number that share the conversion extension
"""
fileCount = 0
extensionPattern = re.compile("\." + self.convertToComboBox.currentText().strip() + "$")
for url in self.__pictureUrlList:
if extensionPattern.search(url) == None:
fileCount+=1
self.numFileCount = fileCount
self.numFiles.setText(str(self.numFileCount))
@Slot(int)
def updateFileCountSlot(self,index):
self.statusBar().showMessage("Output image type changed.")
self.updateFileCount()
def close(self):
if self.__consumerThreads != None:
for thread in self.__consumerThreads:
thread.setRunning(False)
self.app.quit()
def closeEvent(self,event):
event.accept()
self.close()
def __getPictureListFromDirectory(self,directory):
pictureList = []
imagePostfixPattern = re.compile("\.png|\.jpeg|\.jpg|\.tiff|\.tif")
for (path,dirs,files) in os.walk(directory):
for fileName in files:
if imagePostfixPattern.search(fileName) != None:
pictureList.append(path+'/'+fileName)
return pictureList
def showAbout(self):
if not self.__aboutDialog:
self.__aboutDialog = AboutDialog()
self.__aboutDialog.show()
self.__aboutDialog.activateWindow()
if __name__ == '__main__':
app = QApplication(sys.argv)
mainWindow = MyMainWindow(app)
mainWindow.show()
app.exec_()
sys.exit()