-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimages.py
151 lines (136 loc) · 4.34 KB
/
images.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
import io
import os
from mutagen.id3 import ID3, APIC, PictureType, Encoding
from PIL import ImageFile
from pathlib import Path
from common import get_leaf_dirs, get_mp3_files, NEW, unique_path, safe_string
from covers import AUDIOBOOKSTORE_SEARCH_URI
mimetype = {
'JPEG' : 'image/jpeg',
'PNG' : 'image/png',
}
DOC_NAME = '_image_search_links.html'
COMPILED_DOC_NAME = '_compiled_search_links.html'
PAGE_START = '''
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Image Search Links</title>
</head>
<body>
'''
PAGE_END = '''
</body>
</html>
'''
GOOGLE_INFO_SEARCH = 'Google: <a href="https://google.com/search?q={0}">{1}</a>\n'
GOOGLE_IMAGE_SEARCH = 'GoogleImages: <a href="https://google.com/search?q={0}&tbm=isch">{1}</a>\n'
AUDIOBOOKSTORE_SEARCH = f'Audiobookstore: <a href="{AUDIOBOOKSTORE_SEARCH_URI}">{{1}}</a>\n'
def sort_search_items(items):
def key(item):
if 'audiobook cover' in item:
return '1' + item
elif 'author photo' in item:
return '2' + item
else:
return '3' + item
items.sort(key=key)
def create_image_search_links(search_items, docpath=NEW, docname=DOC_NAME):
if not search_items:
return
path = unique_path(Path(docpath) / docname)
with open(path, 'w') as search_doc:
search_doc.write(PAGE_START)
for item in search_items:
safe_item = safe_string(item)
search_doc.write('\n<p>\n')
if item.endswith('audiobook cover'):
trueitem = (item[:-15])
safe_trueitem = safe_string(trueitem)
search_doc.write(f'<h2>{trueitem}</h2>\n')
search_doc.write(GOOGLE_INFO_SEARCH.format(safe_trueitem,trueitem))
search_doc.write('\n<br/>\n')
search_doc.write(AUDIOBOOKSTORE_SEARCH.format(safe_trueitem,trueitem))
search_doc.write('\n<br/>\n')
search_doc.write(GOOGLE_IMAGE_SEARCH.format(safe_item,item))
search_doc.write('\n</p>\n')
search_doc.write(PAGE_END)
def stretch_cover_image(mp3file):
tags = ID3(mp3file)
#TODO: Decide whether should do anything with mulitple pictures
for tag in tags.keys():
if tag[:4] == "APIC":
apic_tag = tag
break
try:
apic = tags[apic_tag]
except NameError:
return
parser = ImageFile.Parser()
parser.feed(apic.data)
im = parser.close()
width, height = im.size
if width == height:
# image is already square
return
maxdim = max(width, height)
square_image = im.resize((maxdim, maxdim))
with io.BytesIO() as output:
square_image.save(output, format=im.format)
data = output.getvalue()
newapic = APIC(
encoding=Encoding.UTF8,
mime=mimetype[im.format],
type=PictureType.COVER_FRONT,
desc='Stretched Book Cover',
data=data,
)
tags.delall(apic_tag)
tags.add(newapic)
tags.save()
def extract_cover_image(mp3file):
tags = ID3(mp3file)
#TODO: Decide whether should do anything with mulitple pictures
for tag in tags.keys():
if tag[:4] == "APIC":
apic_tag = tag
break
try:
apic = tags[apic_tag]
except NameError:
return
tags.delall(apic_tag)
tags.save()
parser = ImageFile.Parser()
parser.feed(apic.data)
im = parser.close()
width, height = im.size
if width == height:
square_image = im
else:
maxdim = max(width, height)
square_image = im.resize((maxdim, maxdim))
with io.BytesIO() as output:
square_image.save(output, format=im.format)
data = output.getvalue()
def stretch_all_covers(startdir):
if isinstance(startdir, str):
dirs = get_leaf_dirs(startdir)
else:
dirs = startdir
for bookdir in dirs:
mp3files = get_mp3_files(bookdir)
for mp3file in mp3files:
stretch_cover_image(os.path.join(bookdir,mp3file))
def stretch_all_covers(startdir):
if isinstance(startdir, str):
dirs = get_leaf_dirs(startdir)
else:
dirs = startdir
for bookdir in dirs:
mp3files = get_mp3_files(bookdir)
for mp3file in mp3files:
stretch_cover_image(os.path.join(bookdir, mp3file))
if len(x for x in bookdir.iterdir() if x.stem.startswith('cover')) > 0:
return