Click here to Skip to main content
15,299,443 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am accepting an image from the user via a streamlit interface. However, I need to crop the image based on certain values. The image is being accepted correctlyThis is my current code to accept the image and pass it to another function where I crop it:

Python
uploaded_image = st.file_uploader("Choose an Image File", type=['jpg', 'jpeg', 'png'])
if uploaded_image is not None:
    with open(uploaded_image.name, "wb") as img:        
        img.write(uploaded_image.getbuffer())           
    im_name = uploaded_image.name.split(".")[0]         
    with st.spinner("Processing......."):
        text = azure_result(im_name, uploaded_image)

Here's the part of the function where I encounter an error, bbox has valid values.

Python
def azure_result(image_name, image):
    img = Image.open(image)
    img2 = img.crop((bbox[-1][0], bbox[-1][1], bbox[-1][4], bbox[-1][5]))


The error: File "C:\Repos/frai-main\text_detector.py", line 156, in azure_result img2 = image.crop((bbox[-1][0], bbox[-1][1], bbox[-1][4], bbox[-1][5])) AttributeError: 'UploadedFile' object has no attribute 'crop'

How do I successfully crop the passed image? I'm open to using any alternative to PIL as well.

What I have tried:

Tried using PIL to crop 'image' directly, same error as with using .open() and 'img'.
Posted
Updated 4-Aug-21 2:37am
Comments
Richard MacCutchan 4-Aug-21 9:20am
   
The message is clearly telling you that the img object does not have a crop method. So check exactly what is returned from the call to Image.open.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900