Correctly encode an image for eCom with Python

aaronaaron Posts: 31Member
So I made the following script:
    def _image(self, image):

        with open(image.path.path, "rb") as image_file:
            base_64_image = base64.b64encode(image_file.read())

            data = '{"productIamge": {"attachment": "' + str(base_64_image) + '", "filename": "' + image.caption + '"}}'

            return data
When I send it to the eCom api it returns a Internal Server error. Is this the correct way to encode an image for the API?
Tags:

1 comment

  • aaronaaron Posts: 31Member
    I found the solution, this did the trick:

    image_file = open(image.path.path, "rb")
    image_read = image_file.read()

    base_64_image = base64.b64encode(image_read).decode('utf-8')

    return {
    "productImage": {
    "attachment": base_64_image,
    "filename": image.caption
    }
    }
Sign In or Register to comment.