- Published
- Author
- Syed SibtainSystem Analyst
The Problem: When comparing images for UI testing, we needed to implement a
Command Line approach
The Mini Magick Solution
Key Insight: Fuzz Factor = Threshold
- In ImageMagick:
- In MiniMagick:
#ruby #image
fuzz factor to ignore small colour differences (like anti-aliasing, compression artifacts, or minor rendering variations). This is crucial for UI screenshots where a 1-2 pixel difference shouldn't count as a "failure."Command Line approach
Code
# This works but is fragile
magick compare -metric AE -fuzz 10% image1.png image2.png NULL: 2>&1
# Problems:
# - Shell command parsing issues
# - Error handling is difficult
# - Output parsing is brittle
# - Cross-platform compatibility issues
# - Hard to debug when things go wrongThe Mini Magick Solution
Code
# Step 1: Create difference image
difference_image = first_image.composite(second_image) do |c|
c.compose "difference"
end
# Step 2: Apply fuzz factor using threshold
thresholded_diff = difference_image.dup
thresholded_diff.combine_options do |c|
c.normalize
c.threshold "10%"
# This acts as our fuzz factor!
end
# Step 3: Get statistics
mean_value = thresholded_diff.identify do |c|
c.format "%[fx:mean]"
end
# Step 4: Convert to percentage
percentage = mean_value.to_f * 100Key Insight: Fuzz Factor = Threshold
- In ImageMagick:
fuzz 10% tells it to ignore differences below 10%- In MiniMagick:
threshold10% does the same thing by setting pixels below 10% to black#ruby #image