Get original image url from base64 Image String using Python? - python

Wondering can someone point me in the right direction on how to convert base64 image string to its original image URL.
My code is scraping top 5 news from google based on my search string.
Images are in one big massive base64 string. Images are printing ok on my outlook email (my code extract the news and send out an email in outlook) but when I forward that email on to different email account can't see any image but a message The linked image cannot be displayed. The file may have been moved, renamed or deleted. Just to check that, I copied the image from my outlook email and tried to paste on word document; all I can see is an empty box but no image.
any advice, please?

You can't get a URL from those. Those base64 encoded strings are fully embedded images. You could base64 decode them and save it to a file or just take the base64 encoded string an attach it to another image tag like in the incoming email.
If you have some specific code I could be of more help.

Related

embedding images in multipart html email - defining the linked images correctly

I have difficulties in embedding images in a multipart email.
I am trying to send out a html file with plenty of embedded images as mail. However, the images do not appear, and are currently just sent as attachments.
I assume I don't manage to link the html code.
Here is part of the HTML code for the first image
</v:shapetype><v:shape id="Picture_x0020_5" o:spid="_x0000_i1029" type="#_x0000_t75"
alt="cid:image001.png#01D58F16.6A9DB2F0" style='width:441.45pt;height:183.85pt;
visibility:visible;mso-wrap-style:square'>
<v:imagedata src="somefolder-data/image001.png"
o:title="image001.png#01D58F16"/>
The images are not placed in the working directory itself.
I believe the issue lies in defining the image's ID as referenced above in the HTML, which looks different from online examples. I have tried a few versions, but haven't had success.
I assumed the part following cid would be relevant (so image001.png), but it might be "image001.png#01D58F16.6A9DB2F0".
Can someone help with to create the right connection here?
msgImage = MIMEImage(fp.read())
fp.close()
msgImage.add_header('Content-Disposition', 'inline', filename='image001.png')
# Attach part into message container.
msg.attach(msgImage)
Thanks in advance

Get image path from src="cid:image.png..." resource

I'm trying to parse e-mail, and the links in the img tags have an unusual format. I'm not strong in regular expressions. I would be glad to hear your suggestions how to get a normal link from this :
src="cid:image006.png#01D4225D.4CE86AB0"
You can't get the image link from that tag, because CID actually references the images in the email that you received. This image has been attached to your email.

Is there a way to pull inline images from gmail using python?

Is there a way to pull inline images from an email within gmail to save somewhere? If not, is there a way to get the image url of the inline image?
Would IMAP or POP support this?
I've been able to pull an email via IMAP, but I can't find any trace of the inline images within the email, unless the image has been converted to strings of letters and numbers. I did a search for the image url, and couldn't find that in the resulting string either, so I'm not sure if it's possible to pull inline images from gmail.
If you can successfully pull the mail from gmail via POP3 or IMAP. Then you'll find the image maybe encoded into BASE64 string.
All you need to do is parse the image part and decode it to binary.
The following maybe useful:
MIME
Email in python

How does Facebook share image base64 from Google App Engine

Im working with Google App Engine Project and I want use facebook share like this.
http://i.stack.imgur.com/uz52n.png
Im already read this
How does Facebook Sharer select Images and other metadata when sharing my URL?
but GAE cant upload physical Image, all image store in blob property in database as base64 so facebook share cant get the image :(
anyone had another idea for this problem ??
Facebook reads the og:image meta to resolve the image from your webpage. og:image don't allow data-URI image (base64 encoded).
You have to provide an image url in og:image, but with that url, you can make a workaround to simulate the behaviour of a direct image resolution and get the image from your appengine database.
This is a solution in python using Django, but the concept works for everything. The name of the image is here "key.png" where key is the key of the object containing the base64 stored image.
First, add an url to the list of django urls for your image resolution:
(r'^image/(?P<key>[^\.^/]+)\.png$', 'yourapp.views.image'),
Then in your views, get the key from the url, retrieve your object, base64 decode and send it back with the correct mimetype:
import base64
def image(request, key):
# get your object from database
f = YourImageObject.get(key)
# f.pic is the base64 encoded image
pic = f.pic[len("data:image/png;base64,"):] # remove the header
# base64 decode and respond with correct mimetype
return HttpResponse(base64.b64decode(pic), mimetype="image/png")

Uploading profile image with twitter api and python

I want to upload an image from my hard drive, using an html form:
Image file: <input name="imageupload" id="imageupload" type="file" />
Then I upload it to twitter with:
image=self.request.get('imageupload')
image2=base64.b64encode(image)
twitapi.Update_profile_image(image=image2)
given twitapi.Update_profile_image:
def Update_profile_image(self,image):
if not self._oauth_consumer:
raise TwitterError("The twitter.Api instance must be authenticated.")
url = '%s/account/update_profile_image.json' % (self.base_url)
data = {'image':image}
json = self._FetchUrl(url, post_data=data)
data = self._ParseAndCheckTwitter(json)
return data
Given _FetchUrl from twitter-api
I always get
TwitterError: There was a problem with your picture. Probably too big.
Any ideas whee it comes from? Thanks!
To submit ah image correctly via a form, you have to include
enctype="multipart/form-data"
eg
<form enctype="multipart/form-data" action='/' method="POST">
Twitter RESTful API Document is not correct.
Do NOT encode image binary to base64!
Remove base64 encode section from your source.
If you encode image binary to base64 string, twitter api says
"... was a problem with your picture probably too big. (...) (code 131)"
As per the documentation, your image:
Must be a valid GIF, JPG, or PNG image of less than 700 kilobytes in size.
So make sure your image fits within these constraints. Maybe you need to scale down your image, or convert it to a different format.
If that doesn't work, try uploading another very tiny image that meets the constraints above. At least you can then verify whether or not the problem lays with the particular image you are using.
Perhaps the image you are receiving via the form upload is already base64 encoded ?
You are then applying a double encoding which could confuse the validation on the twitter server side because it would be unable to find a typical image header in your uploaded file.

Categories