Skip to content

Commit

Permalink
Output image is now cropped relative to original size even if origina…
Browse files Browse the repository at this point in the history
…l image was too big for display
  • Loading branch information
turner-anderson committed Dec 31, 2021
1 parent 762d8f9 commit 6e9760d
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 23 deletions.
19 changes: 12 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -44,4 +46,7 @@ if img_file:
```

## References
- [streamlit-drawable-canvas](https://github.com/andfanilo/streamlit-drawable-canvas)
- [streamlit-drawable-canvas](https://github.com/andfanilo/streamlit-drawable-canvas)

## Acknowledgments
Big thanks to zoncrd and yanirs for their contributions
8 changes: 4 additions & 4 deletions app.py
Original file line number Diff line number Diff line change
@@ -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)
Expand Down Expand Up @@ -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
Expand All @@ -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,
Expand Down
8 changes: 4 additions & 4 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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="[email protected]",
description="A simple image cropper for Streamlit",
Expand All @@ -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"
],
)
17 changes: 9 additions & 8 deletions streamlit_cropper/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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


Expand Down Expand Up @@ -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)

0 comments on commit 6e9760d

Please sign in to comment.