What does "raw image data, as a byte string" mean?

843 views Asked by At

I am doing a program to edit tags on mp3 using Python, right now I am using the mutagen module and in order to embed an image as a cover art to an mp3 file using id3v4 standards I have to add the APIC frame using this.

But I don't understand what I have to put in the parameters encoding,mime and data.

I looked an example from here and came up with this:

frame= APIC(3,"image/jpg",3,"Cover",open("albumcover.jpg"))

But I don't know what the first 3 means? Why when I put "utf-8" it doesn't work? And the open() function doesn't work, it returns an error like this:

Traceback (most recent call last):
  File "<pyshell#104>", line 1, in <module>
    frame= APIC(3,"image/jpg",3,"Cover",open("albumcover.jpg"))
  File "C:\Python34\lib\site-packages\mutagen\id3\_frames.py", line 65, in __init__
    setattr(self, checker.name, checker.validate(self, val))
  File "C:\Python34\lib\site-packages\mutagen\id3\_specs.py", line 184, in validate
    raise TypeError("%s has to be bytes" % self.name)
TypeError: data has to be bytes

and when I put the "b"

frame= APIC(3,"image/jpg",3,"Cover",open("albumcover.jpg","b"))

it returns

Traceback (most recent call last):
  File "<pyshell#106>", line 1, in <module>
    frame= APIC("utf-8","image/jpg",3,"Cover",open("albumcover.jpg","b"))
ValueError: Must have exactly one of create/read/write/append mode and at most one plus

So what should I put there?

And I tried open("albumcover.jpg").read() too and it doesn't work.

2

There are 2 answers

0
Anand S Kumar On BEST ANSWER

You need to open the file in either of - read (rb) or write (wb) or append (ab)modes (b - indicating that its a binary file and that we read bytes from it instead of strings) .

For your case, I think read mode would be sufficient, so try -

frame= APIC(3,"image/jpg",3,"Cover",open("albumcover.jpg","rb").read())

The rb indicates that we need to open the file in read mode and that it is a binary file, calling the .read() function on it causes it to read the bytes from the file and return it.

1
jhoepken On

The parameter 3 means, that it's the album's front cover, read the documentation.