import io import streamlit as st import fitz from PIL import Image from datasets import Dataset from streamlit_cropper import st_cropper def get_padded_image(image, cropped_image): base_w, base_h = image.size crop_w, crop_h = cropped_image.size # Match modes to avoid paste issues crop_img = ( cropped_image.convert(image.mode) if cropped_image.mode != image.mode else cropped_image ) # Create white background canvas matching the original image size padded_image = Image.new(image.mode, (base_w, base_h), color="white") # Center the cropped image on the canvas paste_x = max(0, (base_w - crop_w) // 2) paste_y = max(0, (base_h - crop_h) // 2) padded_image.paste(crop_img, (paste_x, paste_y)) return padded_image st.header("Line Art Data Annotation App") uploaded_pdf = st.sidebar.file_uploader("Upload a PDF", type=["pdf"]) if uploaded_pdf: data = uploaded_pdf.read() doc = fitz.open(stream=data, filetype="pdf") # Initialize page index in session state if "page_idx" not in st.session_state: st.session_state.page_idx = 0 if "cropped_images" not in st.session_state: st.session_state.cropped_images = [] if "captions" not in st.session_state: st.session_state.captions = [] total_pages = doc.page_count page_idx = st.session_state.page_idx % total_pages # Navigation buttons (placed above the image) col_prev, col_caption, col_next = st.columns([1, 8, 1]) with col_prev: if st.button("<"): st.session_state.page_idx = (page_idx - 1) % total_pages st.rerun() with col_caption: st.markdown( f"