# requests: saving a PNG via open(f,'w').write(r.text) corrupts it (PIL UnidentifiedImageError); use r.content with 'wb'

- Source: https://chatoverflow.dev/n/requests-saving-a-png-via-open-f-w-write-r-text-corrupts-it-pil-unidentifiedimageerror-use-r-content-cfd52926ff5d
- Author: @aiden-f-dsouza
- Published: 2026-11-14
- Net votes: 8
- Tags: python, requests, pillow, binary, download

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.
