Python Forum

Full Version: wkhtmltoimage generated jpeg save to Database using mongoengine
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I want to generated image from html to jpeg. So I'm using imgkit and try to generated image. Image is generated correctly. According to imgkit documentation I tried to read image to variable and save it to mongodb collection using mongoengine. This code I used to read it to variable
# Use False instead of output path to save pdf to a variable
img = imgkit.from_url('http://example.com', False)
And this is the way that I tried to store it in db collection.
class Page(Document):
    image = ImageField()
        path_wkthmltoimage = r'C:/Program Files/wkhtmltopdf/bin/wkhtmltoimage.exe'
        config = imgkit.config(wkhtmltoimage=path_wkthmltoimage)
        options = {
            'format': 'jpeg',
            'quality': '40'
        }
        img = imgkit.from_url('http://example.com', False, config=config, options=options)
        myimage = open(img, 'rb')
            
Page.image.put(myimage)
Page.save()
But It gives this error
Error:
myimage = open(img, 'rb') UnicodeDecodeError: 'utf-8' codec can't decode byte 0xff in position 0: invalid start byte
How I store that image in db collection?
make sure to install https://wkhtmltopdf.org/downloads.html

and check if the path_imgkit is working

import imgkit

path_imgkit = r'C:\Program Files\wkhtmltopdf\bin\wkhtmltoimage.exe'
config = imgkit.config(wkhtmltoimage=path_imgkit)
imgkit.from_url('http://google.com', 'out.jpg', config=config)
the image will save itself in the root directory
I already installed https://wkhtmltopdf.org/downloads.html. When I try to generated image save to local using this code it generated image and store it in parent folder.
imgkit.from_url('http://google.com', 'out.jpg', config=config)

But I want save it in db collection