@qtip2001/

images

Python

No description

fork
loading
Files
  • main.py
  • Disco.bmp
  • DiscoModified.bmp
  • Lego.bmp
  • logo1.bmp
  • logo2.bmp
  • Mystery1.bmp
  • Mystery2.bmp
  • SC.bmp
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
from PIL import Image
from math import sqrt
import random

def fib(n):
    return ((1+sqrt(5))**n-(1-sqrt(5))**n)/(2**n*sqrt(5))

def shift(img, length, width, s):
  for l in range(10):
    for w in range(width):
      x = img.getpixel((l,w))
      img.putpixel((l,w), (x+s)%256)
  return img

def mult_2x1(matrix1, matrix2, mod):
  new_mat = [None, None]
  new_mat[0] = (matrix1[0]*matrix2[0]+matrix1[1]*matrix2[1]) % mod
  new_mat[1] = (matrix1[2]*matrix2[0]+matrix1[3]*matrix2[1]) % mod
  return new_mat

def special_encode(im,width,height, matrix, mod, vec):
  count = 0
  for h in range(height):
    print(h)
    for w in range(int(width/2)):
      random.seed(count)
      d = [(im.getpixel((w*2, h))+random.randint(0,255))%256, (im.getpixel((w*2+1, h))+random.randint(0,255))%256]
      k = mult_2x1(matrix, d, mod)
      if count%2 == 0:
        matrix = [matrix[0]*vec[0]%256, matrix[1]*vec[1]%256, matrix[2], matrix[3]]
      else:
        matrix = [matrix[0], matrix[1], matrix[2]*vec[0]%256, matrix[3]*vec[1]%256]

      im.putpixel((w*2, h), int(k[0]))
      im.putpixel((w*2+1, h), int(k[1]))
      count += 1
  return im

def mod_inverse(a, m):
  for x in range(m):
    if (x*a)%m == 1:
      return x
  return -1

def matrix_inverse(matrix, mod):
  det = matrix[0]*matrix[3] - matrix[1]*matrix[2]
  inv = mod_inverse(det, mod)
  if inv == -1:
    #return [-1, -1, -1, -1]
    #print("Error")
    return 0
  else:
    new_mat = [None, None, None, None]
    new_mat[0] = ( inv * matrix[3]) % mod
    new_mat[2] = (-inv * matrix[2]) % mod
    new_mat[1] = (-inv * matrix[1]) % mod
    new_mat[3] = ( inv * matrix[0]) % mod
    return new_mat

def special_decode(im, width, height, matrix, mod, vec):
  count = 0
  for h in range(height):
    print(h)
    for w in range(int(width/2)):
      inv = matrix_inverse(matrix, mod)
      d = [im.getpixel((w*2, h)), im.getpixel((w*2+1, h))]
      k = mult_2x1(inv, d, mod)
      if count%2 == 0:
        matrix = [matrix[0]*vec[0]%256, matrix[1]*vec[1]%256, matrix[2], matrix[3]]
      else:
        matrix = [matrix[0], matrix[1], matrix[2]*vec[0]%256, matrix[3]*vec[1]%256]
      random.seed(count)
      im.putpixel((w*2, h), (k[0]-random.randint(0,255))%256)
      im.putpixel((w*2+1,h),(k[1]-random.randint(0,255))%256)
      count += 1
      
  return im

im = Image.open("DiscoModified.bmp").convert("L")
im = special_decode(im, im.size[0], im.size[1],[2,5,3,20] ,256, [101, 141])
im.save("DiscoModified.bmp")