Created
March 14, 2022 09:42
-
-
Save andfanilo/c46906d9b65b20ef2ca135c15beb9545 to your computer and use it in GitHub Desktop.
Cropping an Image in a circular way using Streamlit - https://www.geeksforgeeks.org/cropping-an-image-in-a-circular-way-using-python/
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| """ | |
| streamlit run st_crop_image.py | |
| """ | |
| import streamlit as st | |
| import numpy as np | |
| from pathlib import Path | |
| from PIL import Image | |
| from PIL import ImageDraw | |
| st.title("Cropping an Image in a circular way using Python") | |
| st.caption("https://www.geeksforgeeks.org/cropping-an-image-in-a-circular-way-using-python/") | |
| path_to_picture = st.text_input("Path to image") | |
| if path_to_picture == "": | |
| st.info("Please write path to image") | |
| st.stop() | |
| if not Path(path_to_picture).exists(): | |
| st.warning(f'File `{path_to_picture}` not found') | |
| st.stop() | |
| c1, c2 = st.columns(2) | |
| img = Image.open(path_to_picture) | |
| height, width = img.size | |
| lum_img = Image.new("L", [height, width], 0) | |
| draw = ImageDraw.Draw(lum_img) | |
| draw.pieslice([(0, 0), (height, width)], 0, 360, fill=255, outline="white") | |
| img_arr = np.array(img) | |
| lum_img_arr = np.array(lum_img) | |
| final_img_arr = np.dstack((img_arr, lum_img_arr)) | |
| with c1: | |
| st.caption("Original image") | |
| st.image(img) | |
| with c2: | |
| st.caption("Cropped image") | |
| st.image(Image.fromarray(final_img_arr)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment