image-to-text / app.py
KZTech's picture
Create app.py
832f690 verified
raw
history blame
980 Bytes
import streamlit as st
from PIL import Image
import pytesseract
import io
st.set_page_config(page_title="OCR – Image to Text", layout="centered")
st.title("πŸ–ΌοΈ OCR – Image to Text")
st.write("Upload a PNG, JPG or JPEG image to extract text.")
uploaded_file = st.file_uploader("Choose an image file", type=["png", "jpg", "jpeg"])
if uploaded_file:
try:
# Read and open the image
img_data = uploaded_file.read()
image = Image.open(io.BytesIO(img_data))
# Show preview
st.image(image, caption="Preview", use_column_width=True)
# Extraction button
if st.button("Extract Text"):
with st.spinner("Running OCR..."):
text = pytesseract.image_to_string(image)
st.subheader("Extracted Text")
st.text_area("", text, height=300)
except Exception as e:
st.error(f"Failed to process image: {e}")
else:
st.info("Please upload an image to get started.")