I've been using mutagen for reading and writing MP3 tags, but I want to be able to embed album art directly into the file.
-
1isn't that a very bad idea? Won't you increase your mp3 sizes by embedding the same picture many times on all mp3 of a single album? Album art should belong to the folder where the album is located. – nosklo Jan 06 '09 at 17:57
-
2@nosklo there are mp3 players showing only embedded pictures and not the one located in folder – sdu Dec 19 '09 at 13:53
-
@nosklo I have found that my mp3 files are so large that a tiny thumbnail doesn't make much difference. I estimated my average size at 6MB, I allow my thumbnails to be up to 60KB so they only add 1% to the file size. With JPEG that turns out to be plenty. – Mark Ransom Aug 01 '16 at 16:47
5 Answers
Here is how to add example.png as album cover into example.mp3 with mutagen:
from mutagen.mp3 import MP3
from mutagen.id3 import ID3, APIC, error
audio = MP3('example.mp3', ID3=ID3)
# add ID3 tag if it doesn't exist
try:
audio.add_tags()
except error:
pass
audio.tags.add(
APIC(
encoding=3, # 3 is for utf-8
mime='image/png', # image/jpeg or image/png
type=3, # 3 is for the cover image
desc=u'Cover',
data=open('example.png').read()
)
)
audio.save()

- 2,547
- 3
- 29
- 27
-
2This doesn't work; I follow the code above and I get "TypeError: an integer is required" – Tensigh Jun 08 '15 at 14:45
-
1For me neither, I get: `TypeError: 'str' object cannot be interpreted as an integer`. Full trace and source here: https://gist.github.com/brutus/69c51cd706a6cf5d1594 – Brutus Oct 01 '15 at 15:16
-
1I found it necessary to use `v2_version=3` in the `save` so that the new tags could be read by Windows 7, and I simplified this code by reading directly into an `ID3` object. Otherwise this answer was perfect, none of the errors reported by others. – Mark Ransom Aug 04 '16 at 04:36
-
@MarkRansom, how did you "simplified this code by reading directly into an ID3 object"? Please post your code in an answer. – MagTun Mar 21 '17 at 13:12
-
5Great answer, but in my case, I found it necessary to add 'rb' mode to read success, just like this: `data=open('example.png', 'rb').read()` – codezjx Jun 18 '17 at 03:55
I've used the eyeD3 module to do this exact thing.
def update_id3(mp3_file_name, artwork_file_name, artist, item_title):
#edit the ID3 tag to add the title, artist, artwork, date, and genre
tag = eyeD3.Tag()
tag.link(mp3_file_name)
tag.setVersion([2,3,0])
tag.addImage(0x08, artwork_file_name)
tag.setArtist(artist)
tag.setDate(localtime().tm_year)
tag.setTitle(item_title)
tag.setGenre("Trance")
tag.update()

- 22,247
- 13
- 42
- 47
-
As of v0.7, usage is like this: `audiofile = eyed43.load(file_name)` and `audiofile.tag.artist = artist`, etc. See: http://eyed3.nicfit.net/ – Dec 06 '15 at 16:54
-
This blog does a good job of explaing the eyed3 implementation using the updated and latest API. http://tuxpool.blogspot.in/2013/02/how-to-store-images-in-mp3-files-using.html – Rajeev Atmakuri Jan 22 '17 at 15:44
-
eyeD3 does NOT support Unicode so it will fail whenever the paths to the music file or the picture contain any non-ASCII character. So it is useless if you have songs like "Canção do mar", "Dvořák's 9. Simphonie" or "昨日は歌を歌いました". I suggest Python's metagen module. – Jaccoud May 24 '21 at 22:27
Looks like you have to add a special type of frame to the MP3. See the site on ID3 tags
Also the tutorial for mutagen implies that you can add ID3 tags in mutagen see

- 10,672
- 3
- 33
- 38
Are you trying to embed images into a lot of files? If so, I found a script (see the link) that goes through a set of directories, looks for images, and the embeds them into MP3 files. This was useful for me when I wanted to actually have something to look at in CoverFlow on my (now defunct) iPhone.

- 11
- 1
A nice small CLI tool which helped me a lot with checking what I did while developing id3 stuff is mid3v2 which is the mutagen version of id3v2. It comes bundled with the Python mutagen library. The source of this little tool gave me also lots of answers about how to use mutagen.

- 2,228
- 24
- 25