Python Convert Bytes Literal To Image
<p>In Python3 I am working on a music playing application. I'm using a library called TinyTag to get metadata from the music files, like the title and artist. It also supports getting the album art. When the art is retrieved it loads it as what I believe is called a bytes literal (I'm unfamiliar with it). I was wondering how to turn this data into an image. Here's the code:</p>

<pre><code>from tinytag import TinyTag
tag = TinyTag.get("/path/to/file.mp3", image=True)
image_data = tag.get_image()
print(image_data)
</code></pre>

<p>The image data is a massive sting prefaced by the letter "b". It looks like this:</p>

<pre><code>b'\xff\xd8\xff\xe0\x00\x10JFIF\x00\x01\x01...'
</code></pre>

<p>but is much longer. How do I convert this into the album cover image. What libraries are need and how can it be done?</p>

<p>The string you see are the contents of an image file. You can just save the string directly to a file:</p>

<pre><code>$ file /tmp/file.mp3
/tmp/file.mp3: Audio file with ID3 version 2.3.0, contains: MPEG ADTS, layer III, v1, 128 kbps, 44.1 kHz, JntStereo
$ ipython
Python 2.7.12 (default, Aug  9 2016, 15:48:18) 
Type "copyright", "credits" or "license" for more information.

IPython 3.2.1 -- An enhanced Interactive Python.
?         -&gt; Introduction and overview of IPython's features.
%quickref -&gt; Quick reference.
help      -&gt; Python's own help system.
object?   -&gt; Details about 'object', use 'object??' for extra details.

In [1]: from tinytag import TinyTag

In [2]: tag = TinyTag.get("/tmp/file.mp3", image=True)

In [3]: image_data = tag.get_image()

In [4]: type(image_data)
Out[4]: str

In [5]: image_data[:20]
Out[5]: '\xff\xd8\xff\xe0\x00\x10JFIF\x00\x01\x01\x01\x00H\x00H\x00\x00'

In [6]: with open("/tmp/album_art.jpg", "w") as f:
   ...:     f.write(image_data)
   ...:   

In [7]: exit()
$ file /tmp/album_art.jpg
/tmp/album_art.jpg: JPEG image data, JFIF standard 1.01, resolution (DPI), density 72x72, segment length 16, Exif Standard: [TIFF image data, big-endian, direntries=7, orientation=upper-left, xresolution=98, yresolution=106, resolutionunit=2, software=Adobe Photoshop CS2 Windows, datetime=2008:04:02 00:23:04], baseline, precision 8, 320x300, frames 3
</code></pre>

<p>If you are using python3, you will have to explicitly specify 'wb' in the mode, since you get a stream of bytes instead:</p>

<pre><code>$ python3
Python 3.5.1 (default, Aug  9 2016, 15:35:51) 
[GCC 6.1.1 20160621 (Red Hat 6.1.1-3)] on linux
Type "help", "copyright", "credits" or "license" for more information.
&gt;&gt;&gt; from tinytag import TinyTag
&gt;&gt;&gt; tag = TinyTag.get("/tmp/file.mp3", image=True)
&gt;&gt;&gt; image_data = tag.get_image()
&gt;&gt;&gt; type(image_data)
&lt;class 'bytes'&gt;
&gt;&gt;&gt; image_data[:20]
b'\xff\xd8\xff\xe0\x00\x10JFIF\x00\x01\x01\x01\x00H\x00H\x00\x00'
&gt;&gt;&gt; with open("/tmp/album_art.jpg", "wb") as f:
...     f.write(image_data)
... 
64790
&gt;&gt;&gt; exit()
$ file /tmp/album_art.jpg
/tmp/album_art.jpg: JPEG image data, JFIF standard 1.01, resolution (DPI), density 72x72, segment length 16, Exif Standard: [TIFF image data, big-endian, direntries=7, orientation=upper-left, xresolution=98, yresolution=106, resolutionunit=2, software=Adobe Photoshop CS2 Windows, datetime=2008:04:02 00:23:04], baseline, precision 8, 320x300, frames 3
</code></pre>

python python-3.x music-player