How to resolve the algorithm Percentage difference between images step by step in the Sidef programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Percentage difference between images step by step in the Sidef programming language
Table of Contents
Problem Statement
Useful for comparing two JPEG images saved with a different compression ratios. You can use these pictures for testing (use the full-size version of each): The expected difference for these two images is 1.62125%
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Percentage difference between images step by step in the Sidef programming language
Source code in the sidef programming language
require('Imager')
func img_diff(a, b) {
func from_file(name) {
%s|Imager|.new(file => name)
}
func size(img) {
(img.getwidth, img.getheight)
}
func pixel_diff(p1, p2) {
[p1.rgba] »-« [p2.rgba] -> map { .abs }.sum
}
func read_pixel(img, x, y) {
img.getpixel(x => x, y => y)
}
var(img1, img2) = (from_file(a), from_file(b))
var(w1, h1) = size(img1)
var(w2, h2) = size(img2)
if ((w1 != w2) || (h1 != h2)) {
return nil
}
var sum = 0
for y,x in (^h1 ~X ^w1) {
sum += pixel_diff(read_pixel(img1, x, y), read_pixel(img2, x, y))
}
sum / (w1 * h1 * 255 * 3)
}
say 100*img_diff('Lenna50.jpg', 'Lenna100.jpg')
You may also check:How to resolve the algorithm Even or odd step by step in the Crystal programming language
You may also check:How to resolve the algorithm Compare a list of strings step by step in the Raku programming language
You may also check:How to resolve the algorithm Sieve of Eratosthenes step by step in the Ursala programming language
You may also check:How to resolve the algorithm QR decomposition step by step in the Axiom programming language
You may also check:How to resolve the algorithm Soloway's recurring rainfall step by step in the Quackery programming language