top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

How to convert Base 64 string Binary data to PNG in Python

+2 votes
1,512 views
How to convert Base 64 string Binary data to PNG in Python
posted Dec 29, 2014 by Avinash Ks

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

0 votes
 
Best answer

Try something like this

f = open("myfile.png", "wb")
f.write(imgData.decode('base64'))
f.close()
answer Dec 29, 2014 by Salil Agrawal
suppose we have base 64 string of jpg image, for every format I need to convert base 64 string to PNG
cant say about the string but there is a work around, try python image library http://www.pythonware.com/products/pil/
 
import Image

im = Image.open('myfile.jpg')
im.save('myfile.png')

Now to work with string you can first save it as jpg and then open and save as png again and delete the jpg file. (though there can be more simple ways)
Base 64 string is any format(jpg,png,jpeg,gif) the output of the image should be in PNG
Thats what I answered first save it in the original format and then reopen using PIL and save it in the format whatever you want.
I am not directly converting images,i am converting image into Base 64 string then displaying in PNG format(base 64 string data is any format of images like png,jpg,gif) these string i have to convert in PNG format.
not able to understand :( the comment please be clear and precise.
in my case am able convert only same format image to base 64(suppose my image in jpg format if I converted this image into base 64, I am able convert this string to jpg not png image).
Thats what I answered you need you need PIL, read my second comment in detail and try out what I explained.
Similar Questions
+1 vote
<audio src="data:audio/mp3;base64,//MkxAA.......></audio>

The above code is working fine in web browsers but not working in mobile browsers.Please help any one what is the problem.

0 votes

I use: Python 2.6 and sqlalchemy 0.6.1

This is what I am trying to do:

 from sqlalchemy.types import (
 Integer,
 String,
 Boolean
 )
 from sqlalchemy.ext.declarative import declarative_base

 Base = declarative_base()

 class SampleMeta(type):
 def __new__(cls, name, bases, attrs):
 attrs.update({ 'id': Column('Id', Integer, primary_key=True),
 'name': Column('Name', String),
 'description': Column('Description', String),
 'is_active': Column('IsActive', Boolean)
 })
 return super(SampleMeta, cls).__new__(cls, name, bases, attrs)

 class Sample(Base):
 __tablename__ = 'Sample'
 __table_args__ = {'useexisting': True}
 __metaclass__ = SampleMeta

 def __init__(self, id, name, description, is_active):
 self.id = id
 self.name = name
 self.description = description
 self.is_active = is_active

 def __repr__(self):
 return "" % (self.id, self.name, self.description, self.isactive)

And the error I am getting is this:

 TypeError: Error when calling the metaclass bases
 metaclass conflict: the metaclass of a derived class must be a (non-strict) subclass of the metaclasses of all its bases

Now, if I do the same thing above by using

 class Sample(object)

instead of

 class Sample(Base)

it works absolutely fine.

I need to update the attributes of the class dynamically. So, I will be using dynamic attribute and column names. And I need the above piece code to work in order to be able to get there.

+2 votes

I want to install Python on a PC with the 64 bit version of Windows 7. I want Python to be able to use as much as possible of the RAM.

When I install the 64 bit version of Python I find that sys.maxint == 2**31 - 1 Whereas the Pythpon installed on my 64 bit linux system returns sys.maxint == 2**63 - 1.

It looks to me as though 32 and 64 bit versions of Python on 64 bit Windows are both really 32 bit Python, differing only in how they interact with Windows. So I wouldnt expect 64 bit Python running on 64 bit Windows to allow the large data struictures I could have with 64 bit Python running on 64 bit linux.

Is that true?I have spent a couple of hours searching for a definitive description of the difference between the 32 and 64 bit versions of Python for Windows and haven't found anything.

+1 vote

I need to get 32 bit binary equivalent of a decimal and need to change the 0's to 1's and 1's to 0's

For Example
if the input is 2 
Output should be:
the 32bit equivalent of 2 :0000 0000 0000 0000 0000 0000 0000 0010
and the 1's compliment is:1111 1111 1111 1111 1111 1111 1111 1101

is there any pre-defined function to get the above results in python??

...