PIL.UnidentifiedImageError: cannot identify image file

이지훈·2022년 10월 26일
0

이미지 파일을 열어서 api 호출을 하는 코드를 작성중이었다.

file =  open(file, 'rb' ) 
upload = { 'file' : file } 

resp1 = requests.post("http://localhost:5000/intact", files=upload)
resp2 =  requests.post("http://localhost:5000/upload", files = upload )

근데 PIL.UnidentifiedImageError: cannot identify image file
라는 에러가 떴다.


  • 해결방법
    file을 닫아주고 다시 열어주면 해결된다
    내 코드의 이전 작업에 의해 이미지 파일이 여전히 사용 중(잠겼기) 때문에 오류가 발생한거였다. 그래서 파일을 닫아줄 필요가 있었다!
file =  open(file, 'rb' ) 
upload = { 'file' : file } 
resp1 = requests.post("http://localhost:5000/intact", files=upload)
file.close()

file =  open(file_path, 'rb' )
upload = { 'file' : file } 
resp2 =  requests.post("http://localhost:5000/upload", files = upload )
file.close()

profile
꾸준하게 🐌

0개의 댓글