Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
monika.chr code decoder and encoder | Doki Doki Literature Club
#!/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:])
#!/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