Working with Profile Pictures

David Moore
Analytics Vidhya
Published in
3 min readFeb 25, 2021

--

img src=”data”: how to read and save off a photo

Photo by Tanjir Ahmed Chowdhury on Unsplash

This past week, I worked on some Computer Vision items, and I came across a new arrangement. <img src=”data:image/png;base64,..>. Generally I was used to seeing <img src=”https….”>. After a little research, I understood this well and wanted to share what I learned with you. Naturally, I first tried to request the image URL, but that really didn’t work out. Let’s take a look at what I learned.

First, we pull in our Python modules.

import time
from selenium import webdriver
import requests
import shutil
from bs4 import BeautifulSoup
from selenium import webdriver
from base64 import decodestring, decodebytes

import os
from urllib.parse import urlparse

We need Selenium and BeautifulSoup as the main workhorses and base64 to decode that <img scr=’data:image/png..’>. You can see that I had requests and urlparse, but those won’t work for me and certainly will not work for you. Next we need to grab the page which contains the image(s) we are working on.

driver = webdriver.Chrome('/Applications/chromedriver')
driver.get(profile);
time.sleep(5) # Let the user actually see something!
soup = BeautifulSoup(driver.page_source, 'lxml')
picture = soup.find('div','user-photo-inner')
pic_url = picture.img['src']
a =…

--

--

Analytics Vidhya
Analytics Vidhya

Published in Analytics Vidhya

Analytics Vidhya is a community of Generative AI and Data Science professionals. We are building the next-gen data science ecosystem https://www.analyticsvidhya.com

David Moore
David Moore

Written by David Moore

xMotorola Finance, xIBM Finance, xDeloitte Tax Management Consulting — Digital Transformation, ESG, Analytics