
import imgkit
from PIL import Image

config = imgkit.config(wkhtmltoimage='C:\\Program Files\\wkhtmltopdf\\bin\\wkhtmltoimage.exe')

print("Generating html...")


print("Converting to image...")
imgkit.from_file('./test.html', 'in.png', 
                 config=config,
                 options={
    'format': 'png',
    'crop-h': '300',
    'crop-w': '400'
})

img = Image.open('./in.png')

# Resize image
w, h = img.size

h_new = 300
w_new = int((float(w) / h) * h_new)
w_cropped = 400
img = img.resize((w_new, h_new), resample=Image.LANCZOS)
x0 = (w_new - w_cropped) / 2
x1 = x0 + w_cropped
y0 = 0
y1 = h_new
img = img.crop((x0, y0, x1, y1))

pal_img = Image.new('P', (1,1))
pal_img.putpalette((255,255,255,0,0,0,0,0,0) + (0,0,0) * 252)

img = img.convert("RGB").quantize(palette=pal_img)
img.save('out.bmp')
print("Done!")
