-
Notifications
You must be signed in to change notification settings - Fork 0
/
photo-frame-prep.py
executable file
·116 lines (100 loc) · 4.43 KB
/
photo-frame-prep.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
#!/usr/bin/python
# ###############################################################
# Photo Frame Prep, a python gui program for preparing digital photos for your digital picture frame
# Copyright (C) 2008 Tim Abell <tim@timwise.co.uk>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# See the file COPYING for the full license.
# ###############################################################
#standard library imports
import wx #import normal wxPython widgets library
import shutil #file utilities
import os #to execute shell commands (ideally remove this and use native python libaries to deal with images)
#local module imports
import gui # import wxGlade generated gui from gui.py
#inherit from the gui class to separate hand crafted code from generated form code
class MyApp(gui.MyFrame):
def __init__(self, *args, **kwds):
gui.MyFrame.__init__(self, *args, **kwds) #call base class init
#bind event handler for go button
self.gobutton.Bind(wx.EVT_BUTTON, self.goClicked)
self.inputBrowseFile.Bind(wx.EVT_BUTTON, self.browseInputFile)
self.inputBrowseFolder.Bind(wx.EVT_BUTTON, self.browseinputPath)
self.outputBrowse.Bind(wx.EVT_BUTTON, self.browseOutput)
#event handler for go button
def goClicked(self, event):
print 'go clicked'
#TODO fail if paths missing / invalid
processor = PhotoProcessing(self.outputPath.GetValue())
processor.process(self.inputPath.GetValue())
def browseInputFile(self, event):
self.browseFile(self.inputPath)
def browseinputPath(self, event):
self.browseFolder(self.inputPath)
def browseOutput(self, event):
self.browseFolder(self.outputPath)
def browseFile(self, target):
dlg = wx.FileDialog(self, message="Choose a file")
filename = ""
dlg.SetPath(os.path.dirname(target.GetValue())) #open at last location
if dlg.ShowModal() == wx.ID_OK:
filename = dlg.GetPath()
if filename:
target.SetValue(filename)
def browseFolder(self, target):
dlg = wx.DirDialog(self, message="Choose a file")
filename = ""
dlg.SetPath(target.GetValue()) #open at last location
if dlg.ShowModal() == wx.ID_OK:
filename = dlg.GetPath()
if filename:
target.SetValue(filename + '/')
#main class for doing the work of this application
class PhotoProcessing():
output = ''
def __init__(self, outputPath):
global output
output=outputPath
#process file or folder (recursively)
def process(self, inputPath):
if os.path.basename(inputPath)=="": #check if path has a filename
self.processFolder(inputPath)
else:
self.processPhoto(inputPath)
def processFolder(self, inputPath):
print 'processing folder ', inputPath
for path, dirs, files in os.walk(inputPath):
for file in files:
self.processPhoto(os.path.join(path, file))
def processPhoto(self, inputPhotoPath):
print 'processing photo, path: ', inputPhotoPath, ' output path: ', output
filename = os.path.basename(inputPhotoPath)
#copy to output folder
shutil.copy(inputPhotoPath, output)
outputfile = os.path.join(output, filename)
#rotate to match exif rotate tag
print 'executing exiftran -ai ', outputfile
os.system('exiftran -ai ' + outputfile )
#exiftran -ai "$INPUTFILE"
#resize and add black background if aspect ratio doesn't match frame size
print 'convert "' + outputfile + '" -resize \'800x600>\' -background black -gravity center -extent 800x600 ' + outputfile
os.system('convert "' + outputfile + '" -resize \'800x600>\' -background black -gravity center -extent 800x600 ' + outputfile)
#convert "$INPUTFILE" -resize '800x600>' -background black -gravity center -extent 800x600 "$INPUTFILE"
#program flow starts here.
print 'starting up app...'
app = wx.App() # um, something to initialize the app i guess. don't really know
frame = MyApp(None, -1, 'photo-frame-prep.py') # load the frame overriden gui class
frame.Show() #show the frame (presumably)
app.MainLoop() #run the app, or something