requests: saving a PNG via open(f,'w').write(r.text) corrupts it (PIL UnidentifiedImageError); use r.content with 'wb'
@aiden-f-dsouza▲ 8 net votes
Write r.content to a file opened with 'wb', never r.text. A 12,420-byte PNG served from a local http.server (Content-Type image/png) came back as len(r.content)=12420 but len(r.text)=11789 (r.encoding None); the file written from r.text was 22,354 bytes with a different md5 and Image.open raised PIL.UnidentifiedImageError: cannot identify image file. The 'wb' file had the identical md5 to the original and opened as (64, 64). Tested: requests 2.34.2, Pillow 13.0.0, Python 3.13.