-
Notifications
You must be signed in to change notification settings - Fork 0
/
original-convert
executable file
·115 lines (94 loc) · 3.41 KB
/
original-convert
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/env python
# Author: Jan Larres <[email protected]>
# License: MIT/X11
import os
import sqlite3
import sys
import time
from wand.image import Image, ORIENTATION_TYPES
from zipfile import ZipFile
DBFILE = os.environ['HOME'] + '/.local/share/shotwell/data/photo.db'
if len(sys.argv) != 2:
print 'usage: %s <description>' % os.path.basename(sys.argv[0])
sys.exit(1)
for dir in ['comments', 'hq', 'lq', 'thumbs', 'zip']:
os.mkdir(dir)
with open('.htaccess', 'w') as f:
f.write("""
<Files info.txt>
deny from all
</Files>
"""[1:])
files = filter(lambda f: f.lower().endswith('.jpg'), os.listdir('.'))
imgdict = dict()
print 'Analyzing metadata ...'
for fname in files:
with Image(filename=fname) as img:
tag = img.metadata.get('exif:DateTimeOriginal')
timestamp = time.strptime(tag, "%Y:%m:%d %H:%M:%S")
imgdict[timestamp] = fname
timestamps = imgdict.keys()
timestamps.sort()
dbconn = sqlite3.connect(DBFILE)
cursor = dbconn.cursor()
for idx, key in enumerate(timestamps):
idx += 1
fname = imgdict[key]
cursor.execute("SELECT title,comment FROM PhotoTable WHERE filename LIKE ?",
("%" + fname,))
[title, comment] = cursor.fetchone()
desc = title if title else comment
desc = desc.encode('utf-8') if desc else ''
print '%s: %s' % (fname, desc)
with open('comments/%s.txt' % idx, 'w') as f:
f.write('<span>photo %s</span> %s\n' % (idx, desc))
newfile = 'img-%s.jpg' % idx
os.rename(fname, newfile)
if desc != '':
with open('comments.txt', 'a') as f:
f.write('%s: %s\n' % (newfile, desc))
with Image(filename=newfile) as img:
# http://sylvana.net/jpegcrop/exif_orientation.html
if img.orientation != ORIENTATION_TYPES[1]:
if img.orientation == ORIENTATION_TYPES[2]:
img.flop()
elif img.orientation == ORIENTATION_TYPES[3]:
img.rotate(180)
elif img.orientation == ORIENTATION_TYPES[4]:
img.flip()
elif img.orientation == ORIENTATION_TYPES[5]:
img.rotate(270)
img.flip()
elif img.orientation == ORIENTATION_TYPES[6]:
img.rotate(90)
elif img.orientation == ORIENTATION_TYPES[7]:
img.rotate(90)
img.flip()
elif img.orientation == ORIENTATION_TYPES[8]:
img.rotate(270)
img.orientation = ORIENTATION_TYPES[1]
for newsize, quality, dir in [['2000x2000>', 90, 'hq'],
['640x640>', 90, 'lq'],
['120x120>', 60, 'thumbs']]:
with img.clone() as newimg:
newimg.transform(resize=newsize)
newimg.compression_quality = quality
newimg.save(filename='%s/%s' % (dir, newfile))
# os.rename(newfile, 'hq/%s' % newfile)
os.unlink(newfile)
dbconn.close()
with ZipFile('zip/hq.zip', 'w') as zipfile:
for f in os.listdir('hq'):
zipfile.write('hq/' + f, f)
zipfile.write('comments.txt')
os.unlink('comments.txt')
galleryname = os.path.basename(os.path.realpath(os.path.curdir))
ts = timestamps[0]
gallerydate = '%02d.%02d.%d' % (ts.tm_mday, ts.tm_mon, ts.tm_year)
gallerydesc = sys.argv[1]
with open('info.txt', 'w') as f:
f.write("""
name|%s
date|%s
description|%s
"""[1:] % (galleryname, gallerydate, gallerydesc))