Skip to content

Instantly share code, notes, and snippets.

@alexjyong
Last active April 14, 2023 18:59
Show Gist options
  • Save alexjyong/dd54aab7c7596d2e45a5f9a348ff6df4 to your computer and use it in GitHub Desktop.
Save alexjyong/dd54aab7c7596d2e45a5f9a348ff6df4 to your computer and use it in GitHub Desktop.
Converts an html page with image links to an html page with base64 links in python
import sys
import base64
import requests
from bs4 import BeautifulSoup
def convert_images_to_base64(input_file, output_file):
with open(input_file, 'r', encoding='utf-8') as f:
html_content = f.read()
soup = BeautifulSoup(html_content, 'html.parser')
for img in soup.find_all('img'):
src = img['src']
if src.startswith('http'):
response = requests.get(src)
img_data = response.content
else:
# Handle local image paths
with open(src, 'rb') as f:
img_data = f.read()
img_b64 = base64.b64encode(img_data).decode()
img['src'] = 'data:image/png;base64,' + img_b64
# Prettify the HTML output
pretty_html = soup.prettify()
with open(output_file, 'w', encoding='utf-8') as f:
f.write(pretty_html)
if __name__ == '__main__':
if len(sys.argv) <= 2:
print("Usage: python3 doStuff.py input_file output_file")
sys.exit(1)
output_file = None
input_file = sys.argv[1]
if len(sys.argv) >= 3:
output_file = sys.argv[2]
if output_file is None:
output_file = "pretty_index.html"
# Example usage:
convert_images_to_base64(input_file, output_file)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment