Commit
·
47e7d4f
1
Parent(s):
16ff55e
add: cropping
Browse files- src/streamlit_app.py +35 -4
src/streamlit_app.py
CHANGED
|
@@ -2,6 +2,26 @@ import streamlit as st
|
|
| 2 |
import fitz
|
| 3 |
from PIL import Image
|
| 4 |
import io
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
st.header("Line Art Data Annotation App")
|
| 7 |
uploaded_pdf = st.sidebar.file_uploader("Upload a PDF", type=["pdf"])
|
|
@@ -9,10 +29,12 @@ uploaded_pdf = st.sidebar.file_uploader("Upload a PDF", type=["pdf"])
|
|
| 9 |
if uploaded_pdf:
|
| 10 |
data = uploaded_pdf.read()
|
| 11 |
doc = fitz.open(stream=data, filetype="pdf")
|
| 12 |
-
|
| 13 |
# Initialize page index in session state
|
| 14 |
if "page_idx" not in st.session_state:
|
| 15 |
st.session_state.page_idx = 0
|
|
|
|
|
|
|
| 16 |
|
| 17 |
total_pages = doc.page_count
|
| 18 |
page_idx = st.session_state.page_idx % total_pages
|
|
@@ -23,7 +45,10 @@ if uploaded_pdf:
|
|
| 23 |
st.session_state.page_idx = (page_idx - 1) % total_pages
|
| 24 |
st.rerun()
|
| 25 |
with col_caption:
|
| 26 |
-
st.markdown(
|
|
|
|
|
|
|
|
|
|
| 27 |
with col_next:
|
| 28 |
if st.button("\>"):
|
| 29 |
st.session_state.page_idx = (page_idx + 1) % total_pages
|
|
@@ -32,5 +57,11 @@ if uploaded_pdf:
|
|
| 32 |
# Render image after controls
|
| 33 |
page = doc.load_page(page_idx)
|
| 34 |
pix = page.get_pixmap(dpi=200)
|
| 35 |
-
|
| 36 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
import fitz
|
| 3 |
from PIL import Image
|
| 4 |
import io
|
| 5 |
+
from streamlit_cropper import st_cropper
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
def get_padded_image(image, cropped_image):
|
| 9 |
+
base_w, base_h = image.size
|
| 10 |
+
crop_w, crop_h = cropped_image.size
|
| 11 |
+
# Match modes to avoid paste issues
|
| 12 |
+
crop_img = (
|
| 13 |
+
cropped_image.convert(image.mode)
|
| 14 |
+
if cropped_image.mode != image.mode
|
| 15 |
+
else cropped_image
|
| 16 |
+
)
|
| 17 |
+
# Create white background canvas matching the original image size
|
| 18 |
+
padded_image = Image.new(image.mode, (base_w, base_h), color="white")
|
| 19 |
+
# Center the cropped image on the canvas
|
| 20 |
+
paste_x = max(0, (base_w - crop_w) // 2)
|
| 21 |
+
paste_y = max(0, (base_h - crop_h) // 2)
|
| 22 |
+
padded_image.paste(crop_img, (paste_x, paste_y))
|
| 23 |
+
return padded_image
|
| 24 |
+
|
| 25 |
|
| 26 |
st.header("Line Art Data Annotation App")
|
| 27 |
uploaded_pdf = st.sidebar.file_uploader("Upload a PDF", type=["pdf"])
|
|
|
|
| 29 |
if uploaded_pdf:
|
| 30 |
data = uploaded_pdf.read()
|
| 31 |
doc = fitz.open(stream=data, filetype="pdf")
|
| 32 |
+
|
| 33 |
# Initialize page index in session state
|
| 34 |
if "page_idx" not in st.session_state:
|
| 35 |
st.session_state.page_idx = 0
|
| 36 |
+
if "cropped_images" not in st.session_state:
|
| 37 |
+
st.session_state.cropped_images = []
|
| 38 |
|
| 39 |
total_pages = doc.page_count
|
| 40 |
page_idx = st.session_state.page_idx % total_pages
|
|
|
|
| 45 |
st.session_state.page_idx = (page_idx - 1) % total_pages
|
| 46 |
st.rerun()
|
| 47 |
with col_caption:
|
| 48 |
+
st.markdown(
|
| 49 |
+
f"<center>Page {page_idx}/{total_pages - 1}</center>",
|
| 50 |
+
unsafe_allow_html=True,
|
| 51 |
+
)
|
| 52 |
with col_next:
|
| 53 |
if st.button("\>"):
|
| 54 |
st.session_state.page_idx = (page_idx + 1) % total_pages
|
|
|
|
| 57 |
# Render image after controls
|
| 58 |
page = doc.load_page(page_idx)
|
| 59 |
pix = page.get_pixmap(dpi=200)
|
| 60 |
+
image = Image.open(io.BytesIO(pix.tobytes("png")))
|
| 61 |
+
cropped_image = st_cropper(image, realtime_update=True)
|
| 62 |
+
st.image(cropped_image)
|
| 63 |
+
if st.button("Save"):
|
| 64 |
+
padded_image = get_padded_image(image, cropped_image)
|
| 65 |
+
st.session_state.cropped_images.append(padded_image)
|
| 66 |
+
print(f"{len(st.session_state.cropped_images)=}")
|
| 67 |
+
print(f"{st.session_state.cropped_images[-1].size=}")
|