Parquet-PIL / app.py
Nymbo's picture
Update app.py
6b5f8e9 verified
import gradio as gr
import pandas as pd
import pyarrow as pa
import pyarrow.parquet as pq
from PIL import Image
import io
import base64
import tempfile
css = '''
.gradio-container{max-width: 950px !important}
h1{text-align:center}
'''
DESCRIPTIONz= """## Image to Parquet ๐Ÿ“‚
"""
def image_to_parquet(files):
image_data = []
for file_info in files:
with open(file_info, "rb") as image_file:
img = Image.open(image_file)
buffered = io.BytesIO()
img.save(buffered, format="PNG")
img_str = base64.b64encode(buffered.getvalue()).decode("utf-8")
image_data.append({"name": file_info, "data": img_str})
df = pd.DataFrame(image_data)
table = pa.Table.from_pandas(df)
with tempfile.NamedTemporaryFile(delete=False, suffix=".parquet") as tmp_file:
pq.write_table(table, tmp_file)
parquet_file_path = tmp_file.name
return parquet_file_path
with gr.Blocks(css=css, theme="Nymbo/Nymbo_Theme") as demo:
gr.Markdown(DESCRIPTIONz)
with gr.Row():
image_input = gr.File(label="Upload Images", type="filepath", file_count="multiple", file_types=["image"])
download_button = gr.File(label="Download Parquet File", interactive=False)
convert_button = gr.Button("Convert Image to Parquet")
convert_button.click(fn=image_to_parquet, inputs=[image_input], outputs=[download_button])
gr.Markdown("๐Ÿ“Œ speed / time of converting images to a .parquet file depends on both the number of images uploaded and the quality and size of the uploaded images invloved for the conversion.")
demo.launch()