Created
February 29, 2020 15:02
-
-
Save felipou/c187876bb4d575b87b21a9e780c70638 to your computer and use it in GitHub Desktop.
Convert image from URL to base64 and also prints the mime type
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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