repl.it
@paulfears/

stegongraphy web app

Django

No description

fork
loading
Files
  • main
    • views.py
    • __pycache__
    • models.py
    • settings.py
    • urls.py
  • static
  • templates
  • db.sqlite3
main/views.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
from django.shortcuts import render
from django.http import HttpResponse
from django.core.files.base import ContentFile
#import PIL
#from PIL import Image as img
import urllib.request as url
import numpy
import io

# Create your views here.
def home(request):
    return render(request, 'main/index.html', {"href":"images/Figure_1.png"})

def decode(request):

  def decode_text(byte_array):
    a = '[email protected]#$%^&*()_+<>,.? '
    output = ""
    print(byte_array)
    for part in byte_array:
      try:
        output+=a[int(part, 2)-10]
      except IndexError:
        break
    return output[:-2]
    

  def decode_text_from_image(img_path):
    image = img.open(img_path)
    pixels = image.load()
    zeros = 0
    pos = [0,0]
    bits = ''
    kill = False
    for i in range((image.size[0]-1)*(image.size[1]-1)):
      if kill:
        break
      if(i%image.size[0]==0 and i>0):
        pos[0] = 0
        pos[1] +=1
      
      bins = list(pixels[pos[0],pos[1]])
      bins = list(map(lambda x: bin(x)[2:], bins))
      print(bins)
      for j in range(3):
        if(len(bits)%9==0):
          bits += " "
        bits += bins[j][-1]
        if bins[j][-1] == '0':
          zeros +=1
          if zeros == 15:
            kill = True
        else:
          zeros = 0
      
      
      pos[0] +=1
    bits = bits.split()
    return decode_text(bits)
  print(request.FILES)
  return(HttpResponse(decode_text_from_image(ContentFile(request.FILES['image'].read()))))
def encode(request):

  def encode_text(text):
    text +=" "
    a = '[email protected]#$%^&*()_+<>,.? '
    output = []
    for letter in text:
      b=bin(a.find(letter)+10)[2:]
      output.append(('0'*(8-len(b)))+b)
    print("output -- ",output)
    return output
      
  def get_image(uri):
    image = img.open(io.BytesIO(url.urlopen(uri).read()))
    return image
    
  def hide_text_in_image(img_url, text):
    text = encode_text(text)
    image = get_image(img_url)
    pixels = image.load()
    pos = [0,0]
    bits = ''.join(text)
    zeros = 17
    for i in range((image.size[0]-1)*(image.size[1]-1)):
      if(i%image.size[0] == 0 and i>0):
        pos[0] = 0
        pos[1] +=1
        
      if(len(bits)>1):
        new = []
        val = pixels[pos[0],pos[1]]
        for j in range(3):
          bval = list(bin(val[j])[2:])
          try:
            bval[-1] = bits[0]
          except IndexError:
            while len(new) < 3:
              bval[-1] = '0'
              bval = ''.join(bval)
              new.append(int(bval,2))
            break
            
          bits = bits[1:]
          bval = ''.join(bval)
          new.append(int(bval, 2))
        print(new)
        pixels[pos[0],pos[1]] = tuple(new)
      else:
        new = []
        val = pixels[pos[0],pos[1]]
        for j in range(3):
          bval = list(bin(val[j])[2:])
          bval[-1] = '0'
          bval = ''.join(bval)
          new.append(int(bval, 2))
        pixels[pos[0],pos[1]] = tuple(new)
        zeros -= 1
        if zeros == 0:
          print('break')
          break
      pos[0] +=1
      
    return image
    
    
  if(request.GET.get("imgurl") and request.GET.get("text")):
    uri = request.GET.get("imgurl")
    text = request.GET.get("text")
    image = hide_text_in_image(uri, text)
    response = HttpResponse(content_type="image/png")
    image.save(response,"PNG")
    return response