I keep getting an IO Error stating my directory doesn't exist. What am I doing wrong?
I have this in a separate file named pirate.py :
with open("/images/image.jpg", "rb") as fin:
image_data = fin.read()
with open("pirate.py","wb") as fout:
fout.write("image_data="+repr(image_data))
And then the code in my main file
from pirate import image_data
# Content-type declaration
print('Content-type: text/html\n')
def main():
print('<!doctype html><head><meta charset="utf-8">')
print('<style>html {background:url (data:image/gif;base64,' + pirate.image_data + ')
I'm trying to encode an image in base64 and then use it as a background in a .cgi, I'm sure the rest of my code is working, what am I doing wrong here?
try to use absolute file paths. or use root prefix ROOT_PATH = os.path.dirname(os.path.realpath( file ))
repr is not what you want, you are saying the image data is base64 ... so make it base 64
with open("pirate.py","wb") as fout:
fout.write("image_data="+base64.b64encode(image_data))
then in addition you say
from pirate import image_data
then you reference it as
pirate.image_data
when you should just reference it as
image_data
theres probably some other probelems since the image image path you show is not the image path from your comment
Your code says
open("/images/image.jpg", "rb")
I suspect the "/" at the beginning is the problem. Or is your images folder in your root?
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.