-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgenpreview.py
More file actions
204 lines (142 loc) · 5.11 KB
/
genpreview.py
File metadata and controls
204 lines (142 loc) · 5.11 KB
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
#!/usr/bin/python2
"""
Preview Generator
Hacked together by Gregory Eric Sanderson Turcot Temlett MacDonnell Forbes
I needed to select a bunch of pictures in a folder (and their sub-folders)
and do some processing on them with a bash script. Having nothing better to do,
I made this script. It generates a HTML page with thumbnails of all the pictures
in a folder. A list of pictures that you have clicked on will appear at the
bottom of the page. Copy/paste the list and do something with it
through bash + xargs
License:
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/>.
"""
import sys
import os
import Image
HEADER = """
<html>
<head>
<title>Simple thumbnail generator (http://github.com/gelendir)</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<style type="text/css">
table {
width: 100%;
}
img {
width: 100%;
}
td {
padding: 3px 3px 3px 3px;
border: 2px solid gray;
}
.dim {
opacity: 0.2;
}
</style>
<script type="text/javascript">
$( function() {
var selected = [];
$("img").click( function() {
var file = $(this).attr("src");
var img = $(this);
if( img.hasClass("dim") ) {
img.removeClass("dim");
selected.splice( $.inArray( file, selected ), 1 );
} else {
img.addClass("dim");
selected.push( file );
}
$("#remove").empty();
for( index in selected ) {
file = selected[index];
$("#remove").append(file + "<br />");
}
});
});
</script>
</head>
<body>
<table>
<tr>
"""
FOOTER = """
</tr>
</table>
<div id="remove">
</div>
</body>
</html>
"""
EXTENSIONS = ['png', 'jpg', 'jpeg', 'gif']
COLUMNS = 3
SCREEN_SIZE = ( 1280, 800 )
def is_image_file( path ):
extension = path.lower().split(".")[-1]
return extension in EXTENSIONS
def image_files( dirpath, filelist, target_folder ):
images = []
for f in filelist:
path = os.path.join( target_folder, dirpath, f )
exists = os.path.isfile( path )
if is_image_file( f ) and not exists:
images.append( f )
return images
def resize_images( folder, target_folder, thumbnail ):
for(dirpath, dirnames, filenames) in os.walk( folder ):
destination_folder = os.path.join( target_folder, dirpath )
if not os.path.isdir( destination_folder ):
os.mkdir( destination_folder )
for filename in image_files( dirpath, filenames, target_folder ):
origin = os.path.join( dirpath, filename )
destination = os.path.join( destination_folder, filename )
print( "resizing {0}".format( origin ) )
image = Image.open( origin )
image.thumbnail( thumbnail, Image.ANTIALIAS )
image.convert('RGB').save( destination, "JPEG" )
def create_html_file( origin_folder, target_folder, columns ):
destination = os.path.join( target_folder, "index.html" )
tmpl = '<td><img src="{filename}" /></td>'
counter = 1
with open( destination, 'w' ) as html:
html.write( HEADER )
for (dirpath, dirnames, filenames) in os.walk( origin_folder ):
filenames = ( f for f in filenames if is_image_file( f ) )
for (pos, filename) in enumerate( filenames ):
path = "/".join([dirpath, filename])
code = tmpl.format( filename = path )
html.write( code )
if counter % columns == 0:
html.write("</tr><tr>")
counter += 1
html.write( FOOTER )
def main():
if len( sys.argv ) < 3:
print( "USAGE: {0} SOURCE_DIR DEST_DIR [COLUMNS] [SCREEN_WIDTH SCREEN_HEIGHT]".format( sys.argv[0] ) )
sys.exit(1)
columns = COLUMNS
screen_size = SCREEN_SIZE
origin_folder = sys.argv[1]
target_folder = sys.argv[2]
if len( sys.argv ) >= 4:
columns = int( sys.argv[3] )
if len( sys.argv ) >= 6:
screen_size = ( int( x ) for x in sys.argv[5:7] )
thumbnail = (
screen_size[0] / columns,
screen_size[1] / columns )
if not os.path.isdir( target_folder ):
os.mkdir( target_folder )
resize_images( origin_folder, target_folder, thumbnail )
create_html_file( origin_folder, target_folder, columns )
if __name__ == "__main__":
main()