表格證件照的 3.5 x 4.5 cm 不是像素尺寸:如何換算釐米與 DPI
3.5 x 4.5 cm is not a pixel size: converting cm and DPI for form photos
證件照的 3.5 x 4.5 cm 是列印尺寸,單靠釐米數無法確定像素尺寸;須按 DPI 以「釐米 ÷ 2.54 × DPI」換算。調整照片時應先裁剪至 7 : 9 比例,再縮放至目標像素尺寸;只改 DPI 不會改變像素或檔案大小。
A lot of application forms ask for a passport-style photo of "3.5 x 4.5 cm". An image file has no centimetres in it. It has a width and height in pixels, plus an optional number in the header that says how many of those pixels should go into one inch when it's printed. Here's how I turn a size in cm into pixels, how to do it with free tools, and the one setting that looks like it changes the size but doesn't.
The formula
There are 2.54 cm in an inch, and DPI (dots per inch, often meaning pixels per inch for images) says how many pixels make one inch. So:
pixels = cm / 2.54 * DPI
The same 3.5 x 4.5 cm gives very different pixel sizes depending on the DPI. I ran it in Python and rounded to whole pixels:
$ python px.py
96 DPI: 132.28 x 170.08 -> 132 x 170 px
200 DPI: 275.59 x 354.33 -> 276 x 354 px
300 DPI: 413.39 x 531.50 -> 413 x 531 px
All three are "3.5 x 4.5 cm". The 300 DPI version has about ten times as many pixels as the 96 DPI one (219,303 against 22,440), which matters for both sharpness and file size.
So a size in cm on its own is incomplete. If the form also gives a pixel size, I use the pixel size. If it gives cm and a DPI, I use the formula. If it gives only cm, I look in the notice and the portal's help for a pixel or DPI figure before guessing.
Step 1: crop to the right shape first
3.5 : 4.5 is the same shape as 7 : 9, a ratio of about 0.778. A phone photo is usually 3 : 4 (0.75). If you resize a 3 : 4 photo straight to 413 x 531, the width gets stretched by about 3.7 percent, enough to make a face look wider. So crop to 7 : 9 first, then resize.
For my 3024 x 4032 test image, that meant keeping the full width and cutting the height to 3024 x 9 / 7 = 3888 pixels, taken from the middle. In a real photo you'd place the crop so the face sits where the form's guidelines want it, rather than cutting blindly from the middle.
Step 2: resize and set the DPI with Pillow
This is the script I tested. It crops to 7 : 9 from the centre, resizes to the pixel size for the DPI you give it, and writes that DPI into the JPEG header:
# resize.py: python resize.py input.jpg 300 photo.jpg
import sys
from PIL import Image
src, dpi, out = sys.argv[1], int(sys.argv[2]), sys.argv[3]
w_px = round(3.5 / 2.54 * dpi)
h_px = round(4.5 / 2.54 * dpi)
im = Image.open(src).convert("RGB")
W, H = im.size
if W * 9 > H * 7: # too wide: trim the sides
new_w = H * 7 // 9
left = (W - new_w) // 2
im = im.crop((left, 0, left + new_w, H))
else: # too tall: trim top and bottom
new_h = W * 9 // 7
top = (H - new_h) // 2
im = im.crop((0, top, W, top + new_h))
im = im.resize((w_px, h_px), Image.LANCZOS)
im.save(out, quality=85, dpi=(dpi, dpi))
print(out, im.size)
Output:
$ python resize.py phone.jpg 300 photo-300.jpg
photo-300.jpg (413, 531)
To check what I got, I read the pixel size and the JFIF density (the DPI field in the JPEG header) and worked out the printed size:
photo-96.jpg: 132x170 px, 1,414 bytes, JFIF density 96x96 dots/inch -> prints at 3.49 x 4.50 cm
photo-200.jpg: 276x354 px, 3,236 bytes, JFIF density 200x200 dots/inch -> prints at 3.51 x 4.50 cm
photo-300.jpg: 413x531 px, 5,713 bytes, JFIF density 300x300 dots/inch -> prints at 3.50 x 4.50 cm
The 3.49 and 3.51 come from rounding to whole pixels. My test image is a smooth synthetic picture, so the byte counts are much smaller than a real photo would be, but the direction is the same: more pixels, more bytes.
exiftool shows the same thing if you have it:
$ exiftool -ImageSize -XResolution -YResolution -ResolutionUnit photo-300.jpg
Image Size : 413x531
X Resolution : 300
Y Resolution : 300
Resolution Unit : inches
Doing it without code
- Preview on a Mac: crop with the selection tool, then Tools > Adjust Size. The width and height fields have a unit menu, and there's a Resolution field. Apple's guide says that deselecting "Resample image" makes the dimensions smaller "without losing any detail". In practice that keeps the same pixels and changes only the print size and resolution. For a form photo that needs fewer pixels, keep "Resample image" ticked.
- GIMP: Image > Scale Image changes the pixels (you can type the size in cm and set the resolution there). Image > Print Size only changes the resolution and print size. GIMP's own docs say Print Size "does not change the number of pixels in the image and it does not resample the image".
- Paint on Windows: its Resize dialog works in percent or pixels, so do the cm to pixel maths yourself with the formula above, untick "Maintain aspect ratio" only after you've cropped to the right shape, and type the pixel size.
The gotcha: changing only the DPI does nothing to the pixels or the KB
It's tempting to open the file, change 96 to 300 in a DPI box, and call it done. I tried exactly that on the 96 DPI file by rewriting only the density bytes in the header:
photo-96.jpg: 132x170 px, 1,414 bytes, JFIF density 96x96 dots/inch -> prints at 3.49 x 4.50 cm
photo-96-relabelled-300.jpg: 132x170 px, 1,414 bytes, JFIF density 300x300 dots/inch -> prints at 1.12 x 1.44 cm
cmp -l found 4 bytes different between the two files, all in the header. Same pixels, same file size, and the printed size dropped to 1.12 x 1.44 cm because the same 132 pixels are now spread at 300 per inch. Doing the same relabel with exiftool -XResolution=300 -YResolution=300 -ResolutionUnit=inches gave the same 132 x 170 pixels; the file grew by 88 bytes only because exiftool also added an EXIF block holding the resolution.
So if a portal checks pixel dimensions or a KB range, editing the DPI field changes neither. What actually changes them is resampling to a new pixel size, and then compressing if the KB is still too high.
A browser option
I built ToolOcean, and its resizer page takes the size in px, cm, mm or inches with a DPI (96 by default, with 200 and 300 presets), does the conversion in the browser without uploading the photo, and stores that DPI in the JFIF header of a JPEG download. A PNG download keeps pixels only. It doesn't crop, so crop to the right shape first: toolocean.co/image-resizer.
Drafted with AI help, tested and checked by me.
來源:DEV Community · dev.to