How to download image using requests
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
Downloading an image with Python requests is easy for a quick script, but a reliable downloader needs a bit more structure than get and write. Timeouts, streaming, content validation, and safe file handling matter if you want the code to behave well when servers are slow, responses are wrong, or files are larger than expected.
The Simplest Working Download
For a small trusted image, the basic pattern is:
This is enough for throwaway scripts, but it loads the entire response into memory. That is fine for tiny files and not ideal for large images or large batches.
Stream the Response Instead of Buffering Everything
For a better default, request the response as a stream and write it in chunks.
This keeps memory usage low and scales much better when you are downloading many files or working with high-resolution images.
Validate That the Response Is Actually an Image
Some endpoints return an HTML error page, rate-limit message, or JSON payload while still producing a successful status code. A quick content-type check catches a lot of bad data early.
That one validation step is worth it in scraping, ETL, and dataset-building jobs where silent bad downloads become expensive later.
Add Retries for Temporary Failures
Transient network failures happen. A requests.Session with bounded retry logic is a good improvement for batch jobs.
You can then reuse the session:
Retries should be limited. Too many retries just make a failing job slower and harder to diagnose.
Use Safe Output Paths
For repeated downloads, do not assume the URL path is already a safe filename. Derive a usable name and write to a temporary file first if partial downloads would be a problem.
An atomic-write pattern is even safer:
That prevents half-written files from being mistaken for valid completed downloads.
Optional Post-Download Validation
If the next step assumes the file is a valid image, verify it after download. One common choice is Pillow.
This is useful when building image datasets, media ingestion pipelines, or upload workflows where corrupt files should fail immediately.
Common Pitfalls
The most common mistake is forgetting to set a timeout, which can leave the script hanging indefinitely. Another is using response.content for large files and creating avoidable memory pressure. Developers also trust status codes too much and skip content-type checks, which lets HTML or JSON responses slip into image directories unnoticed. Finally, writing directly to the final filename can leave broken partial files behind when a download is interrupted.
Summary
- Use
requests.get(..., timeout=...)withraise_for_status()as the baseline. - Prefer streaming with
iter_contentfor real downloads. - Check
Content-Typeso you do not store non-image responses as images. - Add bounded retries when downloads happen in batches or unstable networks.
- Use safe filenames and temporary files to avoid partial-download corruption.
Related reading
- How to drop a list of rows from Pandas dataframe?
- How to drop rows of Pandas DataFrame whose value in a certain column is NaN
- How to drop rows of Pandas DataFrame whose value in a certain column is NaN
- How to dump a dict to a JSON file?
- How to dynamically create a class?
- How to dynamically load a Python class
- How to efficiently compare two unordered lists not sets?
- How to efficiently compare two unordered lists not sets?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.