1
- # -*- coding: utf-8 -*-
2
1
# This file is part of beets.
3
2
# Copyright 2016, Adrian Sampson.
4
3
#
17
16
music and items' embedded album art.
18
17
"""
19
18
20
- from __future__ import division , absolute_import , print_function
21
19
22
20
import subprocess
23
21
import platform
@@ -43,7 +41,7 @@ def get_art(log, item):
43
41
try :
44
42
mf = mediafile .MediaFile (syspath (item .path ))
45
43
except mediafile .UnreadableFileError as exc :
46
- log .warning (u 'Could not extract art from {0}: {1}' ,
44
+ log .warning ('Could not extract art from {0}: {1}' ,
47
45
displayable_path (item .path ), exc )
48
46
return
49
47
@@ -58,20 +56,20 @@ def embed_item(log, item, imagepath, maxwidth=None, itempath=None,
58
56
# Conditions and filters.
59
57
if compare_threshold :
60
58
if not check_art_similarity (log , item , imagepath , compare_threshold ):
61
- log .info (u 'Image not similar; skipping.' )
59
+ log .info ('Image not similar; skipping.' )
62
60
return
63
61
if ifempty and get_art (log , item ):
64
- log .info (u 'media file already contained art' )
62
+ log .info ('media file already contained art' )
65
63
return
66
64
if maxwidth and not as_album :
67
65
imagepath = resize_image (log , imagepath , maxwidth , quality )
68
66
69
67
# Get the `Image` object from the file.
70
68
try :
71
- log .debug (u 'embedding {0}' , displayable_path (imagepath ))
69
+ log .debug ('embedding {0}' , displayable_path (imagepath ))
72
70
image = mediafile_image (imagepath , maxwidth )
73
- except IOError as exc :
74
- log .warning (u 'could not read image file: {0}' , exc )
71
+ except OSError as exc :
72
+ log .warning ('could not read image file: {0}' , exc )
75
73
return
76
74
77
75
# Make sure the image kind is safe (some formats only support PNG
@@ -90,16 +88,16 @@ def embed_album(log, album, maxwidth=None, quiet=False, compare_threshold=0,
90
88
"""
91
89
imagepath = album .artpath
92
90
if not imagepath :
93
- log .info (u 'No album art present for {0}' , album )
91
+ log .info ('No album art present for {0}' , album )
94
92
return
95
93
if not os .path .isfile (syspath (imagepath )):
96
- log .info (u 'Album art not found at {0} for {1}' ,
94
+ log .info ('Album art not found at {0} for {1}' ,
97
95
displayable_path (imagepath ), album )
98
96
return
99
97
if maxwidth :
100
98
imagepath = resize_image (log , imagepath , maxwidth , quality )
101
99
102
- log .info (u 'Embedding album art into {0}' , album )
100
+ log .info ('Embedding album art into {0}' , album )
103
101
104
102
for item in album .items ():
105
103
embed_item (log , item , imagepath , maxwidth , None , compare_threshold ,
@@ -110,7 +108,7 @@ def resize_image(log, imagepath, maxwidth, quality):
110
108
"""Returns path to an image resized to maxwidth and encoded with the
111
109
specified quality level.
112
110
"""
113
- log .debug (u 'Resizing album art to {0} pixels wide and encoding at quality \
111
+ log .debug ('Resizing album art to {0} pixels wide and encoding at quality \
114
112
level {1}' , maxwidth , quality )
115
113
imagepath = ArtResizer .shared .resize (maxwidth , syspath (imagepath ),
116
114
quality = quality )
@@ -135,7 +133,7 @@ def check_art_similarity(log, item, imagepath, compare_threshold):
135
133
syspath (art , prefix = False ),
136
134
'-colorspace' , 'gray' , 'MIFF:-' ]
137
135
compare_cmd = ['compare' , '-metric' , 'PHASH' , '-' , 'null:' ]
138
- log .debug (u 'comparing images with pipeline {} | {}' ,
136
+ log .debug ('comparing images with pipeline {} | {}' ,
139
137
convert_cmd , compare_cmd )
140
138
convert_proc = subprocess .Popen (
141
139
convert_cmd ,
@@ -159,7 +157,7 @@ def check_art_similarity(log, item, imagepath, compare_threshold):
159
157
convert_proc .wait ()
160
158
if convert_proc .returncode :
161
159
log .debug (
162
- u 'ImageMagick convert failed with status {}: {!r}' ,
160
+ 'ImageMagick convert failed with status {}: {!r}' ,
163
161
convert_proc .returncode ,
164
162
convert_stderr ,
165
163
)
@@ -169,7 +167,7 @@ def check_art_similarity(log, item, imagepath, compare_threshold):
169
167
stdout , stderr = compare_proc .communicate ()
170
168
if compare_proc .returncode :
171
169
if compare_proc .returncode != 1 :
172
- log .debug (u 'ImageMagick compare failed: {0}, {1}' ,
170
+ log .debug ('ImageMagick compare failed: {0}, {1}' ,
173
171
displayable_path (imagepath ),
174
172
displayable_path (art ))
175
173
return
@@ -180,10 +178,10 @@ def check_art_similarity(log, item, imagepath, compare_threshold):
180
178
try :
181
179
phash_diff = float (out_str )
182
180
except ValueError :
183
- log .debug (u 'IM output is not a number: {0!r}' , out_str )
181
+ log .debug ('IM output is not a number: {0!r}' , out_str )
184
182
return
185
183
186
- log .debug (u 'ImageMagick compare score: {0}' , phash_diff )
184
+ log .debug ('ImageMagick compare score: {0}' , phash_diff )
187
185
return phash_diff <= compare_threshold
188
186
189
187
return True
@@ -193,18 +191,18 @@ def extract(log, outpath, item):
193
191
art = get_art (log , item )
194
192
outpath = bytestring_path (outpath )
195
193
if not art :
196
- log .info (u 'No album art present in {0}, skipping.' , item )
194
+ log .info ('No album art present in {0}, skipping.' , item )
197
195
return
198
196
199
197
# Add an extension to the filename.
200
198
ext = mediafile .image_extension (art )
201
199
if not ext :
202
- log .warning (u 'Unknown image type in {0}.' ,
200
+ log .warning ('Unknown image type in {0}.' ,
203
201
displayable_path (item .path ))
204
202
return
205
203
outpath += bytestring_path ('.' + ext )
206
204
207
- log .info (u 'Extracting album art from: {0} to: {1}' ,
205
+ log .info ('Extracting album art from: {0} to: {1}' ,
208
206
item , displayable_path (outpath ))
209
207
with open (syspath (outpath ), 'wb' ) as f :
210
208
f .write (art )
@@ -220,7 +218,7 @@ def extract_first(log, outpath, items):
220
218
221
219
def clear (log , lib , query ):
222
220
items = lib .items (query )
223
- log .info (u 'Clearing album art from {0} items' , len (items ))
221
+ log .info ('Clearing album art from {0} items' , len (items ))
224
222
for item in items :
225
- log .debug (u 'Clearing art for {0}' , item )
223
+ log .debug ('Clearing art for {0}' , item )
226
224
item .try_write (tags = {'images' : None })
0 commit comments