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+SrOTNBkQ54DSwFDSZjicb5wGVpJGg+G8ETSwkbQaCseydZaUSZoNhNM4bza0FLUbBmdL0sg+nTA4a5oGeqKWQ+A8MWj2sqYD4MwYNDCXtR0Ah0MDY2Hb3Q6VIcYyAPAsbNw7zoL150iMUkf5xklZNMKFwD8OdVI7S2KTOskzzoRFIx+UZ5w9i0Zi9DjLL86URfMl78ArTsabahKbx0VeceYsGtE19CKvOLw/R3TRufbQ2VhprVg08nUg8YvDovmvXRcdDZUhxOBZUrs+POI8cmhkt7ab/OF8cmgkPp2y/OFwttBfbTvxh0Ma1gDeWnfiDYdxM3ht34s3HMa1LW3fizccq5Ptps8OevGG85uikXinGvKFQ346ndB4w9ngMO8dfDdH+cLBPW3LNqfosnzhoGbpNjecqnzhYMbCaXfd+ML5sNO0PHVW5AvHCtP6mFbtptPWkH4skkSqYN1025y9H6OKzrvpukFbPwaNujjW1LrpvEVLPw2tuzkH1Lr5B20a+6nqo+2109bNv2m2oV2J5XEq9K3R8oVztnt8bNcvXW4zDbXASce9zSTf9Lr7BrLBYj76/no/ku+eh3+nec/xMC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5/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
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 to scrape image from en eCommerce website using scrapy, but for some of the items(5-10 out of 180) image src output is similar to this -
data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNkYAAAAAYAAjCB0C8A .
For the rest of the items I get the correct image URL.
Can someone help me with this.
My code is for image src extraction is
image = response.css('.productimage img::attr(src)').extract()
And due to this I am getting an error while downloading the image to my local system.
This
data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNkYAAAAAYAAjCB0C8A
actually is image - bytes encoded to string using base64, you might use base64 built-in module to get it as file. Consider following simple example:
import base64
txt = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNkYAAAAAYAAjCB0C8A"
content = base64.b64decode(txt.split(',')[-1])
with open('image.png','wb') as f:
f.write(content)
it will create image.png file in current working directory.
This base64 data:
iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNkYAAAAAYAAjCB0C8A
It is empty png image (it is not relevat image)
Usually this base64 data occure on e-commerce websites for products which don't have images.
I recommend You to interpret this products with base64.... as products without image.
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 playing around in python trying to download some images from imgur. I've been using the urrlib and urllib.retrieve but you need to specify the extension when saving the file. This isn't a problem for most posts since the link has for example .jpg in it, but I'm not sure what to do when the extension isn't there. My question is if there is any way to determine the image format of the file before downloading it. The question is mostly imgur specific, but I wouldn't mind a solution for most image-hosting sites.
Thanks in advance
You can use imghdr.what(filename[, h]) in Python 2.7 and Python 3 to determine the image type.
Read here for more info, if you're using Python 2.7.
Read here for more info, if you're using Python 3.
Assuming the picture has no file extension, there's no way to determine which type it is before you download it. All image formats sets their initial bytes to a particular value. To inspect these 'magic' initial bytes check out https://github.com/ahupp/python-magic - it matches the initial bytes against known image formats.
The code below downloads a picture from imgur and determines which file type it is.
import magic
import requests
import shutil
r = requests.get('http://i.imgur.com/yed5Zfk.gif', stream=True) ##Download picture
if r.status_code == 200:
with open('~/Desktop/picture', 'wb') as f:
r.raw.decode_content = True
shutil.copyfileobj(r.raw, f)
print magic.from_file('~/Desktop/picture') ##Determine type
## Prints: 'GIF image data, version 89a, 360 x 270'
Instead of using directories to reference an image, is it possible to code an image into the program directly?
You can use the base64 module to embed data into your programs. From the base64 documentation:
>>> import base64
>>> encoded = base64.b64encode('data to be encoded')
>>> encoded
'ZGF0YSB0byBiZSBlbmNvZGVk'
>>> data = base64.b64decode(encoded)
>>> data
'data to be encoded'
Using this ability you can base64 encode an image and embed the resulting string in your program. To get the original image data you would pass that string to base64.b64decode.
Try img2py script. It's included as part of wxpython (google to see if you can dl seperately).
img2py.py -- Convert an image to PNG format and embed it in a Python
module with appropriate code so it can be loaded into a program at runtime. The benefit is that since it is Python source code it can be delivered as a .pyc or 'compiled' into the program using freeze, py2exe, etc.
Usage:
img2py.py [options] image_file python_file
There is no need to base64 encode the string, just paste it's repr into the code
If you mean, storing the bytes that represent the image in the program code itself, you could do it by base64 encoding the image file, and setting a variable to that string.
You could also declare a byte array, where the contents of the array are the bytes that represent the image.
In both cases, if you want to operate on the image, you may need to decode the value that you have included in your source code.
Warning: you may be treading on a performance minefield here.
A better way might be to store the image/s in the directory structure of your module, and the loading it on demand (even caching it). You could write a generalized method/function that loads the right image based on some identifier which maps to the particular image file name that is part and parcel of your module.