-
Notifications
You must be signed in to change notification settings - Fork 90
/
Copy pathimageCompare.ts
36 lines (34 loc) · 1.16 KB
/
imageCompare.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import { asInputFile, Call, MagickFile, blobToString, MagickInputFile } from '..'
/**
* Compare the two images and return true if they are equal visually. Optionally, a margin of error can be provided using `fuzz`
*/
export async function compare(img1: MagickFile | string, img2: MagickFile | string, fuzz: number = 0.015): Promise<boolean> {
const identical = await compareNumber(img1, img2)
return identical <= fuzz
}
export async function compareNumber(img1: MagickFile | string, img2: MagickFile | string): Promise<number> {
const imgs: MagickInputFile[] = []
let name1: string
let name2: string
if (typeof img1 !== 'string') {
const inputFile = await asInputFile(img1)
imgs.push(inputFile)
name1 = inputFile.name
}
else {
name1 = img1
}
if (typeof img2 !== 'string') {
const inputFile = await asInputFile(img2)
imgs.push(inputFile)
name2 = inputFile.name
}
else {
name2 = img2
}
const result = await Call(imgs,
['convert', name1, name2, '-resize', '256x256^!', '-metric', 'RMSE', '-format', '%[distortion]', '-compare', 'info:info.txt'],
)
const n = await blobToString(result[0].blob)
return parseFloat(n)
}