Uploading images correctly using Python

dmizrahidmizrahi Member Posts: 2

I'm trying to upload images using python firstly by using a base64 encode of my image. The code is as following:

image_rb = open('dune-new.jpg', mode='rb').read()

image_base = base64.b64encode(image_rb)

API_url = 'https://api.webshopapp.com/nl/products/{product_id}/images.json'

payload = {'productImage[attachment]': image_base, 'productImage[filename]': 'dune-new.jpg'}

response = requests.post(API_url, data=payload, auth=(key, secret))

This is successful however I saved my jpg images with a quality of 80. If look at the size of that image before uploading and the image which I manually download from the shop after uploading, I see that the uploaded image is much larger in size than the original. If I try to open the uploaded image in GIMP and export it by using the settings from the image itself, it says the quality setting is 95 and not 80. What is happening here and how can I fix this? Am I encoding it in the wrong way?

Secondly I tried a different method:

image = open('dune-new.jpg', mode='rb')

image_path = 'C:\\Users\\comp\\Informatics\\dune-new.jpg'

image_name = 'dune-new.jpg'

API_url = 'https://api.webshopapp.com/nl/products/{product_id}/images.json'

headers = {'content-type': 'multipart/form-data'}

files = {'productImage[attachment]: (image_path, image), 'productImage[filename]': (None, image_name)}

response.requests(API_url, headers=headers, files=files, auth=(key, secret))

This second method doesn't work. I get 400 as a response from the server.


How can I upload an image using the API but keeping the image size as small as possible? We are trying to keep our shops as fast as possible and to maintain a small image size by properly compressing them is paramount but somehow images get larger when uploading.

Thank you for your help.

1 comment

  • LucienVersendaalLucienVersendaal Moderator, Lightspeed Staff Posts: 61 moderator

    Hi @dmizrahi ,

    Thank you for contacting us.

    The filename should be file itself. The same way that in html you use the <file name="filename>". https://developers.lightspeedhq.com/ecom/endpoints/productimage/#post-create-a-new-image

    Also the original size of the image can be big, doesn't really matter. Because if you are requesting with a smaller size using the url_image filter on your theme and passing the dimensions our CDN will resize it and make the image smaller.

    Using filename it will upload the original image, but if it's a jpg the quality will be always 95. But if you don't use the original tag on your theme the image will be always smaller on store to make the page load faster.


    I hope this will help.

    Kind regards,


    Lucien Versendaal

Sign In or Register to comment.