Skip to content

Instantly share code, notes, and snippets.

@mildsunrise
Created September 16, 2023 19:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mildsunrise/07000dbdcd568edbddeb406992ec5158 to your computer and use it in GitHub Desktop.
Save mildsunrise/07000dbdcd568edbddeb406992ec5158 to your computer and use it in GitHub Desktop.
SIGNALIS save data decoder/encoder
#!/usr/bin/python
"""
signalis.py decode <input.png> <output.json>
signalis.py encode <input.json> <output.png>
the decoder does NOT validate the input image! to do that, encode data again and check image pixels match
"""
import os, sys
import numpy as np
from PIL import Image
def __main__():
args = sys.argv[1:]
if len(args) != 3:
print(__doc__, file=sys.stderr)
exit(1)
mode, infile, outfile = args
{ "decode": decode, "encode": encode }[mode](infile, outfile)
TEMPLATE_PATH = os.path.join(os.path.dirname(__file__), "template.png")
template = np.array(Image.open(TEMPLATE_PATH))
def decode(infile: str, outfile: str):
image = np.array(Image.open(infile, "r", ["png"]))
data = (template != image)[::-1, :, :3].reshape(-1, 8)
data = bytes( sum(int(b[i]) << i for i in range(8)) for b in data )
end = len(data)
while end and data[end - 1] == 0: end -= 1
data = data[:end]
with open(outfile, "wb") as f:
f.write(data)
def encode(infile: str, outfile: str):
image = template.copy()
with open(infile, "rb") as f:
data = f.read()
pixels = image[::-1, :, :3]
values = pixels.reshape(-1)
write_values(values, data)
pixels[:] = values.reshape(pixels.shape)
Image.fromarray(image).save(outfile, "png")
def write_values(values: np.ndarray, data: bytes):
assert not data.endswith(b"\0")
bits = ( (byte >> i) & 1 for byte in data for i in range(8) )
for i, bit in enumerate(bits):
diff = bit * 10
if values[i] + diff > 255:
# adding would cause an overflow; subtract instead
diff *= -1
values[i] += diff
if __name__ == "__main__":
__main__()
@nottellingyouanything
Copy link

What image are you using as template.png, and where can i find it?

@D6player
Copy link

D6player commented Apr 17, 2024

What image are you using as template.png, and where can i find it?
template
You can find it in the internal assets of the game. This one will work fine.

@nottellingyouanything
Copy link

You are a lifesaver, i cannot thank you enough.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment