Python Convert HTML Image to PNG - html

Given a saved HTML file with an image (an output from Bokeh), how can I save it as a PNG file with a DPI of 300?
I found some answers to similar questions, but they don't seem to work for me. I think I need someone to explain the whole process (importing the needed package, where it needs to be located if applicable, and how to call it).
I've tried this after pip installing webkit2png:
import os
os.system("webkit2png" "texas.html")
I've also tried:
import subprocess
subprocess.call("webkit2png", "texas.html")
Thanks in advance!

As of Bokeh 0.12.6, it is now possible to export PNG and SVG directly from
Python code.
Exporting PNGs looks like this
export_png(plot, filename="plot.png")
And exporting SVGs looks like this
plot.output_backend = "svg"
export_svgs(plot, filename="plot.svg")
There are some optional dependencies that need to be installed.
You can find more information in the Exporting Plots section of the User Guide.
Currently, plots are saved at their native resolution, but in 0.12.7 you will be able to set the size.

There's no such thing as an "HTML image." HTML enables one to incorporate image files of various types within an HTML document, which a Web browser knows how to display and process.
Run your Bokeh (?) code and use a commercial screen capture utility (I like SnagIt!, which is FREE). That gives you the capability of saving out the image to disk in a wide array of formats (JPEG, TIF, PNG, GIF, etc.).

Related

How to create a web-page looking exactly like a Python notebook?

I have a jupyter notebook that I want to share on Internet.
I will upload the *.ipynb file so that the visitor can download and run it, but I also wanted to show the notebook directly in a webpage.
Note : I don't want the reader to be able to actually RUN the notebook, I am just looking for an appropriate HTML / CSS / JS template or something similar. I assume the plots will just be pictures, which is fine
You can use <object> tag to embed files in html. but iPython notebook file is not supported. So covert it using the nbconvert.

Python jupyter notebook after converting to html , numbering of markdown disappeared

I used Table of Contents(2) of Nbextensions to create Table of Contents. And the titles were created using markdown.
Every thing is working fine. I mean , it looks pretty good in notebook modus.
But after I had converted the ipynb file to html file , then the number of each title dispeared . I used menu: File->Download as -> HTML to do this.
I tried to use another option "File->Download as ->HTML with toc" to convert to html. Although it generated desired numbering , this is still not what I want, because it will generate not only an html file but also multiple images file if there are some plots in the notebook.
Does anyone have a good idea?
I just need a SINGLE html file with everything embedded .
The numbering of the Nbextensions will be present if you'll download the notebook to PDF (after installing the relevant packages).
Regarding the HTML version, I didn't find an answer yet and will be happy for help either.
Notebook uses different formatting for the content. Exporting option only provide you to save your code. So, that you can send it to others.
Better save the data in the python notebook form itself to maintain formatting. Even exporting you to PDF won't provide you true formatting as the notebook does. But it will be better than HTML.
But if you still want the HTML format, you can format it manually as a webpage. May the formatting get improved in next version of nbconvert.

Faster background image on PDF generation with wkhtmltopdf

I'm trying to generate a 5-page PDF based on my HTML file with wkhtmltopdf and it is working well. I'm having a problem, however, regarding the time spent for this task, especifically when using a background image for each page.
When I use a bg_image, wkhtmltopdf uses about ~1.7s to generate, but without it (--nobackground option) only ~0.5s are spent. I've tried to convert my bg_image from .jpg to .png to try to make it faster, but it didn't work.
Is there any workaround onto it? Maybe a parameter change or a css change? (Right now my html file contains a declaration for a bg_image on each page, and a global declararion at the start setting its size and "no_repeat" option).
Thanks a lot for your attention!
I don't know about internals of wkhtmltopdf, how they handle a background. If they need some measureable time, i would expect, that they decode and encode the image data of the background image.
You could also use pdftk utility to apply a prepared pdf-background file to all pages of a pdf, which should be fast:
pdftk in.pdf background back.pdf output out.pdf
https://linux.die.net/man/1/pdftk

Need Help to Disable Download from a URL

Is there a way to disable a user from downloading a file from a URL?
For example I have a link:
wow.mywebsitedomain.com/templates/filename.svg
I want to disable the user from downloading the filename.svg
These svg files are not just an image, they are editable designs that I have spent countless hours on each. No, I do not care if someone does a screenprint or gets a png etc, as those are not scalable, editable, vector files.
When the user clicks on a png thumbnail my actual link opens my online design editor to allow the user to customize these files, then save to my server, then purchase printed media, and they are not allowed to download any files.
I tried putting the actual files into a password protected folder on my server, but they do not open properly, and I do not want the user to have password access to this folder.
Essentially I need the link to be accessible, just not show the actual link for someone to copy and open/save/download etc.
Hopefully there is a simple solution for a non-programmer with basic html skills?
Thanks
Your can do things like "disabling right-click" and stuff - it may prevent some users from downloading your file, BUT basically you cannot prevent a file which is downloaded and interpreted by the browser from being downloaded to a user's hard drive.
This is not only true for SVGs, but also for music, videos, etc.
Instead, you can convert your SVG file to a PNG on server-side, and show only the PNG to the user. Note that you have the possibility to create PNGs of different sizes on the fly - dependent on the request, user's screen resolution, etc. You can also implement caching of the generated PNGs if needed.
On how to create a PNG from SVG in PHP read here:
Convert SVG image to PNG with PHP
You can choose other raster image format, of course.
If they can view it, they can download it. End of story. If you only want them to see a PNG, make a PNG from it and put that up
My understanding is; if you can see it, you can download it,

Loading images from various sources in QTWebKit

I am trying to create a "smart" web browser to load local images. Basically it works as a GUI for an application. I am using QTWebKit to power the browser, the problem is that the images of a given page can be found in different places, some are local files, others are in different resource files.
For example:
an HTML node to load image x.jpg can look like <img src="x.jpg"> and for image y.gif on the same page it can be <img src="y.gif">, now x.jpg is a local file that can be either in the root directory or in some other place. y.gif on the other hand can be in a resource file.
I want the web browser first to set the paths to all possible sources and once the page has been loaded or preferably while the page is loading searches for the images and loads them from their original path.
I considered the option of reading the HTML data first, parse it and search for the resources one by one, then edit the html to include the full path of the image but that would take a long time than what I want and it isn't practical.
Can any one put me on the right direction or does any one have any ideas on how such a design can be implemented.
EDIT: I have manage to delegate the requests by overriding the QNetwrokAccessManager and QNetwrokReply and been able to get the path to the image. The only problem is loading the image into view. I am currently using QHttp to handle the incoming requests but so far I haven't been able to load the image.
been trying to use QHttp's Get() function and passing the path to the jpg image as (file:///path/to/image) and also tried using the local path but nothing is working.
Take a look at How to tell QWebPage not to load specific type of resources?
You need the same approach but instead of dropping request by constructing QNetworkRequest with empty QUrl you need to pass url to the file on the disk.