Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Calculate size automaticly #91

Open
QuasyStellar opened this issue Dec 15, 2022 · 2 comments
Open

Calculate size automaticly #91

QuasyStellar opened this issue Dec 15, 2022 · 2 comments

Comments

@QuasyStellar
Copy link

Add a function that calculate the size of image automaticly.

@vgalin
Copy link
Owner

vgalin commented Jun 15, 2023

From the readme:

Can I automatically take a full page screenshot?
Sadly no, it is not easily possible. Html2Image relies on the headless mode of Chrome/Chromium browsers to take screenshots and there is no way to "ask" for a full page screenshot at the moment. If you know a way to take one (by estimating the page size for example) I would be happy to see it, so please open an issue or a discussion!

@vgalin vgalin closed this as completed Jun 15, 2023
@vgalin vgalin reopened this Jun 15, 2023
@tsbertalan
Copy link

tsbertalan commented Aug 22, 2023

One way is to save it too-big, and then find the bbox for nonzero content with PIL.Image.Image.getbbox:

save_dir = os.path.dirname(png_path)
save_name = os.path.basename(png_path)
hti = html2image.Html2Image(output_path=save_dir, size=(2000, 10000))
hti.screenshot(html_str=html, save_as=save_name)

# open the PNG again, and crop it to the content.
from PIL import Image
img = Image.open(png_path)

# Get the content bounds.
content = img.getbbox()

# Crop the image.
img = img.crop(content)

# Save the image.
img.save(png_path)

This might only work with very simple web pages, however, like the output of data structure visualizers (in my case, pyparsing.core.ParserElement.create_diagram).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants