Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to save base64 png image #489

Closed
dexception opened this issue Jul 8, 2020 · 5 comments
Closed

Unable to save base64 png image #489

dexception opened this issue Jul 8, 2020 · 5 comments
Labels
seeking-help upstream-issue Requires a functionality / behavior change outside of this python project

Comments

@dexception
Copy link

dexception commented Jul 8, 2020

I am using the latest wand github repo.

from wand.image import Image

with Image(filename='inline:data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAFoAAAAzCAYAAAAXUx+QAAAABHNCSVQICAgIfAhkiAAAAMpJREFUeJzt2jEOwjAQBVEMK6E07p2SgvvfgJKC60SyUplTZLbYeSf4mmJlKWmv975uutw9e0AVhoYYGmJoiKEhhoYYGtK+v4/vaECMMbI3lBC99+wNJcT23LI3lNDWmt5ogK8OiKEhhobEPGf2hhIiHpG9oQRfHRBvNMTQEENDDA0xNMTQEENDDA0xNMTQEENDDA0xNMTQEENDDA0xNMTQEENDDA0xNCTW8iM4IY55ZG8owf86IN5oiKEhhoYYGmJoiKEhhoYYGvIHUtEaWai7GEMAAAAASUVORK5CYII=') as img:
    img.save(filename='noseguy_b64.png')

Traceback (most recent call last):
  File "convert_base64_2_wand.py", line 3, in <module>
    with Image(filename='inline:data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAFoAAAAzCAYAAAAXUx+QAAAABHNCSVQICAgIfAhkiAAAAMpJREFUeJzt2jEOwjAQBVEMK6E07p2SgvvfgJKC60SyUplTZLbYeSf4mmJlKWmv975uutw9e0AVhoYYGmJoiKEhhoYYGtK+v4/vaECMMbI3lBC99+wNJcT23LI3lNDWmt5ogK8OiKEhhobEPGf2hhIiHpG9oQRfHRBvNMTQEENDDA0xNMTQEENDDA0xNMTQEENDDA0xNMTQEENDDA0xNMTQEENDDA0xNCTW8iM4IY55ZG8owf86IN5oiKEhhoYYGmJoiKEhhoYYGvIHUtEaWai7GEMAAAAASUVORK5CYII=') as img:
  File "/usr/local/lib/python3.6/dist-packages/Wand-0.6.2-py3.6.egg/wand/image.py", line 8555, in __init__
  File "/usr/local/lib/python3.6/dist-packages/Wand-0.6.2-py3.6.egg/wand/image.py", line 9030, in read
  File "/usr/local/lib/python3.6/dist-packages/Wand-0.6.2-py3.6.egg/wand/resource.py", line 230, in raise_exception
wand.exceptions.BlobError: unable to open image `data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAFoAAAAzCAYAAAAXUx+QAAAABHNCSVQICAgIfAhkiAAAAMpJREFUeJzt2jEOwjAQBVEMK6E07p2SgvvfgJKC60SyUplTZLbYeSf4mmJlKWmv975uutw9e0AVhoYYGmJoiKEhhoYYGtK+v4/vaECMMbI3lBC99+wNJcT23LI3lNDWmt5ogK8OiKEhhobEPGf2hhIiHpG9oQRfHRBvNMTQEENDDA0xNMTQEENDDA0xNMTQEENDDA0xNMTQEENDDA0xNMTQEENDDA0xNCTW8iM4IY55ZG8owf86IN5oiKEhhoYYGmJoiKEhhoYYGvIHUtEaWai7GEMAAAAASUVORK5CYII=': No such file or directory @ error/blob.c/OpenBlob/2701

@emcconville
Copy link
Owner

emcconville commented Jul 8, 2020

Odd. Working for me with the latest ImageMagick-6 & ImageMagick-7. Take a peak at the policy.xml file under /etc/ImageMagick-(6|7) to see if INLINE has been disabled, and post the output of python -mwand.version --verbose

@dexception
Copy link
Author

dexception commented Jul 9, 2020

Thanks for replying !

I am using python3.

Output:

Wand 0.6.2
ImageMagick 6.9.7-4 Q16 x86_64 20170114 http://www.imagemagick.org

I haven't changed anything in policy.xml. It is the default file that comes with ubuntu 18.04.

@emcconville
Copy link
Owner

emcconville commented Jul 9, 2020

I think you may be stuck with decoding the base64 image into bytes, and passing it wand as a blob.

from base64 import b64decode
from wand.image import Image

str_data = 'inline:data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAFoAAAAzCAYAAAAXUx+QAAAABHNCSVQICAgIfAhkiAAAAMpJREFUeJzt2jEOwjAQBVEMK6E07p2SgvvfgJKC60SyUplTZLbYeSf4mmJlKWmv975uutw9e0AVhoYYGmJoiKEhhoYYGtK+v4/vaECMMbI3lBC99+wNJcT23LI3lNDWmt5ogK8OiKEhhobEPGf2hhIiHpG9oQRfHRBvNMTQEENDDA0xNMTQEENDDA0xNMTQEENDDA0xNMTQEENDDA0xNMTQEENDDA0xNCTW8iM4IY55ZG8owf86IN5oiKEhhoYYGmJoiKEhhoYYGvIHUtEaWai7GEMAAAAASUVORK5CYII='
bin_data = b64decode(str_data[str_data.find(','):])

with Image(blob=bin_data, format='PNG') as img:
    img.save(filename='noseguy_b64.png')

From a quick google search, I can see that data:image/png;base64, URL part was developed back in 2015, and Ubuntu Bionic shipped ImageMagick-6 version from 2017. I would assume that Bionic's version of ImageMagick-6.9.7 includes URL support for INLINE. Try converting the base64 string directly in terminal with the convert command. Also note that older version had a character limit / bug that was also addressed between those two dates.

@emcconville emcconville added seeking-help upstream-issue Requires a functionality / behavior change outside of this python project labels Jul 9, 2020
@dexception
Copy link
Author

dexception commented Jul 10, 2020

Thanks for resolving the issue...!

I am also looking to convert the wand.image.Image back to base64.

@emcconville
Copy link
Owner

emcconville commented Jul 14, 2020

Closing this as I believe the issue has be fixed upstream. For converting back into base64, you would use the Image.make_blob() method.

from base64 import b64encode
from wand.image import Image

with Image(filename="icon.png") as img:
    b64img = "data:{0};base64,{1}".format(img.mimetype, str(b64encode(img.make_blob())))

.. or something close to that. I may make a Image.data_url() helper method in a future release.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
seeking-help upstream-issue Requires a functionality / behavior change outside of this python project
Projects
None yet
Development

No branches or pull requests

2 participants