How to add ID3 "cover art" by command line?

An excellent Python application that I routinely use to add cover art to mp3 files is the command line applicationeyeD3. This can be installed from a Terminal as follows:

sudo apt-get install eyed3

Here is an example of a command to add a cover image named cover.jpg to an mp3 file named test.mp3:

eyeD3 --add-image "cover.jpg:FRONT_COVER" test.mp3

See an example below of this at work on my own computer, adding a cover image to an mp3 otherwise empty of meta tags:

andrew@ilium~$ eyeD3 --add-image "cover.jpg:FRONT_COVER" test.mp3
test.mp3    [ 946.12 KB ]
-------------------------------------------------------------------------------
Adding image cover.jpg
Time: 01:00 MPEG1, Layer III    [ 128 kb/s @ 44100 Hz - Joint stereo ]
-------------------------------------------------------------------------------
ID3 v2.4:
title: 
artist: 
album: 
album artist: None
track:      
FRONT_COVER Image: [Size: 95788 bytes] [Type: image/jpeg]
Description: 

Writing ID3 version v2.4
-------------------------------------------------------------------------------
andrew@ilium~$ 

There are many other options for adding images although I have given the basic syntax above. Below are these other options:

  --add-image IMG_PATH:TYPE[:DESCRIPTION]
                        Add or replace an image. There may be more than one
                        image in a tag, as long as the DESCRIPTION values are
                        unique. The default DESCRIPTION is ''. If PATH begins
                        with 'http[s]://' then it is interpreted as a URL
                        instead of a file containing image data. The TYPE must
                        be one of the following: OTHER, ICON, OTHER_ICON,
                        FRONT_COVER, BACK_COVER, LEAFLET, MEDIA, LEAD_ARTIST,
                        ARTIST, CONDUCTOR, BAND, COMPOSER, LYRICIST,
                        RECORDING_LOCATION, DURING_RECORDING,
                        DURING_PERFORMANCE, VIDEO, BRIGHT_COLORED_FISH,
                        ILLUSTRATION, BAND_LOGO, PUBLISHER_LOGO.

References:

  • eyeD3: Complex Options (Images)

Here is a Python script that works for me. Run it with python script.py audiofile.mp3.

You will need mutagen; install it with sudo -H pip install mutagen.

from mutagen.mp3 import MP3
from mutagen.id3 import ID3, APIC, error
import sys
mp3file=sys.argv[1]
audio = MP3(mp3file, ID3=ID3)

try:
   audio.add_tags()
except error:
   pass

audio.tags.add(
   APIC(
      encoding=1,
      mime='image/png',
      type=3,
      desc=u'Cover',
      data=open('/path/to/artwork.png').read()
   )
)
audio.save()