DEV Community

StagingBench Team
StagingBench Team

Posted on AI-assisted

Compare Virtual Staging Image Sizes with Python and Counter

A staged room photo comes with a width and a height, just like the original. Python's standard library is enough to compare their total pixel counts and summarize the results by tool. The example below embeds 16 records drawn from the 120 test records with known original size in the public StagingBench data. It includes each of the 13 anonymous tools and keeps multiple rows for T07 and T09 so you can see how a single tool can contribute to several categories.

Each tuple stores the tool ID, the staged width and height, and the original width and height in that order. The loop unpacks those values and multiplies each width by its corresponding height. It passes the resulting integer pixel counts to Fraction, with the staged count as the numerator and the original count as the denominator. This computes the ratio directly from the dimensions and preserves it exactly through the comparison.

For example, both T07 rows in the sample have an output of 1448 by 1086, which gives 1,572,528 pixels. The first original has 1,228,800 pixels, so the ratio is about 1.28. The second original has 1,920,000 pixels, so the ratio is about 0.82. The output dimensions stayed the same across the two records. The original dimensions determined which category each row entered.

The if and elif branches handle ratios below 1 and exactly equal to 1. The remaining branch handles ratios above 1. In this program, identical means identical total pixel counts. Images with equal counts could have different dimensions, and even matching width and height tell you only about the size of each image.

A defaultdict creates a fresh Counter when the loop first encounters a tool ID. Each row adds one count to exactly one category for that tool. During printing, a Counter returns zero for any category it has never seen. That lets the script print all three columns for every tool with the same statement. Sorting the dictionary items by tool ID gives the printed table a predictable order.

Read each row across to see how the sample records were classified. T07 contributes one smaller and one larger image. T09 contributes one image to each of the three categories. Across the sample, six images are smaller, two have identical pixel counts, and eight are larger. These are results for the embedded records. To use the same calculation with your own photos, supply one tuple per original and staged pair with positive pixel dimensions. The counting loop and print statements can stay as written.

from collections import Counter, defaultdict
from fractions import Fraction

records = [
    ("T01", 2336, 1760, 1280, 960),
    ("T02", 1195, 896, 1280, 960),
    ("T03", 1280, 960, 1280, 960),
    ("T04", 2048, 1536, 1280, 960),
    ("T05", 2400, 1792, 1280, 960),
    ("T06", 2528, 1696, 1280, 960),
    ("T07", 1448, 1086, 1280, 960),
    ("T07", 1448, 1086, 1600, 1200),
    ("T08", 2048, 1536, 1280, 960),
    ("T09", 1360, 1024, 1280, 960),
    ("T09", 1280, 960, 1280, 960),
    ("T09", 1360, 1024, 1600, 1200),
    ("T10", 684, 512, 1280, 960),
    ("T11", 1184, 864, 1280, 960),
    ("T12", 2390, 1792, 1280, 960),
    ("T13", 1200, 896, 1280, 960),
]

counts = defaultdict(Counter)
for tool, out_w, out_h, orig_w, orig_h in records:
    ratio = Fraction(out_w * out_h, orig_w * orig_h)
    if ratio < 1:
        category = "smaller"
    elif ratio == 1:
        category = "identical"
    else:
        category = "larger"
    counts[tool][category] += 1

print(f"Records: {len(records)}")
print("Tool Smaller Identical Larger")
for tool, totals in sorted(counts.items()):
    print(tool, totals["smaller"], totals["identical"], totals["larger"])
Enter fullscreen mode Exit fullscreen mode

Output:

Records: 16
Tool Smaller Identical Larger
T01 0 0 1
T02 1 0 0
T03 0 1 0
T04 0 0 1
T05 0 0 1
T06 0 0 1
T07 1 0 1
T08 0 0 1
T09 1 1 1
T10 1 0 0
T11 1 0 0
T12 0 0 1
T13 1 0 0
Enter fullscreen mode Exit fullscreen mode

Top comments (0)