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 an image (PNG, JPG, JPEG) to extract text.") uploaded_file = st.file_uploader("Upload Image", type=["png", "jpg", "jpeg"]) image = None if uploaded_file is not None: # Display file metadata st.write(f"**Filename:** {uploaded_file.name}") st.write(f"**Type:** {uploaded_file.type}") st.write(f"**Size:** {uploaded_file.size} bytes") try: # Read bytes and open with PIL bytes_data = uploaded_file.getvalue() image = Image.open(io.BytesIO(bytes_data)) st.image(image, caption="Preview", use_column_width=True) except Exception as e: st.error(f"⚠️ Failed to open image: {e}") # Only show extract button if image was loaded successfully if image is not None: if st.button("Extract Text"): with st.spinner("Running OCR..."): try: text = pytesseract.image_to_string(image) st.success("✅ Text extracted!") st.text_area("Extracted Text", value=text, height=300) except Exception as e: st.error(f"❌ OCR error: {e}")