Created
January 18, 2018 14:49
-
-
Save aNNiMON/dc0335b70c3cea46203c99b3efb08ade to your computer and use it in GitHub Desktop.
monika.chr code decoder and encoder | Doki Doki Literature Club
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
import getopt | |
import sys | |
from PIL import Image | |
def usage(): | |
print('Usage: monika_decode.py [-v] [FILE]') | |
sys.exit(2) | |
def main(argv): | |
if len(argv) == 0: | |
usage() | |
verbose = False | |
input = '' | |
for arg in argv: | |
if arg == '-v': | |
verbose = True | |
else: | |
input = arg | |
if len(input) == 0: | |
usage() | |
image = Image.open(input) | |
width, height = image.size | |
i = 0 | |
value = 0 | |
bits = '' | |
text = '' | |
for y in range(height): | |
for x in range(width): | |
pixel = image.getpixel((x, y)) | |
mean = (pixel[0] + pixel[1] + pixel[2]) / 3 | |
bit = 1 if mean >= 128 else 0 | |
bits += str(bit) | |
value = value | (bit << (7 - i)) | |
i += 1 | |
if i >= 8: | |
text += chr(value) | |
value = 0 | |
i = 0 | |
bits += '\n' | |
if verbose: | |
print('Input image: {}x{}'.format(width, height)) | |
print('Bits:') | |
print(bits) | |
print('Result string:') | |
print(text) | |
if __name__ == '__main__': | |
main(sys.argv[1:]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
import getopt | |
import sys | |
from math import ceil, sqrt | |
from PIL import Image | |
def main(argv): | |
if len(argv) != 2: | |
print('Usage: python monika_encode.py [INPUT_FILE] [OUTPUT_FILE]') | |
sys.exit(2) | |
with open(argv[0], 'r') as f: | |
input = f.read() | |
bits_count = len(input) * 8 | |
size = int(ceil(sqrt(bits_count))) | |
image = Image.new("RGB", (size, size), "black") | |
pixels = image.load() | |
x = 0 | |
y = 0 | |
white = (255, 255, 255) | |
for ch in input: | |
value = ord(ch) | |
for i in range(8): | |
bit = (value >> (7-i)) & 1 | |
if bit == 1: | |
pixels[x, y] = white | |
x += 1 | |
if x >= size: | |
x = 0 | |
y += 1 | |
image.save(argv[1], 'png') | |
if __name__ == '__main__': | |
main(sys.argv[1:]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment