Batch Converting iPhone HEIC Photos to JPEG with Python
iPhone photos are saved as HEIC by default. The format is efficient, but a lot of software still cannot open it: older Windows installs, some web forms, many image editors and plenty of upload widgets. When you have a folder with hundreds of HEIC files spread across subfolders, converting them one by one is not a realistic option. I built heictojpeg to handle that case: point it at a folder, and it writes JPEG copies into another folder with the same layout.
Why I built it
The task I wanted to automate was simple to state: take everything under a Heic folder and produce a matching tree of JPEG files. Online converters are awkward for this. They usually work one file at a time, and you are uploading personal photos to a service you do not control. A local script avoids both problems.
I also wanted a few details to be right from the start, because they are the things that go wrong in quick scripts: photos should not come out rotated the wrong way, metadata should not disappear, and re-running the tool should not redo work or clobber files.
What it does
The command-line tool is a single script, heic_to_jpeg.py. You run it like this:
python heic_to_jpeg.py <input_folder> <output_folder> [--quality 1-100] [--overwrite] [--workers N]
- It walks the input folder and all of its subfolders, finds the HEIC images and converts each one to JPEG.
- The output folder mirrors the input directory structure, so a photo in
trip/day1ends up intrip/day1under the output folder. - EXIF metadata is kept, and the orientation is applied to the pixels.
--qualitysets the JPEG quality and defaults to 90.- By default, files that already exist in the output are skipped.
--overwritereplaces them. --workerssets how many conversions run in parallel. The default is 1.- If two files would produce the same output name, for example
a.heicanda.HEIC, they are saved asa.jpganda_1.jpginstead of one overwriting the other. - At the end it prints a summary, and the exit code is 1 if any file failed.
A typical run looks like this:
python heic_to_jpeg.py /path/to/heic_images /path/to/jpeg_output --quality 85 --workers 4
There is also a small web version. index.html is an upload page, and api/convert.py is a Python serverless function that converts an uploaded image to JPG and returns it for download. It accepts HEIC and also other formats such as PNG and WebP.
How it works
The stack is Python with two libraries: pillow for image handling and pillow-heif for HEIC support. Pillow does not read HEIC on its own. The pillow-heif package plugs into Pillow so that opening a HEIC file works like opening any other image, and the rest of the code is ordinary Pillow.
Orientation and EXIF
iPhones often store photos in the sensor's native orientation and record the intended rotation in an EXIF tag. A viewer that ignores the tag shows the photo sideways. The tool applies the orientation to the pixel data so the JPEG looks correct everywhere, and it keeps the EXIF metadata in the output. This is the reusable lesson: if you strip or ignore orientation during conversion, you get rotated photos; if you apply the rotation but leave the old tag in place, some viewers can rotate the image a second time. It is worth checking how your code handles both sides of that.
Preserving the directory tree
For each input file, the output path is computed relative to the input root and placed under the output root. Parent folders are created as needed. Keeping the structure means the output can be dropped in as a replacement for the original tree, and it avoids a different problem: a flat output folder would force name collisions between files like IMG_0001.HEIC from different directories.
Collisions and re-runs
Case-insensitive extensions create a subtle issue. a.heic and a.HEIC in the same folder both map to a.jpg. Rather than silently losing one, the tool appends a numeric suffix. Separately, skipping existing outputs by default makes the tool safe to re-run after an interruption: it picks up what is missing. --overwrite is there for when you want to regenerate everything, for example at a different quality setting.
Parallel conversions
Decoding HEIC and encoding JPEG is CPU-bound, so --workers lets you spread the files across several parallel conversions. I left the default at 1 so the behavior is predictable, and you opt in to more.
Exit codes and the summary
A batch tool that always exits 0 is hard to use in scripts. The tool prints a summary at the end and returns exit code 1 if any file failed, so a shell script or CI job can detect problems without parsing output.
The web app
The web version is deliberately small: a static HTML page and one Python serverless function, deployed on Vercel with no build settings. You can import the repository into Vercel or run vercel from the project folder, and vercel dev serves both the page and the API locally. The function reuses the same idea as the command-line tool: open the image with Pillow, convert, return a JPG.
There is a real constraint here. Vercel limits request bodies to 4.5 MB, so uploads are capped at about 4 MB. That is fine for many single photos but not for large originals, and it is the main reason the command-line tool is the better choice for bulk work.
Tests
The project has a test suite run with pytest, with its dependencies listed in requirements-dev.txt. Install them and run pytest from the project folder.
What I learned
- The defaults matter more than the options. Skipping existing files, handling name collisions and returning a meaningful exit code are small features, but they are what make a script safe to run on a large folder without supervision.
- Metadata needs deliberate handling. Keeping EXIF and applying orientation are two separate decisions, and both affect how the result looks in other software.
- Serverless limits shape the product. The 4.5 MB request limit decided what the web app is good for. Knowing that early kept me from promising bulk conversion in the browser.
- Keep the surface small. One script, two dependencies and a handful of flags is easy to explain and easy to test.
Conclusion
I built heictojpeg to solve one annoying problem: getting a whole tree of iPhone photos into a format that every tool accepts, without losing orientation or metadata. The command-line script does the bulk work, and the small Vercel page covers the occasional single file. If you are writing a similar converter, the parts I would copy are the mirrored output tree, the collision handling and the honest exit code.