Skip to content

Commit

Permalink
Update compute_palette_script to sample 3 nes logical pixels
Browse files Browse the repository at this point in the history
  • Loading branch information
timotheeg committed May 19, 2020
1 parent 5dfbc31 commit c57e31b
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 21 deletions.
2 changes: 1 addition & 1 deletion nestris_ocr/palettes/easiercap.json
Original file line number Diff line number Diff line change
@@ -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]]]
[[[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]]]
Binary file modified nestris_ocr/palettes/easiercap.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
47 changes: 27 additions & 20 deletions scripts/compute_color_palette.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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)

Expand All @@ -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)

Expand Down

0 comments on commit c57e31b

Please sign in to comment.