okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
736266e87
main
1"""tools/image/gate/diff_where.py - TEST-ONLY (OWN-5 gate harness): where two picture sets differ, and what the user 2sees after the server's compositing. 3 4python -I -B diff_where.py <pics root> <tagA> <tagB> 5 6Per pair (p*_s*.png in both): the largest absolute difference over R, G, B and alpha with its pixel (row, column) and 7channel, whether that pixel lies within 16 px of the border, the share of pixels whose largest RGB or alpha difference 8is above 8 / 255, and the RGB PSNR of the RGBA pictures and of the pictures as the server sends them by default 9(serve/images.py flatten_png: on white, 8-bit RGB). The last lines are the worst picture of the set by RGB PSNR.""" 10from __future__import annotations 11 12import io 13import math 14import sys 15from pathlib import Path 16 17import numpy as np 18from PIL import Image 19 20sys .path .insert (0 ,str (Path (__file__ ).resolve ().parents [3 ])) 21from serve .images import flatten_png # noqa: E402 22 23CH = "RGBA" 24 25 26def psnr (a :np .ndarray ,b :np .ndarray )-> float : 27d = a .astype (np .float64 )- b .astype (np .float64 ) 28mse = float ((d * d ).mean ()) 29return math .inf if mse == 0 else 10 * math .log10 (255 * 255 / mse ) 30 31 32def one (pa :Path ,pb :Path )-> dict : 33A = np .asarray (Image .open (pa ).convert ("RGBA" )).astype (np .int16 ) 34B = np .asarray (Image .open (pb ).convert ("RGBA" )).astype (np .int16 ) 35d = np .abs (A - B ) 36y ,x ,c = np .unravel_index (int (np .argmax (d )),d .shape ) 37H ,W = d .shape [:2 ] 38fa = np .asarray (Image .open (io .BytesIO (flatten_png (pa .read_bytes ()))).convert ("RGB" )) 39fb = np .asarray (Image .open (io .BytesIO (flatten_png (pb .read_bytes ()))).convert ("RGB" )) 40return {"name" :pa .name ,"max" :int (d .max ()),"at" : (int (y ),int (x )),"channel" :CH [c ], 41"border" :bool (min (y ,x ,H - 1 - y ,W - 1 - x )< 16 ), 42"max_rgb" :int (d [..., :3 ].max ()),"max_alpha" :int (d [...,3 ].max ()), 43"pct_rgb_over8" :float ((d [..., :3 ].max (axis = 2 )> 8 ).mean ()* 100 ), 44"pct_alpha_over8" :float ((d [...,3 ]> 8 ).mean ()* 100 ), 45"psnr_rgb" :psnr (A [..., :3 ],B [..., :3 ]),"psnr_flat" :psnr (fa ,fb ), 46"flat_max" :int (np .abs (fa .astype (np .int16 )- fb .astype (np .int16 )).max ())} 47 48 49def main (argv )-> int : 50root ,ta ,tb = Path (argv [0 ]),argv [1 ],argv [2 ] 51rows = [] 52for pa in sorted ((root / ta ).glob ("p*_s*.png" )): 53pb = root / tb / pa .name 54if pb .is_file (): 55r = one (pa ,pb ) 56rows .append (r ) 57f" { r [ 'name' ] } : max |d| { r [ 'max' ] } at row { r [ 'at' ][ 0 ] } col { r [ 'at' ][ 1 ] } channel { r [ 'channel' ] } " 58f" { ' (border)' if r [ 'border' ] else '' } ; RGB max { r [ 'max_rgb' ] } , alpha max { r [ 'max_alpha' ] } ; " 59f"pixels > 8: RGB { r [ 'pct_rgb_over8' ]:.4f } %, alpha { r [ 'pct_alpha_over8' ]:.4f } %; " 60f"PSNR RGBA-pictures { r [ 'psnr_rgb' ]:.2f } dB, flattened on white { r [ 'psnr_flat' ]:.2f } dB " 61f"(max |d| { r [ 'flat_max' ] } )" ) 62if not rows : 63"no pairs" ) 64return 1 65w = min (rows ,key = lambda r :r ["psnr_rgb" ]) 66f"WORST { ta } vs { tb } : { w [ 'name' ] } PSNR { w [ 'psnr_rgb' ]:.2f } dB (flattened { w [ 'psnr_flat' ]:.2f } dB)" ) 67fl = [r ["psnr_flat" ]for r in rows if math .isfinite (r ["psnr_flat" ])] 68f"FLATTENED { ta } vs { tb } : n { len ( rows ) } , PSNR worst { min ( fl ):.2f } mean { np . mean ( fl ):.2f } dB" ) 69return 0 70 71 72if __name__ == "__main__" : 73sys .exit (main (sys .argv [1 :]))