diff --git a/README.md b/README.md index 6413af8..e63a232 100644 --- a/README.md +++ b/README.md @@ -20,13 +20,15 @@ st.set_option('deprecation.showfileUploaderEncoding', False) st.header("Cropper Demo") img_file = st.sidebar.file_uploader(label='Upload a file', type=['png', 'jpg']) realtime_update = st.sidebar.checkbox(label="Update in Real Time", value=True) -box_color = st.sidebar.beta_color_picker(label="Box Color", value='#0000FF') +box_color = st.sidebar.color_picker(label="Box Color", value='#0000FF') aspect_choice = st.sidebar.radio(label="Aspect Ratio", options=["1:1", "16:9", "4:3", "2:3", "Free"]) -aspect_dict = {"1:1": (1,1), - "16:9": (16,9), - "4:3": (4,3), - "2:3": (2,3), - "Free": None} +aspect_dict = { + "1:1": (1, 1), + "16:9": (16, 9), + "4:3": (4, 3), + "2:3": (2, 3), + "Free": None +} aspect_ratio = aspect_dict[aspect_choice] if img_file: @@ -44,4 +46,7 @@ if img_file: ``` ## References -- [streamlit-drawable-canvas](https://github.com/andfanilo/streamlit-drawable-canvas) \ No newline at end of file +- [streamlit-drawable-canvas](https://github.com/andfanilo/streamlit-drawable-canvas) + +## Acknowledgments +Big thanks to zoncrd and yanirs for their contributions \ No newline at end of file diff --git a/app.py b/app.py index 6353d45..7cff5ce 100644 --- a/app.py +++ b/app.py @@ -1,4 +1,5 @@ import streamlit as st +import numpy as np from streamlit_cropper import st_cropper from PIL import Image st.set_option('deprecation.showfileUploaderEncoding', False) @@ -28,11 +29,12 @@ if img_file: img = Image.open(img_file) - + if not realtime_update: + st.write("Double click to save crop") if return_type == 'box': rect = st_cropper( img, - realtime_update=True, + realtime_update=realtime_update, box_color=box_color, aspect_ratio=aspect_ratio, return_type=return_type @@ -44,8 +46,6 @@ masked_image[top:top + height, left:left + width] = raw_image[top:top + height, left:left + width] st.image(Image.fromarray(masked_image), caption='masked image') else: - if not realtime_update: - st.write("Double click to save crop") # Get a cropped image from the frontend cropped_img = st_cropper( img, diff --git a/setup.py b/setup.py index 995a8a8..7437844 100644 --- a/setup.py +++ b/setup.py @@ -13,7 +13,7 @@ def readme() -> str: setuptools.setup( name="streamlit-cropper", - version="0.1.3", + version="0.2.0", author="Turner Anderson", author_email="andersontur11@gmail.com", description="A simple image cropper for Streamlit", @@ -25,8 +25,8 @@ def readme() -> str: classifiers=[], python_requires=">=3.6", install_requires=[ - "streamlit >= 0.63", - "Pillow >= 7.1.2", - "numpy >= 1.18.4" + "streamlit >= 1.3.1", + "Pillow >= 8.4.0", + "numpy >= 1.21.5" ], ) diff --git a/streamlit_cropper/__init__.py b/streamlit_cropper/__init__.py index e945e0a..8936da7 100644 --- a/streamlit_cropper/__init__.py +++ b/streamlit_cropper/__init__.py @@ -110,7 +110,7 @@ def st_cropper(img: Image, realtime_update: bool=True, box_color: str='blue', as resized_img = _resize_img(img) resized_ratio_w = img.width / resized_img.width resized_ratio_h = img.height / resized_img.height - img = resized_img + orig_img, img = img, resized_img # Find a default box if not box_algorithm: @@ -149,16 +149,17 @@ def st_cropper(img: Image, realtime_update: bool=True, box_color: str='blue', as else: rect = box + # Scale box according to the resize ratio, but make sure new box does not exceed original bounds + rect['left'] = max(0, int(rect['left'] * resized_ratio_w)) + rect['top'] = max(0, int(rect['top'] * resized_ratio_h)) + rect['width'] = min(orig_img.size[0] - rect['left'], int(rect['width'] * resized_ratio_w)) + rect['height'] = min(orig_img.size[1] - rect['top'], int(rect['height'] * resized_ratio_h)) + # Return the value desired by the return_type if return_type.lower() == 'image': - cropped_img = img.crop((rect['left'], rect['top'], rect['width'] + rect['left'], rect['height'] + rect['top'])) + cropped_img = orig_img.crop((rect['left'], rect['top'], rect['width'] + rect['left'], rect['height'] + rect['top'])) return cropped_img elif return_type.lower() == 'box': - # Return scaled box according to resize ratio - rect['left'] = int(rect['left'] * resized_ratio_w) - rect['width'] = int(rect['width'] * resized_ratio_w) - rect['top'] = int(rect['top'] * resized_ratio_h) - rect['height'] = int(rect['height'] * resized_ratio_h) return rect @@ -223,4 +224,4 @@ def st_cropper(img: Image, realtime_update: bool=True, box_color: str='blue', as # Manipulate cropped image at will st.write("Preview") _ = cropped_img.thumbnail((150, 150)) - st.image(cropped_img) + st.image(cropped_img) \ No newline at end of file