I am trying to convert a list of strings to byte64 format so I can decode them and download them as Images. The byte64 format images are stored in file.txt and a snippet of the file is shown below. My attempt only does this for a single byte64 format string, how should I do this for a file containing multiple lines?
snippet from file.txt:
data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/...
data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/...
Here's what I've tried to do:
# read lines from file
f = open("file.txt", "r")
for x in f:
data = f.readlines()
f.close()
# Removing unecessary substrings
images = [w.replace('\n', '') for w in data]
images = [w.replace('data:image/jpeg;base64,', '') for w in images]
# encode string to byte64 format
test = images[1].encode()
# Convert to image
with open("image_1.png", "wb") as fh:
fh.write(base64.decodebytes(test))