How to convert a JPG to Base64
- Drop a
.jpgor.jpegfile, pick it with the file picker, or paste a copied image with Ctrl+V. - For photos straight from a camera or phone, set Max width (for example 1200) and adjust Quality. The default is 85%.
- Tick Strip metadata (EXIF) if the photo will be shared or published.
- Choose Output as: Data URI is the default, Plain Base64 is what most APIs want. Then Copy output.
The preview shows pixel dimensions and, after re-encoding, the new file size, so you can try a few settings and watch the number drop.
What is inside a JPG’s Base64
All JPEG files start with the SOI marker FF D8 FF, so they all start with /9j/ in Base64. The next characters tell you which header follows:
| File starts with | Base64 starts with | Typical source |
|---|---|---|
FF D8 FF E0 + JFIF | /9j/4AAQSkZJRg | image editors, web exports, most converters |
FF D8 FF E1 + Exif | /9j/4 and then varying characters | cameras and phones (EXIF block first) |
FF D8 FF DB | /9j/2w | stripped or minimal encoders |
A camera JPEG carries an EXIF block with the device model, capture time, orientation and, if location was on, GPS coordinates. Base64 hides none of it. Anyone who decodes the string with Base64 to JPG gets the original file with the metadata intact.
Shrinking photos before you encode them
A modern phone photo is several megabytes. As Base64 it grows by another third, which is too much for a JSON request, a CSS file or an HTML email, and slow everywhere else.
Two settings do most of the work:
- Max width sets the maximum width in pixels. Halving the width cuts the pixel count to a quarter.
- Quality controls JPEG compression. It takes effect whenever the photo is re-encoded, that is, with a Max width, a Convert to format or Strip metadata (EXIF) set. Between 75% and 85% the difference is hard to see on a photo, while the file gets much smaller than at 95–100%.
Re-encoding a JPEG always loses a little detail, because it is decoded and compressed again. Do it once, from the original, and avoid running the result through the converter a second time.
JPEG has no transparency. If you convert a PNG or WebP with a transparent background to JPEG, the transparent parts become white. Keep PNG or WebP when you need the alpha channel.
Where JPG Base64 is used
- Photo uploads through JSON APIs, including AI vision models that describe or analyze images. Many of them downscale large images on their side, so sending a smaller JPEG speeds up the request without a visible loss.
- Inline thumbnails and placeholders in HTML, such as a 20-pixel-wide blurred preview shown while the full image loads.
- Single-file documents, for example an HTML report with a few photos embedded.
For large photos on a website, a normal image file is still better: it can be cached, lazy-loaded and served in several sizes with srcset.
Resize and encode in Python
Pillow resizes and re-encodes the photo in a few lines, and the standard library does the Base64. Pillow does not copy EXIF to the new file unless you pass it explicitly, so this also strips metadata:
import base64, io
from PIL import Image
with Image.open("photo.jpg") as im:
im.thumbnail((1024, 1024))
buf = io.BytesIO()
im.save(buf, "JPEG", quality=80)
b64 = base64.b64encode(buf.getvalue()).decode("ascii")
More on reading files and streaming in Base64 in Python.
Common problems
- The photo shows up rotated. Many phones save the pixels in sensor orientation and put the rotation into an EXIF tag. Current browsers apply that tag, and when the tool redraws a photo it keeps the visible orientation. Software that ignores EXIF can show the raw orientation.
- The Base64 is longer than expected. Check the input size in the meter. A “small” photo from a messenger can still be 3000 pixels wide.
- An API rejects the image. Confirm whether it wants the
data:image/jpeg;base64,prefix or bare Base64, and sendimage/jpegas the type, notimage/jpg.
FAQ
Is it image/jpg or image/jpeg in a data URI?
image/jpeg. image/jpg is not a registered type. Browsers display the picture anyway because they sniff image bytes, but APIs that validate the media type may reject it.
Does a Base64 JPG still contain my GPS location?
Yes, if the original did. Base64 copies every byte, EXIF included. Tick Strip metadata (EXIF) to remove it; the photo is redrawn without metadata before encoding.
Why does my JPG Base64 start with /9j/?
Every JPEG begins with the bytes FF D8 FF, and those three bytes encode to exactly /9j/. A string that starts differently is not a JPEG, whatever its file name says.
How can I make the Base64 of a photo shorter?
Reduce the pixels and the quality before encoding. A Max width that matches the display size and a Quality around 75–85% shrink a phone photo far more than anything you can do to the Base64 itself.