I have had the pleasure to try out streamlit-cropper by turner-anderson. It is a wonderful module for all computer vision projects! Unfortunately, I faced some issue getting the module to work properly on mobile.
As such, I took some time to integrate another amazing project from fengyuanchen: cropperjs using streamlit components.
Have released the component on pypi and can be easily installed via pip install streamlit-cropperjs
Hi!
Iām using this code to take a snip, Iām facing a problem when I click the button Detect, the whole cropping thing goes away and nothing is displayed of the cropped part.
import streamlit as st
import fitz
from PIL import Image
import io
from streamlit_cropperjs import st_cropperjs
def main():
st.title("Snipping tool")
uploaded_file = st.file_uploader("Upload a PDF file", type=["pdf"])
if uploaded_file is not None:
page_number = st.number_input("Enter page number", value=1, min_value=1)
pdf_page_image = get_pdf_page_image(uploaded_file, page_number)
# Display the original PDF image
st.image(pdf_page_image, caption=f"Original - Page {page_number}", use_column_width=True)
if st.button("Open Cropper"):
image_bytes = io.BytesIO()
pdf_page_image.save(image_bytes, format='JPEG')
pdf_page_binary_data = image_bytes.getvalue()
crop_result = st_cropperjs(pic=pdf_page_binary_data, btn_text="Detect!", key="foo")
print("See")
print(f"..what..{crop_result}..")
if crop_result is not None:
cropped_image = Image.open(io.BytesIO(crop_result))
st.image(cropped_image, caption="Cropped Image", use_column_width=True)
print("See1")
print(f"..check..{type(cropped_image)}")
@st.cache_data
def get_pdf_page_image(uploaded_pdf, page_number):
pdf_document = fitz.open(stream=uploaded_pdf.read(), filetype="pdf")
pdf_page = pdf_document[page_number - 1]
pix = pdf_page.get_pixmap()
img = Image.frombytes("RGB", [pix.width, pix.height], pix.samples)
return img
if __name__ == "__main__":
main()
Iāve tried removing the key, but it doesnāt help, then I put the debugging statements so crop_result is not able to store the value. Can you please advise.
the issue is with too many nested buttons. Streamlit default behavior will reload the page at each button press. Hence, you need to make use of session state.
Example of a running code:
import io
import fitz
import streamlit as st
from PIL import Image
from streamlit_cropperjs import st_cropperjs
def main():
if "uploaded" not in st.session_state and "crop" not in st.session_state:
st.session_state["uploaded"] = False
st.session_state["crop"] = False
st.title("Snipping tool")
uploaded_file = st.file_uploader("Upload a PDF file", type=["pdf"])
if uploaded_file is not None:
st.session_state["uploaded"] = True
page_number = st.number_input("Enter page number", value=1, min_value=1)
pdf_page_image = get_pdf_page_image(uploaded_file, page_number)
# Display the original PDF image
st.image(
pdf_page_image,
caption=f"Original - Page {page_number}",
use_column_width=True,
)
if st.session_state["uploaded"]:
if st.button("Open Cropper"):
st.session_state["crop"] = True
if st.session_state["uploaded"] and st.session_state["crop"]:
image_bytes = io.BytesIO()
pdf_page_image.save(image_bytes, format="JPEG")
pdf_page_binary_data = image_bytes.getvalue()
crop_result = st_cropperjs(
pic=pdf_page_binary_data, btn_text="Detect!", key="foo1"
)
if crop_result is not None:
cropped_image = Image.open(io.BytesIO(crop_result))
st.image(cropped_image, caption="Cropped Image", use_column_width=True)
print("See1")
print(f"..check..{type(cropped_image)}")
@st.cache_data
def get_pdf_page_image(uploaded_pdf, page_number):
pdf_document = fitz.open(stream=uploaded_pdf.read(), filetype="pdf")
pdf_page = pdf_document[page_number - 1]
pix = pdf_page.get_pixmap()
img = Image.frombytes("RGB", [pix.width, pix.height], pix.samples)
return img
if __name__ == "__main__":
main()
Thanks for stopping by! We use cookies to help us understand how you interact with our website.
By clicking āAccept allā, you consent to our use of cookies. For more information, please see our privacy policy.
Cookie settings
Strictly necessary cookies
These cookies are necessary for the website to function and cannot be switched off. They are usually only set in response to actions made by you which amount to a request for services, such as setting your privacy preferences, logging in or filling in forms.
Performance cookies
These cookies allow us to count visits and traffic sources so we can measure and improve the performance of our site. They help us understand how visitors move around the site and which pages are most frequently visited.
Functional cookies
These cookies are used to record your choices and settings, maintain your preferences over time and recognize you when you return to our website. These cookies help us to personalize our content for you and remember your preferences.
Targeting cookies
These cookies may be deployed to our site by our advertising partners to build a profile of your interest and provide you with content that is relevant to you, including showing you relevant ads on other websites.