Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert PIL Image.image object to base64 string? [duplicate]

I am trying to manipulate a base64 encoded image in such a way as to rotate it at 90 angle. After this manipulation, I want to convert it back to base64 string. But unfortunately unable to achieve this yet.

Here is what I have done so far:

image_string = StringIO(base64.b64decode(base64_string_here)) image = Image.open(image_string) angle = 90 rotated_image = image.rotate( angle, expand=1 ) 

Kindy help me how to convert this rotated_image to base64 string.

here's the dir() of rotated_image:

['_Image__transformer', '__doc__', '__getattr__', '__init__', '__module__', '__repr__', '_copy', '_dump', '_expand', '_makeself', '_new', 'category', 'convert', 'copy', 'crop', 'draft', 'filter', 'format', 'format_description', 'fromstring', 'getbands', 'getbbox', 'getcolors', 'getdata', 'getextrema', 'getim', 'getpalette', 'getpixel', 'getprojection', 'histogram', 'im', 'info', 'load', 'mode', 'offset', 'palette', 'paste', 'point', 'putalpha', 'putdata', 'putpalette', 'putpixel', 'quantize', 'readonly', 'resize', 'rotate', 'save', 'seek', 'show', 'size', 'split', 'tell', 'thumbnail', 'tobitmap', 'tostring', 'transform', 'transpose', 'verify']

like image 722
Hammad Qureshi Avatar asked Aug 05 '15 07:08

Hammad Qureshi


People also ask

Can we convert image to Base64?

Convert Images to Base64Just select your JPG, PNG, GIF, Webp, or BMP picture or drag & drop it in the form below, press the Convert to Base64 button, and you'll get a base-64 string of the image. Press a button – get base64. No ads, nonsense, or garbage. Drag and drop your image here!

How can I convert an image into Base64 string using Python?

To convert the Image to Base64 String in Python, we have to use the Python base64 module that provides b64encode() method. Python base64. b64encode() function encodes a string using Base64 and returns that string.

How can I convert an image into Base64 string using Java?

Convert Image File to Base64 Stringbyte[] fileContent = FileUtils. readFileToByteArray(new File(filePath)); String encodedString = Base64. getEncoder(). encodeToString(fileContent);

How to convert image to Base64 code in Python?

encode it in base64 using the base64 module in Python. Print the string. Here we will take an example image to show you how to do this. Now we will convert this image to its base64 code using the below Python Program: import base64 with open ("my_image.jpg", "rb") as img_file: my_string = base64.b64encode (img_file.read ()) print (my_string)

How do I convert an image to a string in Python?

Convert Image to Base64 String in Python To convert the Image to Base64 String in Python, we have to use the Python base64 module that provides b64encode () method. Python base64.b64encode () function encodes a string using Base64 and returns that string.

What is PiL image in Python?

Python PIL | Image.convert () Method Last Updated : 26 Jul, 2019 PIL is the Python Imaging Library which provides the python interpreter with image editing capabilities. The Image module provides a class with the same name which is used to represent a PIL image.

What is a base64-encoded url?

In fact, it is a data URL, but it contains the Base64-encoded image: This can only be useful if you don’t need the original Base64 string or original image type. Be aware that this method may return different results for the same image depending on the browser and operating system.


1 Answers

Python 3

import base64 from io import BytesIO  buffered = BytesIO() image.save(buffered, format="JPEG") img_str = base64.b64encode(buffered.getvalue()) 

Python 2

import base64 import cStringIO  buffer = cStringIO.StringIO() image.save(buffer, format="JPEG") img_str = base64.b64encode(buffer.getvalue()) 
like image 89
Eugene V Avatar answered Oct 09 '22 15:10

Eugene V