Artificial Intelligence 7 min read

QHBinimageMan: A Swift macOS Tool for Converting Color Images to Grayscale and Binary

This article introduces QHBinimageMan, a Swift‑based macOS utility that demonstrates the full pipeline from color image to grayscale and finally to binary image generation, covering pixel handling, histogram analysis, thresholding, resolution reduction, and a practical live‑streaming gift animation application.

Sohu Tech Products
Sohu Tech Products
Sohu Tech Products
QHBinimageMan: A Swift macOS Tool for Converting Color Images to Grayscale and Binary

The article presents QHBinimageMan, a macOS tool implemented in Swift that converts color images to grayscale and then to binary (black‑and‑white) images, allowing users to control threshold and resolution scaling.

On macOS/iOS, the tool reads RGB data by converting NSImage or UIImage to CVPixelBuffer using the CoreVideo framework. Sample code creates a pixel buffer and accesses its base address:

// Create CVPixelBuffer
var temp_pb: CVPixelBuffer?
let r = CVPixelBufferCreate(kCFAllocatorDefault, w, h, kCVPixelFormatType_32ARGB, opt, &temp_pb);

// Read original data
let pb_data = CVPixelBufferGetBaseAddress(pb)

To obtain a grayscale image, the RGB components are combined using the BT.709 luminance formula (Y = 0.2126 R + 0.7152 G + 0.0722 B). The resulting luminance value replaces the three color channels:

// rgb → y value
let red = Float(data[offset+1]) * 0.2126
let green = Float(data[offset+2]) * 0.7152
let blue = Float(data[offset+3]) * 0.0722
let v = UInt8(red + green + blue)

// set rgb to y value
data[offset+1] = v
data[offset+2] = v
data[offset+3] = v

A histogram of the grayscale image (split into 16 intensity bins) helps visualize the distribution and select an appropriate threshold for binarization. The tool generates this histogram and shows that most intensity values cluster around black (0), white (256), and mid‑gray (128).

Thresholding converts the grayscale image to a binary image by marking pixels whose intensity falls within a user‑defined range as 1 (white) and others as 0 (black). Example code:

let v = data[offset+1]
// Threshold range check & record
if v > minbin && v < maxbin {
    matrixLine += "1"
} else {
    matrixLine += "0"
}

Different threshold ranges produce varying binary results, as illustrated with sample images. The article recommends copying the generated binary image into a text editor (e.g., Sublime Text) to view the 0‑1 pattern clearly.

For resolution reduction, the tool crops the image by skipping rows and columns according to a scaling factor, effectively down‑sampling while preserving the overall shape. Sample code:

// Scaling factor
let ss = max(ratio, 1)

// Crop rows and columns
for y in 0..
The reduced‑resolution image is used for histogram and binary generation because full‑resolution images are too large to preview conveniently.
As a practical application, the binary image can drive a live‑streaming gift animation: low‑level gifts are represented by small icons placed at the ‘1’ positions of the binary map, creating a composite animation without hard‑coding each frame. This approach simplifies updates and allows easy modification of the binary pattern.
Links to the source code and related articles are provided at the end of the document.
image-processingSwiftmacOSbinary imagehistogramresolution reductionthresholding
Sohu Tech Products
Written by

Sohu Tech Products

A knowledge-sharing platform for Sohu's technology products. As a leading Chinese internet brand with media, video, search, and gaming services and over 700 million users, Sohu continuously drives tech innovation and practice. We’ll share practical insights and tech news here.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.