-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAutoImageCover.py
More file actions
119 lines (95 loc) · 3.18 KB
/
AutoImageCover.py
File metadata and controls
119 lines (95 loc) · 3.18 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
import sys
import os
from mutagen.flac import FLAC
# Change import name for code clarity
from mutagen.flac import Picture as FlacPicture
from mutagen.easyid3 import EasyID3
from mutagen.id3 import ID3, APIC
from mutagen.mp3 import MP3
from io import BytesIO
import PIL
import PIL.ImageTk
SONG_PATH = sys.argv[1]
IMG_PATH = sys.argv[2].replace('\n', '')
def imgChangesCall(item, img_path, *args, **kwargs):
'''
Calls the procedure to only change the image metadata.
Loads the new image and then iterates over each item setting
the picture.
'''
if img_path.endswith(('jpg', 'jpeg', 'png')) and os.path.exists(img_path):
# Load the item metadata and set the new picture.
if item.endswith('flac'):
# Load the new image
img = flacPicture(img_path)
meta_audio = FLAC(item)
applyImgChangesFLAC(meta_audio, img)
elif item.endswith('mp3'):
# Load the new image
img = mp3Picture(img_path)
meta_audio = ID3(item)
applyImgChangesMP3(meta_audio, img)
print('Added "{}" to "{}"'.format(img_path, item))
def flacPicture(*args, **kwargs):
'''
Creates a mutagen.flac.Picture object, sets its mimetype, its
type and its description. Then loads the selected img and returns
the Picture object.
'''
# Set the corresponding mime type
if img_path.endswith('png'):
mime_type = 'image/png'
else:
mime_type = 'image/jpg'
# Open bytes like object for the image
albumart = open(img_path, 'rb').read()
# create img object for flacs and set its properties
img = FlacPicture()
# type 3 is for cover image
img.type = 3
# Set the corresponding mime type
img.mime = mime_type
# Set description
img.desc = 'front cover'
img.data = albumart
return img
def mp3Picture(img_path, *args, **kwargs):
'''
Creates a mutagen APIC object, sets its mimetype, its
type and its description. Then loads the selected img and returns
the Picture object.
'''
# Set the corresponding mime type
if img_path.endswith('png'):
mime_type = 'image/png'
else:
mime_type = 'image/jpg'
# Open bytes like object for the image
albumart = open(img_path, 'rb').read()
# Create Image object for mp3s and set its properties
apic = APIC(
encoding=3,
mime=mime_type,
type=3, desc='front cover',
data=albumart
)
return apic
def applyImgChangesFLAC(meta_audio, img=None, *args, **kwargs):
''' Changes the image of a flac audio file '''
if img:
meta_audio.clear_pictures() # clear other images
meta_audio.add_picture(img) # set new image
meta_audio.save()
def applyImgChangesMP3(meta_audio, apic=None, *args, **kwargs):
''' Changes the image of a mp3 audio file '''
if apic:
for tag in meta_audio:
if 'APIC' in tag:
meta_audio.delall(tag)
break
meta_audio['APIC'] = apic
meta_audio.save()
if SONG_PATH and IMG_PATH:
imgChangesCall(SONG_PATH, IMG_PATH)
else:
print('No song and no img')