1

I am using the DALLE API to generate images, and taking that file and uploading it to spotify as a playlist cover. However I receive a 413 error when I run the following line:

sp.playlist_upload_cover_image(playlist_id=playlist_id, image_b64=image)

where 'image' is my DALLE generated image in base-64 format.

image = openai.Image.create(
  prompt=string_to_upload,
  n=1,
  size="256x256",
  response_format="b64_json"
)

image = image['data'][0]['b64_json']

Here is the error message:

requests.exceptions.HTTPError: 413 Client Error: Request Entity Too Large for url: https://api.spotify.com/v1/playlists/68jf42L1vcopcrBPZkmmre/images

I believe that the b64 file that I receive from the DALLE API is slighty larger than the max file size for the spotipy upload (256 KB). I can tell because if I download the image as a png and convert it to b64 online, it says the file is about 262 KB. Is there any way I can make the b64 file from DALLE slightly smaller?

  • See also [python - Is it possible to change playlist image using Spotipy?](https://stackoverflow.com/questions/67351225/is-it-possible-to-change-playlist-image-using-spotipy): MIME-type must be JPEG, max-size 256 KB. – hc_dev Apr 05 '23 at 18:16
  • This are actually 2 questions as duplicates: (1) [png to jpeg with python](https://stackoverflow.com/questions/43258461/convert-png-to-jpeg-using-pillow) with optional [compression/reduce-size](https://stackoverflow.com/questions/10607468/how-to-reduce-the-image-file-size-using-pil) and (2) [encoding image file to base64](https://stackoverflow.com/questions/3715493/encoding-an-image-file-with-base64/3715530#3715530). – hc_dev Apr 05 '23 at 18:22
  • Does this answer your question? [Convert png to jpeg using Pillow](https://stackoverflow.com/questions/43258461/convert-png-to-jpeg-using-pillow) – miken32 Apr 25 '23 at 15:31

2 Answers2

1

Off course there is a way to make the b64 from DALLE smaller by compressing it. You can try the PIL library with Python. CHeck the following example where we can use the PIL library for resizing and compressing it before converting it to base64.

import io
from PIL import Image

# convert base64 image to bytes
image_bytes = io.BytesIO(base64.b64decode(image))

# open image with PIL
pil_image = Image.open(image_bytes)

# this is to resize and compress image
max_size = 256
quality = 80  # adjust quality to get smaller file size
pil_image = pil_image.resize((max_size, max_size))
pil_image.save(image_bytes, format='JPEG', quality=quality)

# convert compressed image to base64
compressed_image = base64.b64encode(image_bytes.getvalue()).decode('utf-8')
N Miaoulis
  • 83
  • 8
0

I'm not sure why N Miaolis's code was not working for my case. I guess saving the new lower quality image in the same BytesIO object did not work for me

I created this method where the image_format is PNG or JPG and the picture.image is a bytes object of the base64 encoded image e.g. b'iVBORw0KGgoAAAA....'

The max_size parameter is used to set the dimensions squared and as for the quality I do not recommend going below 70 as far too few details can be noticed.

def compress_image(picture, quality=70, max_size=256):
    image_bytes = io.BytesIO(base64.b64decode(picture.image))
    image_format = parse_image_format(picture.image_format)

    pil_image = Image.open(image_bytes)
    pil_image = pil_image.resize((max_size, max_size))

    output_buffer = io.BytesIO()
    pil_image.save(output_buffer, format=image_format, quality=quality)
    compressed_image = output_buffer.getvalue()
    picture.image = base64.b64encode(compressed_image)