0

I have a feature with my application where a user can upload a photo. I would like to convert the photo into a JPG file and then upload it to my servers.

The photo is received in base64. I've seen other answers which suggest using PIL however, it saves the image to a local directory. Instead I would like to convert the received image into a base64 JPG image.

How would I do this? Thanks.

Community
  • 1
  • 1
Pav Sidhu
  • 6,724
  • 18
  • 55
  • 110
  • I don't quite understand, you have base64 of a jpg file and you want to get the jpg file? Just decode it data.decode('base64') – DorElias Sep 13 '15 at 18:02
  • Sorry, I ment that I have a base64 of any image file, for example a .png and I would like to convert it into base64 .jpg. – Pav Sidhu Sep 13 '15 at 18:40

1 Answers1

0

try this:

import base64
from PIL import Image
from io import BytesIO

im = Image.open(BytesIO(base64.b64decode(data)))
output = BytesIO()
im.save(output, 'JPEG')
jpg_img = output.read()

it will save the data to a stream ( the same way tou enter it) and then you can read from that stream

DorElias
  • 2,243
  • 15
  • 18