Which method to choose
| Method | Best for | How it works |
|---|---|---|
| Threshold | Logos, scans, text with a known background | Every pixel brighter than the value becomes white, the rest black. |
| Otsu | Scanned documents and OCR, batches of different images | Picks the threshold that best separates the two humps of the histogram, so each image gets its own value. |
| Dither | Photos on thermal printers, e-ink, engraving | Floyd–Steinberg: spreads each pixel's rounding error to its neighbours, so dot density follows brightness. |
On the sample images Otsu picks 97 for the sunset photo, 175 for the logo on white, 75 for the portrait, as measured in the black and white guide. One fixed threshold cannot suit them all, which is why Otsu is the default here.
What the script handles for you
- Phone photos stored sideways are turned upright with
ImageOps.exif_transpose()first, so the result matches what you see. - 16-bit images (scientific cameras, depth maps, medical scans) are scaled to 8 bits before thresholding. A plain
convert("L")clips every value above 255, so a 16-bit image comes out almost entirely white. - Animated GIFs: only the first frame is converted. To process every frame, loop over
ImageSequence.Iterator(img)as in the animated GIF guide.
1-bit files are tiny
Mode "1" stores one bit per pixel. Saved as PNG the sample photo shrinks from about 510 KB as greyscale PNG to
about 20 KB, and to about 11 KB as a Group 4 fax TIFF (measured in the
black and white guide). Save with compression="group4" for TIFF.
How exact is the preview?
Threshold and Otsu previews use Pillow's own greyscale formula (ITU-R 601-2 luma, in the same fixed-point arithmetic) and the same Otsu code as the script, so they match Pillow. The dithering preview uses the standard Floyd–Steinberg weights; Pillow's implementation differs in small details, so individual dots can differ while the overall look is the same. Press Run with real Pillow for the exact file.