Hackvent_2018/Day 6/decode.py
Roman Hergenreder aa56104b3f Progress
2018-12-07 13:52:13 +01:00

229 lines
6.3 KiB
Python

#!/usr/bin/python
from PIL import Image
import math
import numpy
def to24Bit(data):
red = (data >> 5) * 32;
green = ((data & 28) >> 2) * 32;
blue = (data & 3) * 64;
return [red, green, blue]
def to8Bit(pixel):
# return (math.floor((pixel[0] / 32)) << 5) + (math.floor((pixel[1] / 32)) << 2) + math.floor((pixel[2] / 64))
return (pixel[0] * 7 // 255) << 5 + (pixel[1] * 7 // 255) << 2 + (pixel[2] * 3 // 255)
images = ["house", "trees", "lake", "sky", "sheep", "snake"]
tileSize = 10
im = Image.open("Piet'ers Gallery_files/house.png")
pix = im.load()
numTiles = im.size[0] // tileSize
for y in range(im.size[1] - 2 * tileSize):
line = ""
for x in range(im.size[0] - 2 * tileSize):
x_abs = x + tileSize
y_abs = y + tileSize
pixelX = min(math.floor(x_abs / tileSize) * tileSize + 5, im.size[0] - 5)
pixelY = min(math.floor(y_abs / tileSize) * tileSize + 5, im.size[1] - 5)
pixels = [
pix[5, pixelY],
pix[im.size[0] - 5, pixelY],
pix[pixelX, 5],
pix[pixelX, im.size[1] - 5]
]
result = ((pixels[0] | pixels[1]) & (pixels[2] | pixels[3]))
pix[x_abs, y_abs] = result
for tileY in range(numTiles):
line = ""
for tileX in range(numTiles):
p = pix[tileX * tileSize + 5, tileY * tileSize + 5]
# line += "%08d " % int(bin(p)[2:])
line += chr(p)
print(line)
im.save("test.png")
# def decodePixel(x, y, tileSize):
# # x_abs = x*tileSize + tileSize // 2
# # y_abs = y*tileSize + tileSize // 2
# pixel = pix[x, y]
#
# return pixel
# return "%02d" % pixel
# r = pixel[0]
# g = pixel[1]
# b = pixel[2]
# bit8 = to8Bit(pixel)
# return colors[pixel]
# r = 1 if r == 192 else (10 if r == 255 else r)
# g = 1 if g == 192 else (10 if g == 255 else g)
# b = 1 if b == 192 else (10 if b == 255 else b)
# return "%02d%02d%02d" % (r, g, b)
# return "%02d" % pixel
# return "%08d" % int(bin(bit8)[2:])
# What to do with this pixel?
# return bin(pixel)[2:]
# return "%04d" % int(bin(pixel)[2:])
# return str(chr(ord('A') + pixel))
# return pix[x,y]
# for imageName in images:
# im = Image.open("Piet'ers Gallery_files/%s.png" % imageName)
# pix = im.load()
#
# tileSize = 10
# tilesW = im.size[0] // tileSize
# tilesH = im.size[1] // tileSize
# #
# data = []
#
# # First row
# for x in range(tilesW):
# data.append(decodePixel(x, 0, tileSize))
#
# # Column right
# for y in range(1, tilesH - 1):
# data.append(decodePixel(tilesW - 1, y, tileSize))
#
# # Row bottom
# for x in reversed(range(tilesW - 1)):
# data.append(decodePixel(x, tilesH - 1, tileSize))
#
# # Column left
# for y in reversed(range(1, tilesH - 1)):
# data.append(decodePixel(0, y, tileSize))
#
# # print(''.join(data))
# print(imageName, "\t", ' '.join(data))
# for
# im = Image.open("Piet'ers Gallery_files/lake.png")
# im = Image.open("Piet'ers Gallery_files/house_2.png")
# im = Image.frombytes('P', im.size, im.tobytes())
# for imageName in images:
# im = Image.open("Piet'ers Gallery_files/%s.png" % imageName)
# pix = im.load()
#
# tileSize = 10
# tilesW = im.size[0] // tileSize
# tilesH = im.size[1] // tileSize
# for y in range(im.size[1] - 2 * tileSize):
# line = ""
# for x in range(im.size[0] - 2 * tileSize):
# x_abs = x + tileSize
# y_abs = y + tileSize
#
# pixelX = min(math.floor(x_abs / tileSize) * tileSize + 5, im.size[0] - 5)
# pixelY = min(math.floor(y_abs / tileSize) * tileSize + 5, im.size[1] - 5)
#
# pixels = [
# pix[5, pixelY],
# pix[im.size[0] - 5, pixelY],
# pix[pixelX, 5],
# pix[pixelX, im.size[1] - 5]
# ]
#
# result = (pixels[0] ^ pixels[1] ^ pixels[2] ^ pixels[3]) % 18
# line += str(result)
#
# print(pixels)
# pixelX = 5 if (x_abs < im.size[0] // 2) else im.size[0] - 5
# pixelY = 5 if (y_abs < im.size[1] // 2) else im.size[1] - 5
# print("pixelX, pixelY", pixelX, pixelY, x, y)
# print("asdas", x, y)
# pixel1 = decodePixel(pixelX, , tileSize)
# pixel2 = decodePixel(math.floor(x_abs / tileSize) * tileSize, pixelY, tileSize)
# pix[x_abs, y_abs] = result
# print(line)
#
# im.save("%s_decoded.png" % imageName)
# data = []
#
# # First row
# for x in range(tilesW):
# data.append(decodePixel(x, 0))
#
# # Column right
# for y in range(1, tilesH - 1):
# data.append(decodePixel(tilesW - 1, y))
#
# # Row bottom
# for x in reversed(range(tilesW - 1)):
# data.append(decodePixel(x, tilesH - 1))
#
# # Column left
# for y in reversed(range(1, tilesH - 1)):
# data.append(decodePixel(0, y))
# #
# print(''.join(data))
# print(len(''.join(data)))
# for x in range(tilesW):
# data.append(decodePixel(x, 0))
# pixel = numpy.array(im)
# print(bin(pix[4*tileSize + 5,0]))
# data = [[0 for x in range(tilesW)] for y in range(tilesH)]
# for y in range(tilesH):
# line = ""
# for x in range(tilesW):
# # pixel = im.getpixel((x * tileSize + 5, y * tileSize + 5))
# pixel = pix[x * tileSize + 5, y * tileSize + 5]
# # binary = bin(pixel)[2:]
# # binaryStr = "%08d" % int(binary)
# # line += binaryStr
# # line += chr(ord('A') + pixel)
# # data[x][y] = pixel
# # line += chr(pixel)
# # print(to24Bit(int(binaryStr[::-1], 2)))
# # print(binaryStr, binary, chr(pixel))
# # str = str + binaryStr
# # str += binary
# line += "%02d" % pixel
# print(line)
# data[1][1] = data[0][1] + data[1][0]
# for x in range(1,tilesW//2 + 1):
# data[x][1] = data[0][x] | data[1][0]
# print(chr(ord('A') + data[x][1]))
# for y in range(1,tilesH):
# pixel = pix[tilesW * tileSize - 5, y * tileSize + 5]
# binary = bin(pixel)[2:]
# binaryStr = "%08d" % int(binary)
# str = str + binaryStr
#
# print(str)
#
# data = [
# [255,192,255],
# [255,0,255],
# [0,255,255]
# ]
#
# print(' '.join(["%08d" % int(bin(to8Bit(p))[2:]) for p in data]))
# print(pix[5, 15], pix[5, 25], pix[5, 35], pix[5, 45], pix[5, 55])