Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
@@ -39,3 +39,52 @@ svg_string = dwg.tostring()
|
|
39 |
|
40 |
# Display the SVG canvas in the Streamlit app
|
41 |
st.write(f'<svg viewBox="0 0 {CANVAS_WIDTH} {CANVAS_HEIGHT}">{svg_string}</svg>', unsafe_allow_html=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
39 |
|
40 |
# Display the SVG canvas in the Streamlit app
|
41 |
st.write(f'<svg viewBox="0 0 {CANVAS_WIDTH} {CANVAS_HEIGHT}">{svg_string}</svg>', unsafe_allow_html=True)
|
42 |
+
|
43 |
+
|
44 |
+
import streamlit as st
|
45 |
+
from svglib.svglib import svg2rlg
|
46 |
+
from reportlab.graphics import renderPDF, renderPM
|
47 |
+
from io import BytesIO
|
48 |
+
|
49 |
+
# Define the SVG images as strings
|
50 |
+
svg_images = {
|
51 |
+
'Fraser spiral': """
|
52 |
+
<svg viewBox="-40 -40 80 80" xmlns="http://www.w3.org/2000/svg">
|
53 |
+
<g stroke-width="0.4" stroke="black" fill="none">
|
54 |
+
<path d="M40 0 Q 40 40 0 40 Q -40 40 -40 0 Q -40 -40 0 -40 Q 40 -40 40 0" />
|
55 |
+
<path d="M-39 1 Q -39 -39 0 -39 Q 38 -39 39 -1" />
|
56 |
+
<path d="M0 -39 Q 0 1 38 1" />
|
57 |
+
</g>
|
58 |
+
</svg>
|
59 |
+
""",
|
60 |
+
'Penrose triangle': """
|
61 |
+
<svg viewBox="-100 -100 200 200" xmlns="http://www.w3.org/2000/svg">
|
62 |
+
<g fill="none" stroke="black" stroke-width="4">
|
63 |
+
<path d="M -60 60 L 60 60 L 0 -60 Z" />
|
64 |
+
<path d="M -60 -60 L 60 -60 L 0 60 Z" />
|
65 |
+
<path d="M -60 60 L -60 -60 L 60 0 Z" />
|
66 |
+
<path d="M 60 60 L 60 -60 L -60 0 Z" />
|
67 |
+
</g>
|
68 |
+
</svg>
|
69 |
+
"""
|
70 |
+
}
|
71 |
+
|
72 |
+
# Define the functions to convert SVG to PNG
|
73 |
+
def svg_to_image(svg_string):
|
74 |
+
drawing = svg2rlg(BytesIO(svg_string.encode()))
|
75 |
+
img_data = BytesIO()
|
76 |
+
renderPM.drawToFile(drawing, img_data, fmt="PNG")
|
77 |
+
return img_data.getvalue()
|
78 |
+
|
79 |
+
# Define the app layout
|
80 |
+
st.set_page_config(page_title="SVG Optical Illusions", page_icon=":eyeglasses:")
|
81 |
+
st.title("SVG Optical Illusions")
|
82 |
+
|
83 |
+
# Display the SVG images and convert them to PNG
|
84 |
+
for name, svg_string in svg_images.items():
|
85 |
+
st.subheader(name)
|
86 |
+
svg = st.markdown(svg_string, unsafe_allow_html=True)
|
87 |
+
png = svg_to_image(svg_string)
|
88 |
+
st.image(png, use_column_width=True)
|
89 |
+
|
90 |
+
|