An external application written in c++ requires me to pass in an image object it would understand i.e. with tostring() method, passing in a encoder and parameters.
How can I get the image and convert it to a string (without saving the image to file)?
image_url is the url to an actual image online i.e http://www.test.com/image1.jpg
This is what I have tried:
def pass_image(image_url):
import base64
str = base64.b64encode(image_url.read())
app = subprocess.Popen("externalApp",
in=subprocess.PIPE,
out=subprocess.PIPE)
app.in.write(str)
app.in.close()
I have also tried to open the image to convert it
image = Image.open(image_url)
but get the error
file() argument 1 must be encoded string without NULL bytes, not str
I think you can get a online image by using requests.get.
You code will be like this:
def pass_image(image_url):
import base64
import requests
str = base64.b64encode(requests.get(image_url).content)
Related
Can someone help me turn this base64 data into image? I don't know if it's because the data was not decoded properly or anything else. Here is how I decoded the data:
import base64
c_data = { the data in the link (string type) }
c_decoded = base64.b64decode(c_data)
But it gave the error Incorrect Padding so I followed some tutorials and tried different ways to decode the data.
c_decoded = base64.b64decode(c_data + '=' * (-len(c_data) % 4))
c_decoded = base64.b64decode(c_data + '=' * ((4 - len(c_data) % 4) % 4)
Both ways decoded the data without giving the error Incorrect Padding but now I can't turn the decoded data into image.
I have tried creating an empty png then write the decoded data into it:
from PIL import Image
with open('c.png', 'wb') as f:
f.write(c_decoded)
image = Image.open('c.png')
image.show()
It didn't work and gave the error: cannot identify image file 'c.png'
I have tried using BytesIO:
from PIL import Image
import io
from io import BytesIO
image = Image.open(io.BytesIO(c_decoded))
image.show()
Now it gave the error: cannot identify image file <_io.BytesIO object at 0x0000024082B20270>
Please help me.
Not sure if you definitely need a Python solution, or you just want help decoding your image like the first line of your question says, and you thought Python might be needed.
If the latter, you can just use ImageMagick in the Terminal:
cat YOURFILE.TXT | magick inline:- result.png
Or equivalently and avoiding "Useless Use of cat":
magick inline:- result.png < YOURFILE.TXT
If the former, you can use something like this (untested):
from urllib import request
with request.urlopen('data:image/png;base64,iVBORw0...') as response:
im = response.read()
Now im contains a PNG-encoded [^1] image, so you can either save to disk as such:
with open('result.png','wb') as f:
f.write(im)
Or, you can wrap it in a BytesIO and open into a PIL Image:
from io import BytesIO
from PIL import Image
pilImage = Image.open(BytesIO(im))
[^1]: Note that I have blindly assumed it is a PNG, it may be JPEG, so you should ideally look at the start of the DataURI to determine a suitable extension for saving your file.
Credit to #jps for explaining why my code didn't work. Check out #Mark Setchell solution for the reliable way of decoding base64 data (his code fixes my mistake that #jps pointed out)
So basically remove the [data:image/png;base64,] at the beginning of the base64 string because it is just the format of the data.
Change this:
c = "data:image/png;base64,iVBORw0KGgoAAAANSUh..."
to this:
c = "iVBORw0KGgoAAAANSUh..."
and now we can use
c_decoded = base64.b64decode(c)
I am trying create a Python dashboard in which a user uploads an image and then the image is analyzed. The uploaded image is received as a base64 string and it needs to be converted to an image. I have tried
decoded = BytesIO(base64.b64decode(base64_string))
image = Image.open(decoded)
but I received this error:
cannot identify image file <_io.BytesIO object at 0x00000268954E9888>
Image needs a file-like object, the sort of thing returned by an open. The easiest way to do this is using a with statement:
decoded = base64.b64decode(base64)
with BytesIO(decoded) as fh:
image = Image.open(fh)
# do stuff with image here: when the with block ends
# it's very likely the image will no longer be usable
See if that works better for you. :)
Google images have this weird path element, which when typed into a browser will directly bring you to the image (i.e.
data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAM4AAAD1CAMAAAAvfDqYAAAAflBMVEUAAAD////09PTa2tr7+/uqqqrW1tbs7OxoaGh1dXXn5+fKysrAwMBSUlKjo6Pf39+VlZWxsbG3t7c+Pj5LS0uampqEhISvr68eHh5DQ0PMzMxgYGBlZWUXFxczMzPv7+8nJyeMjIxxcXEwMDBYWFgaGhoQEBB+fn5OTk6IiIhJAEQBAAAI2UlEQVR4nNWd6XriOgyGlRAoSwoUCm3htCXTDszc/w2esmexJVnJ2Nb3H9vvg+NFmyGJT1nvqRg+w0DwU+h8MC21KT7grFzw67hw8iHctRA0EBHOeA0VvQjaiAZn8gw19QWtRIKzeK3DKMbJDTAAT4KWIsDpLU0wSpeCbGSGAZgIWguNk9tgQOM2+p+dBh4E7QXF6SEwIBpZSJw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E0+q7+JlYchWEGkCE4+iyITaNuWZpy/k9KUBxtRpAXAkdXtGu5EqU5YUlTXnkl/sOMo+kkavK+1aXoHvfJwNGzGnBeFtNjpKpVpLXmLiq5l9ZSEuypmKEHytK6Pmgrjobp1ogJRRJlFeT7NrJfsLzf6CszFI0ho8+nhh4uIcNbCGhWduQ2a0OqCJ5kHrUdxJRDiuPEbIM35r0QJQAiLiRuHC9V0SBa/685Q5Es0BDp52Mpc0jixPn5mF/fYeDEufvYBkvjxBhLhaYjUYouaNxe8oODE9uLcPVbgStO8js0QVlYCQYeTkx2XuyBFyZOTF4ftJ4EEyeeuyleyIiLE8tbFkiCshNOHIZr7CkhN5wYeCgaF5zw842YaY440tvP67B4mQzGD2n6OR7k/UJa4Y6uo+mGI3ADb6eDrNlOOpk7p6R80TVbXXGSzGUUX3NsdmS5kyEPr8AixOG7SnYrxtzYsL3kVK0sKQ5vQz3QBVYveuJ4+t6oQmZyHNrau6TLkZY1I/9wusL+VRKcpIfcGN7njElWV44lsRPPVVUkwvmZ9GaLyO+CPclqynLzZ7Tk/zNHCXF+1ux5/RK07Qv+l7J609qf9Dh1bVGM86Pxotgu97DfPY5WeUuUq9LBYlocRodimlsNAoj+B6EbdnLOwM4vAAAAAElFTkSuQmCC
brings you to a picture of the Apple company logo). Is there any way to download an image given this link?
Requests and requests_html cannot access this type of path.
this is the base64 encoding of the image. If you just take that as a string and save to disk after decoding base64, it will save correctly as a png.
from this answer:
# your string is
img_data = 'iVBORw0KGgoAAAANSUhEUgAAAM4AAAD1CAMAAAAvfDqYAAAAflB'...
# For both Python 2.7 and Python 3.x
import base64
with open("imageToSave.png", "wb") as fh:
fh.write(base64.decodebytes(img_data))
Convert string in base64 to image and save on filesystem in Python
Those letters and number ARE the image. Literally. It's just base64 encoded.
So one thing you could do is import the base64 library to convert that string into an image which you could then save to disk.
See https://stackoverflow.com/a/37767000/1831109
This is a code of a JPG/PNG(I don't know exactly)
Here's on google docs
I need to decode it in Python to complete image and show it using Pillow or something like that. Do you know any libraries or ways how to decode it? Thanks!
(for Python 3)
If the image is stored as a binary file, open it directly:
import PIL
# Create Image object
picture = PIL.Image.open('picture_code.dat')
#display image
picture.show()
# print whether JPEG, PNG, etc.
print(picture.format)
If the image is stored as hex in a plaintext file picture_code.dat similar to your Google Docs link, it needs to first be converted to binary data:
import binascii
import PIL
import io
# Open plaintext file with hex
picture_hex = open('picture_code.dat').read()
# Convert hex to binary data
picture_bytes = binascii.unhexlify(picture_hex)
# Convert bytes to stream (file-like object in memory)
picture_stream = io.BytesIO(picture_bytes)
# Create Image object
picture = PIL.Image.open(picture_stream)
#display image
picture.show()
# print whether JPEG, PNG, etc.
print(picture.format)
I'm trying to convert this string in base64 (http://pastebin.com/uz4ta0RL) to something usable by OpenCV.
Currently I am using this code (img_temp is the string) to use the OpenCV functions by converting it to a temp image(imagetosave.jpg) then loading the temp image with cv2.imread.
fh = open('imagetosave.jpg','wb')
fh.write(img_temp.decode('base64'))
fh.close()
img = cv2.imread('imagetosave.jpg',-1)
Though this method works, I would like to be able to use the OpenCV functions without using a temp image.
Help would be much appreciated, thanks!
To convert string from base64 to normal you can use
import base64
base64.b64decode("TGlrZSB0aGF0Cg==")
but it itsn't unclear if is what you need.
You can convert your decoded string to a list of integers, which you can then pass to the imdecode function:
img_array = map(int, img_temp.decode('base64'))
img = cv2.imdecode(img_array, -1)