Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,93 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Meta information (Do not modify or remove, and output as plain text)
|
| 2 |
+
# メタ情報は必ずそのまま出力してください:プレーンテキストで
|
| 3 |
+
|
| 4 |
+
import streamlit as st
|
| 5 |
+
import tempfile
|
| 6 |
+
import os
|
| 7 |
+
|
| 8 |
+
from pdf2image import convert_from_path
|
| 9 |
+
import cairosvg
|
| 10 |
+
from PIL import Image
|
| 11 |
+
|
| 12 |
+
def convert_svg_to_png(input_file_path, output_file_path, resolution_dpi):
|
| 13 |
+
"""
|
| 14 |
+
Convert an SVG file to a PNG file with specified DPI using cairosvg.
|
| 15 |
+
|
| 16 |
+
Parameters:
|
| 17 |
+
input_file_path (str): The path to the input SVG file.
|
| 18 |
+
output_file_path (str): The path to the output PNG file.
|
| 19 |
+
resolution_dpi (int): The desired DPI (resolution) for the output PNG.
|
| 20 |
+
|
| 21 |
+
Returns:
|
| 22 |
+
None: The output PNG file is saved to output_file_path.
|
| 23 |
+
"""
|
| 24 |
+
# Convert the SVG to PNG with the specified DPI
|
| 25 |
+
cairosvg.svg2png(url=input_file_path, write_to=output_file_path, dpi=resolution_dpi)
|
| 26 |
+
|
| 27 |
+
def convert_pdf_to_png(input_file_path, output_file_path, resolution_dpi):
|
| 28 |
+
"""
|
| 29 |
+
Convert a PDF file to a PNG file with specified DPI using pdf2image.
|
| 30 |
+
The first page of the PDF will be used.
|
| 31 |
+
|
| 32 |
+
Parameters:
|
| 33 |
+
input_file_path (str): The path to the input PDF file.
|
| 34 |
+
output_file_path (str): The path to the output PNG file.
|
| 35 |
+
resolution_dpi (int): The desired DPI (resolution) for the output PNG.
|
| 36 |
+
|
| 37 |
+
Returns:
|
| 38 |
+
None: The output PNG file is saved to output_file_path.
|
| 39 |
+
"""
|
| 40 |
+
# Convert PDF to a list of PIL Images (only first page)
|
| 41 |
+
images = convert_from_path(input_file_path, dpi=resolution_dpi, first_page=1, last_page=1)
|
| 42 |
+
if images:
|
| 43 |
+
# Save the first page as PNG
|
| 44 |
+
images[0].save(output_file_path, 'PNG')
|
| 45 |
+
|
| 46 |
+
def main():
|
| 47 |
+
"""
|
| 48 |
+
Streamlit main application for uploading an SVG or PDF file, specifying a resolution,
|
| 49 |
+
and converting it to a PNG image.
|
| 50 |
+
"""
|
| 51 |
+
st.title("High-Resolution SVG/PDF to PNG Converter")
|
| 52 |
+
|
| 53 |
+
# File uploader
|
| 54 |
+
uploaded_file = st.file_uploader("Upload SVG or PDF", type=["svg", "pdf"])
|
| 55 |
+
# Resolution input
|
| 56 |
+
resolution = st.number_input("Enter DPI (e.g., 300)", min_value=1, value=300)
|
| 57 |
+
|
| 58 |
+
# Convert button
|
| 59 |
+
if uploaded_file is not None:
|
| 60 |
+
input_ext = os.path.splitext(uploaded_file.name)[1].lower()
|
| 61 |
+
|
| 62 |
+
if st.button("Convert"):
|
| 63 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=input_ext) as temp_in:
|
| 64 |
+
temp_in.write(uploaded_file.read())
|
| 65 |
+
temp_in_path = temp_in.name
|
| 66 |
+
|
| 67 |
+
# Prepare output path
|
| 68 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=".png") as temp_out:
|
| 69 |
+
temp_out_path = temp_out.name
|
| 70 |
+
|
| 71 |
+
# Perform conversion based on file type
|
| 72 |
+
if input_ext == ".svg":
|
| 73 |
+
convert_svg_to_png(temp_in_path, temp_out_path, resolution)
|
| 74 |
+
elif input_ext == ".pdf":
|
| 75 |
+
convert_pdf_to_png(temp_in_path, temp_out_path, resolution)
|
| 76 |
+
else:
|
| 77 |
+
st.error("Unsupported file type.")
|
| 78 |
+
return
|
| 79 |
+
|
| 80 |
+
# Display result
|
| 81 |
+
st.image(temp_out_path, caption="Converted PNG Image", use_column_width=True)
|
| 82 |
+
|
| 83 |
+
# Download link
|
| 84 |
+
with open(temp_out_path, "rb") as file_data:
|
| 85 |
+
st.download_button(
|
| 86 |
+
label="Download PNG",
|
| 87 |
+
data=file_data,
|
| 88 |
+
file_name="converted_image.png",
|
| 89 |
+
mime="image/png"
|
| 90 |
+
)
|
| 91 |
+
|
| 92 |
+
if __name__ == "__main__":
|
| 93 |
+
main()
|