View difference between Paste ID: cyUzDmsF and eSZn13Bw
SHOW: | | - or go back to the newest paste.
1
#reddit.com/r/dailyprogrammer
2
#intermediate-101
3
4
import sys, Image
5
6
#CONSTANTS
7
TERMINATION = '11111111'
8
9
def steg(image_fn,data_fn):
10
    #read in our image file and declare variables
11
    img = Image.open(image_fn)
12
    pixels = img.load()
13
    im_width = img.size[0]
14
    im_height = img.size[1]
15
16
    #read in our data file and declare variables
17
    with open(data_fn, 'r') as f:
18
        data = f.read()
19
    b_data = ""
20
    for char in data:
21
        b_data += '%08d'%int(bin(ord(char))[2:])
22
        
23
    b_data += TERMINATION #used to find end when getting data out
24
    
25
    #verify image file is large enough
26
    pix_needed = len(b_data) / 2 
27
    if im_width * im_height < pix_needed:
28
        print 'Image file too small for data'
29
        sys.exit()
30
31
    #add data to image file
32
    for x in range(im_width):
33
        for y in range(im_height):
34
            if len(b_data) == 0:
35
                break
36
                
37
            pix = pixels[x,y]
38
            
39
            blueBase = bin(pix[2])[2:] #current pixels blue binary value
40
            newBlue = blueBase[:-2] + b_data[0:2] #create new binary value
41
            
42
            b_data = b_data[2:] #remove used bits from b_data
43
            
44
            pixels[x,y] = (pix[0],pix[1],int(newBlue,2)) #put new pixel into image file
45
        else:
46
            continue
47
        break
48
49
    #save new image as png
50
    new_file = 'steg_' + image_fn[:image_fn.rfind('.')] + '.png'
51
    img.save(new_file)
52
    return 'image saved as ' + new_file
53
        
54
def desteg(image_fn):
55
    #read in our image file and declare variables
56
    img = Image.open(image_fn)
57
    pixels = img.load()
58
    im_width = img.size[0]
59
    im_height = img.size[1]
60
61
    #get bianry data
62
    b_data = ""
63
    for x in range(im_width):
64
        for y in range(im_height):
65
            pix = pixels[x,y]
66
            b_data += bin(pix[2])[-2:]
67
            
68
    #get actual data
69
    s_data = ""
70
    for x in range(0,len(b_data),8):
71
        chunk = b_data[x:x+8]
72-
        if chunk == '11111111':
72+
        if chunk == TERMINATION:
73
            break #end of steganography data
74
        
75
        char = chr(int(chunk,2))
76
        s_data += char
77
78
    return s_data
79
    
80
81
#Process args
82
if len(sys.argv) == 1:
83
    print 'Usage: steganography.py {steg|desteg} {image file} {data file(steg only)}'
84
    sys.exit()
85
    
86
if sys.argv[1] == 'steg' and len(sys.argv) == 4:
87
    i_file = sys.argv[2]
88
    d_file = sys.argv[3]
89
    print steg(i_file,d_file)
90
elif sys.argv[1] == 'desteg' and len(sys.argv) == 3:
91
    i_file = sys.argv[2]
92
    print desteg(i_file)
93
else:
94
    print 'Usage: steganography.py {steg|desteg} {image file} {data file(steg only)}'
95
    sys.exit()