Spaces:
Sleeping
Sleeping
Update app.py
Browse filesAdded Zoom and JS delay parameters
app.py
CHANGED
|
@@ -3,27 +3,37 @@ import imgkit
|
|
| 3 |
from PIL import Image
|
| 4 |
from io import BytesIO
|
| 5 |
|
| 6 |
-
def html_to_image(html_code, width, height):
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
-
def website_to_image(website_link, width, height):
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
|
| 28 |
with gr.Blocks() as interface:
|
| 29 |
gr.Markdown("# HTML to Image Converter")
|
|
@@ -46,6 +56,16 @@ with gr.Blocks() as interface:
|
|
| 46 |
value=720,
|
| 47 |
step=10,
|
| 48 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
with gr.Row(variant="panel", ):
|
| 50 |
with gr.Column():
|
| 51 |
website_link = gr.Textbox(
|
|
@@ -54,42 +74,53 @@ with gr.Blocks() as interface:
|
|
| 54 |
)
|
| 55 |
snapshotwebsite = gr.Button("Show Website", size="lg")
|
| 56 |
output_image = gr.Image(type="pil", format="PNG", show_label=False)
|
| 57 |
-
generate_button = gr.Button("Refresh")
|
| 58 |
-
|
| 59 |
html_code_input.change(
|
| 60 |
fn=html_to_image,
|
| 61 |
-
inputs=[html_code_input, width_input, height_input],
|
| 62 |
outputs=output_image
|
| 63 |
)
|
| 64 |
width_input.change(
|
| 65 |
fn=html_to_image,
|
| 66 |
-
inputs=[html_code_input, width_input, height_input],
|
| 67 |
outputs=output_image
|
| 68 |
)
|
| 69 |
height_input.change(
|
| 70 |
fn=html_to_image,
|
| 71 |
-
inputs=[html_code_input, width_input, height_input],
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 72 |
outputs=output_image
|
| 73 |
)
|
| 74 |
|
| 75 |
generate_button.click(
|
| 76 |
fn=html_to_image,
|
| 77 |
-
inputs=[html_code_input, width_input, height_input],
|
| 78 |
outputs=output_image
|
| 79 |
)
|
| 80 |
|
| 81 |
snapshotwebsite.click(
|
| 82 |
fn=website_to_image,
|
| 83 |
-
inputs=[website_link, width_input, height_input],
|
| 84 |
outputs=output_image
|
| 85 |
)
|
| 86 |
|
| 87 |
gr.Examples(
|
| 88 |
examples=[
|
| 89 |
-
["<div style='background: linear-gradient(45deg, #ff6b6b, #4ecdc4); padding: 20px; border-radius: 10px;'><h1 style='color: white; font-family: Arial;'>Hello, World!</h1></div>", 800, 400],
|
| 90 |
-
["<div style='background: #f0f0f0; padding: 20px;'><ul style='color: #333;'><li>Item 1</li><li>Item 2</li><li>Item 3</li></ul></div>", 600, 300]
|
| 91 |
],
|
| 92 |
-
inputs=[html_code_input, width_input, height_input]
|
| 93 |
)
|
| 94 |
|
| 95 |
if __name__ == "__main__":
|
|
|
|
| 3 |
from PIL import Image
|
| 4 |
from io import BytesIO
|
| 5 |
|
| 6 |
+
def html_to_image(html_code, width, height, zoom, jsdelay):
|
| 7 |
+
try:
|
| 8 |
+
options = {
|
| 9 |
+
'format': 'png',
|
| 10 |
+
'encoding': 'UTF-8',
|
| 11 |
+
'width': str(width),
|
| 12 |
+
'height': str(height),
|
| 13 |
+
'zoom': str(zoom),
|
| 14 |
+
'javascript-delay': str(jsdelay),
|
| 15 |
+
}
|
| 16 |
+
image = Image.open(BytesIO(imgkit.from_string(html_code, False, options=options)))
|
| 17 |
+
return image
|
| 18 |
+
except Exception as e:
|
| 19 |
+
raise gr.Error(e, duration=5)
|
| 20 |
|
| 21 |
+
def website_to_image(website_link, width, height, zoom, jsdelay):
|
| 22 |
+
try:
|
| 23 |
+
if not website_link:
|
| 24 |
+
website_link = "https://www.google.com"
|
| 25 |
+
options = {
|
| 26 |
+
'format': 'png',
|
| 27 |
+
'encoding': 'UTF-8',
|
| 28 |
+
'width': str(width),
|
| 29 |
+
'height': str(height),
|
| 30 |
+
'zoom': str(zoom),
|
| 31 |
+
'javascript-delay': str(jsdelay),
|
| 32 |
+
}
|
| 33 |
+
image = Image.open(BytesIO(imgkit.from_url(website_link, False, options=options)))
|
| 34 |
+
return image
|
| 35 |
+
except Exception as e:
|
| 36 |
+
raise gr.Error(e, duration=5)
|
| 37 |
|
| 38 |
with gr.Blocks() as interface:
|
| 39 |
gr.Markdown("# HTML to Image Converter")
|
|
|
|
| 56 |
value=720,
|
| 57 |
step=10,
|
| 58 |
)
|
| 59 |
+
zoom_input = gr.Number(
|
| 60 |
+
label="Zoom",
|
| 61 |
+
value=1.0,
|
| 62 |
+
step=0.1
|
| 63 |
+
)
|
| 64 |
+
jsdelay_input = gr.Number(
|
| 65 |
+
label="Javascript Delay",
|
| 66 |
+
value=200,
|
| 67 |
+
step=100
|
| 68 |
+
)
|
| 69 |
with gr.Row(variant="panel", ):
|
| 70 |
with gr.Column():
|
| 71 |
website_link = gr.Textbox(
|
|
|
|
| 74 |
)
|
| 75 |
snapshotwebsite = gr.Button("Show Website", size="lg")
|
| 76 |
output_image = gr.Image(type="pil", format="PNG", show_label=False)
|
| 77 |
+
generate_button = gr.Button("Force Refresh")
|
| 78 |
+
|
| 79 |
html_code_input.change(
|
| 80 |
fn=html_to_image,
|
| 81 |
+
inputs=[html_code_input, width_input, height_input, zoom_input, jsdelay_input],
|
| 82 |
outputs=output_image
|
| 83 |
)
|
| 84 |
width_input.change(
|
| 85 |
fn=html_to_image,
|
| 86 |
+
inputs=[html_code_input, width_input, height_input, zoom_input, jsdelay_input],
|
| 87 |
outputs=output_image
|
| 88 |
)
|
| 89 |
height_input.change(
|
| 90 |
fn=html_to_image,
|
| 91 |
+
inputs=[html_code_input, width_input, height_input, zoom_input, jsdelay_input],
|
| 92 |
+
outputs=output_image
|
| 93 |
+
)
|
| 94 |
+
zoom_input.change(
|
| 95 |
+
fn=html_to_image,
|
| 96 |
+
inputs=[html_code_input, width_input, height_input, zoom_input, jsdelay_input],
|
| 97 |
+
outputs=output_image
|
| 98 |
+
)
|
| 99 |
+
|
| 100 |
+
jsdelay_input.change(
|
| 101 |
+
fn=html_to_image,
|
| 102 |
+
inputs=[html_code_input, width_input, height_input, zoom_input, jsdelay_input],
|
| 103 |
outputs=output_image
|
| 104 |
)
|
| 105 |
|
| 106 |
generate_button.click(
|
| 107 |
fn=html_to_image,
|
| 108 |
+
inputs=[html_code_input, width_input, height_input, zoom_input, jsdelay_input],
|
| 109 |
outputs=output_image
|
| 110 |
)
|
| 111 |
|
| 112 |
snapshotwebsite.click(
|
| 113 |
fn=website_to_image,
|
| 114 |
+
inputs=[website_link, width_input, height_input, zoom_input, jsdelay_input],
|
| 115 |
outputs=output_image
|
| 116 |
)
|
| 117 |
|
| 118 |
gr.Examples(
|
| 119 |
examples=[
|
| 120 |
+
["<div style='background: linear-gradient(45deg, #ff6b6b, #4ecdc4); padding: 20px; border-radius: 10px;'><h1 style='color: white; font-family: Arial;'>Hello, World!</h1></div>", 800, 400, 1.0, 200],
|
| 121 |
+
["<div style='background: #f0f0f0; padding: 20px;'><ul style='color: #333;'><li>Item 1</li><li>Item 2</li><li>Item 3</li></ul></div>", 600, 300, 1.0, 200],
|
| 122 |
],
|
| 123 |
+
inputs=[html_code_input, width_input, height_input, zoom_input, jsdelay_input],
|
| 124 |
)
|
| 125 |
|
| 126 |
if __name__ == "__main__":
|