Year of the Earth Horse

Another short post, as Wednesdays are my longest days. I made it to Chapter 3 of Obey The Testing Goat. Slow and steady wins the race.

I’ve also had some interesting adventures with converting images into text strings over the past couple of weeks. Got some of that finished today. That’ll probably be a blog post at some point. Part of the final code looks KINDA like this, but I’ve spread this code out to make it clearer to understand.

def get_jpeg_screenshot_as_base64_string(self):
    '''
    Convert JPEG version of screenshot to Base64 string
    :return: 64 bit encoded ascii string
    '''
    # This function returns the path of a saved JPEG screenshot
    jpg_screenshot = get_screenshot_as_jpeg()

    with open(jpg_screenshot, mode="rb") as jpg_image:
        # reading from an image gives a byte string of hex nums?
        byte_string_hex = jpg_image.read()

    # extra insurance, making sure I got the right data type
    assert isinstance(byte_string_hex, bytes)

    # converts to byte string of Base64 encoded ascii
    byte_string_64 = base64.b64encode(byte_string_hex)

    # converts to standard string of Base64 encoded ascii 
    ascii_string_64 = byte_string_64.decode('ascii')

    # Return the compressed version
    return base64.b64encode(jpg_image.read()).decode('ascii')

I’ll probably talk more about this another time. NOW it’s time for bed.