Sample code for 30+ languages & platforms
Chilkat2-Python

Download Image (JPG, GIF, etc.) to Base64

See more HTTP Examples

Demonstrates how to download an image, or any type of file, to get the data in base64 encoding format.

Chilkat Chilkat2-Python Downloads

Chilkat2-Python
import chilkat2

success = False

# This example requires the Chilkat API to have been previously unlocked.
# See Global Unlock Sample for sample code.

http = chilkat2.Http()
http.KeepResponseBody = True

bd = chilkat2.BinData()

success = http.DownloadBd("https://www.chilkatsoft.com/images/starfish.jpg",bd)
statusCode = http.LastStatus
if (success == False):
    if (statusCode == 0):
        # Unable to either send the request or get the response.
        print(http.LastErrorText)
    else:
        # We got a response, but the status code was not in the 200s
        print("Response status code: " + str(statusCode))
        # Examine the response body.
        print("Response body:")
        print(http.LastResponseBody)

    print("Download failed.")

else:
    print("Download success, response status = " + str(statusCode))

    base64_image_data = bd.GetEncoded("base64")
    print("image data in base64 format:")
    print(base64_image_data)