Skip to content

Instantly share code, notes, and snippets.

@felipou
Created February 29, 2020 15:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save felipou/c187876bb4d575b87b21a9e780c70638 to your computer and use it in GitHub Desktop.
Save felipou/c187876bb4d575b87b21a9e780c70638 to your computer and use it in GitHub Desktop.
Convert image from URL to base64 and also prints the mime type
# Requires python-magic, which requires the native libmagic lib
# I installed both on macOS with:
# - pip3 install python-magic
# - port install libmagic
#
# To run:
# python3 tobase64mime.py http://imagedomain.com/file.jpeg
import sys
import magic
import urllib.request
from base64 import b64encode
response = urllib.request.urlopen(sys.argv[1])
data = response.read()
mime_type = magic.from_buffer(data, mime=True)
print(mime_type)
print(b64encode(data).decode('utf-8'))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment