Python Forum
wkhtmltoimage generated jpeg save to Database using mongoengine - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Web Scraping & Web Development (https://python-forum.io/forum-13.html)
+--- Thread: wkhtmltoimage generated jpeg save to Database using mongoengine (/thread-29104.html)



wkhtmltoimage generated jpeg save to Database using mongoengine - Madoo - Aug-18-2020

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?


RE: wkhtmltoimage generated jpeg save to Database using mongoengine - Cryptus - Aug-18-2020

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


RE: wkhtmltoimage generated jpeg save to Database using mongoengine - Madoo - Aug-18-2020

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