Chapter 8 image does not open
Closed this issue · 6 comments
Hi and thanks for a great book!
I just have an issue opening the image after the filter is applied and I try to run the Jupyter commands.
The response from the post action is fine (below), so there must be something with the filters code. Hope someone can help?
<bound method Response.raise_for_status of <Response [200]>>
The following error is returned
`---------------------------------------------------------------------------
UnidentifiedImageError Traceback (most recent call last)
in
2 import io
3
----> 4 image = Image.open(io.BytesIO(response.content))
5 image.save("response,jpg", "JPEG")
6
C:\ProgramData\Anaconda3\lib\site-packages\PIL\Image.py in open(fp, mode)
2860 warnings.warn(message)
2861 raise UnidentifiedImageError(
-> 2862 "cannot identify image file %r" % (filename if filename else fp)
2863 )
2864
UnidentifiedImageError: cannot identify image file <_io.BytesIO object at 0x000002AE80FDAB28>`
@AlexAdamov Thank you for raising this.
Can you share a copy of your Jupyter notebook or the code that you are using please?
@AlexAdamov
This looks like an error, you need to change the comma (,) to a period (.) and try again
image.save("response,jpg", "JPEG")
This should solve the problem
image.save("response.jpg", "JPEG")
Can you try this and let me know if it solves your problem please?
OK. What is happening is your response.content
is empty and thus raising an error. It is empty because response
was the defined earlier in the notebook (beginning of Section 8.5) and your commands seem to be standalone.
These commands needs to be run in the same notebook as where the previous commands were. In the comments notice it says In[4]
which means it's the fourth cell in a series of commands.
Alternatively, what you can do is add the following snippet to your 4 lines above and rerun
import requests
file = {"image": open("sample.jpg","rb")}
headers = {"type": "multipart/image"}
URL = "http://127.0.0.1:5000"
filter = "contour"
response = requests.post(f"{URL}/{filter}",headers=headers, files=file)
response.raise_for_status
from PIL import Image
import io
image = Image.open(io.BytesIO(response.content))
image.save("response.jpg","JPEG")
image
Let me know if this works
The last alternative - all in one cell worked :-)
It's strange because I ran the same commands in 4 separate cells (but in the same notebook) and for some reason it didn't identify the io.BytesIO object in the fourth cell.
Thanks for helping out. You book has been very useful!
The last alternative - all in one cell worked :-)
It's strange because I ran the same commands in 4 separate cells (but in the same notebook) and for some reason it didn't identify the io.BytesIO object in the fourth cell.
Just for your knowledge, this could would have happened because you may have shutdown the notebook / VSCode. The cell execution results do not persist across restarts.
Thanks for helping out. You book has been very useful!
🙂 Always encouraging to head good things, if possible please leave a review so it gets more exposures.