lundi 7 novembre 2016

Pull random image from directory, encode to base64 and then print

I'm having difficult time trying to work these two together. It's frustrating me a little, so I hope to find ideas/solutions.

The complete works (what I'm planning on) should grab a random image from an online directory, encode it to base64 then print the base64. I've had total curl madness going all day and now I'm turning to python. Onwards!

These are kinda just notes at the minute but should explain the process.

import random, os
import base64

def search(): #get file
    path = r"/Users/Impshum/Pictures" #should be able to http
    random_filename = random.choice([
        x for x in os.listdir(path)
        if os.path.isfile(os.path.join(path, x))
    ])
    print(random_filename) #not printing full location


def encode(): #encode to base64
    image = open('heaven.jpg', 'rb')
    image_read = image.read()
    image_64_encode = base64.encodestring(image_read)
    print image_64_encode

search() #notes
encode() #notes

Many thanks in advance.




Aucun commentaire:

Enregistrer un commentaire