From c57e31bae626cdaa278757cf0808dfadc16428e1 Mon Sep 17 00:00:00 2001 From: Timothee Groleau Date: Tue, 19 May 2020 11:41:46 +0800 Subject: [PATCH] Update compute_palette_script to sample 3 nes logical pixels --- nestris_ocr/palettes/easiercap.json | 2 +- nestris_ocr/palettes/easiercap.png | Bin 1314 -> 1328 bytes scripts/compute_color_palette.py | 47 ++++++++++++++++------------ 3 files changed, 28 insertions(+), 21 deletions(-) diff --git a/nestris_ocr/palettes/easiercap.json b/nestris_ocr/palettes/easiercap.json index 6107b3a..4ec65f3 100644 --- a/nestris_ocr/palettes/easiercap.json +++ b/nestris_ocr/palettes/easiercap.json @@ -1 +1 @@ -[[[64, 38, 234], [83, 168, 255]], [[0, 129, 0], [124, 201, 1]], [[142, 9, 185], [214, 93, 255]], [[63, 39, 231], [81, 212, 39]], [[157, 16, 88], [59, 208, 108]], [[61, 208, 109], [133, 131, 255]], [[150, 36, 0], [89, 90, 87]], [[99, 15, 222], [85, 0, 18]], [[64, 39, 234], [154, 41, 1]], [[151, 37, 0], [215, 136, 26]]] \ No newline at end of file +[[[65, 40, 228], [86, 168, 254]], [[3, 127, 1], [125, 199, 6]], [[139, 14, 179], [211, 96, 255]], [[64, 41, 225], [85, 210, 45]], [[154, 19, 87], [64, 205, 111]], [[65, 206, 112], [134, 131, 255]], [[146, 38, 2], [90, 91, 87]], [[97, 18, 215], [82, 0, 20]], [[64, 40, 227], [151, 43, 4]], [[147, 39, 3], [211, 137, 32]]] \ No newline at end of file diff --git a/nestris_ocr/palettes/easiercap.png b/nestris_ocr/palettes/easiercap.png index 33eaa92155035560fc5c00477e32042261a92816..027e42c1293f251d4c9903f8e841a6a20b43b8aa 100644 GIT binary patch literal 1328 zcmeAS@N?(olHy`uVBq!ia0vp^X$%ZZY#dBLmgcowHwFfl@18D>Ar*7p-d%s{Lb%NF zkDq5J7dncYn^?Mrm9h4VZ!ijQ?4D-&Uu5wK37%tJI}{?;%-YM4>n&yS;#%8ksW-nW zn3wOl{F8J2b-Wnh&#C`W_;o_3ei;9cKOWOOX$je^x5)0? zyY`;J$A{Ms8y;J`e}9y1$|2p;Dzt^ovbX;I`StM?udw<3|0c&ODt{Jyoxr(WXEJRe zPg#bw#ozltYyIP8<&)=Be?0t3Q8+JBjkZwo&4sf6H(IYstJPKh&gd!p@a7l2to`=0 z7ijIgKNaacAAje!uWt9*kvcPlws3>~YUOz|EbUe)y zn37{He`S4b>tuiL*Kb%ScYlqX(Zgw}lV}SM^5wEGzqbaMfxdh#PwuHs-xou#4D{>Y w31CVN|NqnASjErdomZXJ?rid*F?=K3QmX&>(j(u8z><)`)78&qol`;+0DEXgSpWb4 literal 1314 zcmeAS@N?(olHy`uVBq!ia0vp^X$%ZZY#dBLmgcowHwFflm!2+;Ar*7p-d*@=Myf>n z!{?i4%Ct4Q75>|}wtL4m9VNemTTBX%>U8upo>a0BxK_@!tur$>Q6qRGm-j5O=bU?< z`n_Ksb6xPyj>Ez8AK!{T|ExytUs+|EUB!o_?-N?5m+}92KT&5gEg_HJiy7<7{Ud&5 zr9C_Ce*B#*cV46#ZK33x3m@N}wtshT!-u1vUmJfuz5L&_|N0)&JZTBp)Jyi1f4u&V zm)qX9HaG2FebL716HU>yv=U@C(AqouOOn60wa%`$pE0vsu6`e#tljhb$2X8~AMb)SE| gQ>1;4jr^wH@K5{X=C?9?fn^_qr>mdKI;Vst0M21DQ2+n{ diff --git a/scripts/compute_color_palette.py b/scripts/compute_color_palette.py index 92ec883..9882d13 100644 --- a/scripts/compute_color_palette.py +++ b/scripts/compute_color_palette.py @@ -4,7 +4,7 @@ from PIL import Image import numpy as np from nestris_ocr.colors import Colors -from math import sqrt, floor +from math import sqrt def getAverageColor(pixels): @@ -26,18 +26,20 @@ def getColors(level, field): # perfect calibration implies capturing the right and bottom black edges # still, since we're capturing a safe zone, things should still work with small discrepancies + nes_pixels_to_sample = ( + (2, 4.5), + (4.5, 2), + (4.5, 4.5), + ) + nes_pix_xsize = img.width / 80 nes_pix_ysize = img.height / 160 spanx = nes_pix_xsize * 8 spany = nes_pix_ysize * 8 - # floor() to err on size of caution and not accidentally capture edges - capx = floor(nes_pix_xsize * 2) - capy = floor(nes_pix_ysize * 2) - - corner_highlight = Image.new("RGBA", (2, 2), (255, 0, 0, 128)) - capture_highlight = Image.new("RGBA", (capx, capy), (0, 255, 0, 128)) + corner_highlight = Image.new("RGBA", (1, 1), (255, 0, 0, 128)) + capture_highlight = Image.new("RGBA", (1, 1), (0, 255, 0, 128)) np_img = np.array(img, dtype=np.uint16) @@ -48,25 +50,30 @@ def getColors(level, field): for y in range(20): for x in range(10): - xidx = round(spanx * x) - yidx = round(spany * y) + xidx = spanx * x + yidx = spany * y # print the top-left corner - res_img.paste(corner_highlight, (xidx, yidx), corner_highlight) + res_img.paste( + corner_highlight, (round(xidx), round(yidx)), corner_highlight + ) - xidx = round( - spanx * x + nes_pix_xsize * 3.5 - ) # 3.5 to favour capturing on the right - yidx = round(spany * y + nes_pix_xsize * 3) + pixels = [] - res_img.paste(capture_highlight, (xidx, yidx), capture_highlight) + for offsetx, offsety in nes_pixels_to_sample: + right = round(xidx + nes_pix_xsize * offsetx) + top = round(yidx + nes_pix_ysize * offsety) + left = round(xidx + nes_pix_xsize * (offsetx + 1)) + bottom = round(yidx + nes_pix_ysize * (offsety + 1)) - pixels = [] + cap = capture_highlight.resize((left - right, bottom - top)) + + res_img.paste(cap, (right, top), cap) - # grab all pixels in the capture area - for i in range(xidx, xidx + capx): - for j in range(yidx, yidx + capy): - pixels.append(np_img[j, i]) + # grab all pixels in the capture area + for i in range(right, left): + for j in range(top, bottom): + pixels.append(np_img[j, i]) pix = getAverageColor(pixels)