Change language

Image-based steganography using Python

| |

The idea behind image-based steganography is very simple. Images are composed of digital data (pixels) that describe what is inside the image, usually the colors of all pixels. Since we know that each image is composed of pixels and each pixel contains 3 values ​​(red, green, blue).

Encode the data:

Each byte of data is converted to its 8- bit binary using ASCII values. Pixels are now read from left to right in a group of 3 containing a total of 9 values. The first 8 values ​​are used to store binary data. The value becomes odd if a 1 is encountered and even if a 0 is encountered.

For example:
Suppose the message to be hidden is “` Hii ` ". Since the message is 3 bytes, the number of pixels required to encode the data is 3 x 3 = 9. Consider a 4 x 3 image with a total of 12 pixels, which is enough to encode the data.

` [( 27, 64, 164), (248, 244, 194), (174, 246, 250), (149, 95, 232), (188, 156, 169), (71, 167, 127), (132, 173, 97), (113, 69, 206), (255, 29, 213), (53, 153, 220), (246, 225, 229), (142, 82, 175)] `

ASCII value & # 39; H & # 39; is equal to 72, the binary equivalent of which is ` 01001000 `.

I take the first 3 pixels (27, 64, 164), (248, 244, 194), (174, 246, 250) for coding. Now change the pixel to odd for 1 and even for 0. So the changed pixels are: (26, 63, 164), (248, 243, 194), (174, 246, 250). Since we have to encode more data, the last value must be even. Likewise, "`i `" can be encoded in this image.

The new image will look like this:

` [( 26, 63, 164), (248, 243, 194), (174, 246, 250), (148, 95, 231), (188, 155, 168), (70, 167, 126), (132, 173, 97), (112, 69, 206), (254, 29, 213), (53, 153, 220), (246, 225, 229), (142, 82, 175)] `

Decode the data:

For decoding, three pixels are read at a time until the last value is odd, which means the message is over. Every 3 pixels contains binary data that can be extracted using the same encoding logic. If the if value is odd, the binary bit is 1, otherwise 0.

Below is implementation of the above idea:

 ` # Python program that implements image steganography `   ` # The PIL module is used to extract ` ` # pixels of the image and modify it ` ` from ` ` PIL ` ` import ` ` Image `   ` # Convert encoding data to 8-bit binary ` ` # form using ASCII character value ` ` def ` ` genData (data): `   ` ` ` # list of binaries ` ` Quantity provided data ` ` newd ` ` = ` ` [] `   ` for ` ` i ` ` in ` ` data: ` ` ` ` newd.append (` ` format ` ` (` ` ord ` ` (i), ` ` ’08b’ ` `)) ` ` ` ` retur n ` ` newd `   ` # Pixels are modified according to ` ` # 8-bit binary data and finally returned ` ` def ` ` modPix (pix, data): ` ` `  ` datalist ` ` = ` ` genData (data) ` ` lendata ` ` = ` ` len ` ` (datalist) ` ` imdata ` ` = ` ` iter ` ` ( pix) `   ` for ` ` i ` ` in ` ` range ` ` (lendata): `   ` # Extract 3 pixels at a time ` ` ` ` pix ` ` = ` ` [value ` ` for ` ` value ` ` in ` ` imdata .__ next __ () [: ` ` 3 ` `] ` ` + ` ` ` ` imdata .__ next __ () [: ` ` 3 ` `] ` ` + ` ` imdata .__ next __ () [: ` ` 3 ` `]] ` ` `  ` ` ` # Pixel value must be done ` ` # odd for 1 and even for 0 ` ` for ` ` j ` ` in ` ` range ` ` (` ` 0 ` `, ` ` 8 ` `): ` ` if ` ` (datalist [i] [j] ` ` = ` ` = ` ` ’0’ ` `) ` ` and ` ` (pix [j] ` `% ` ` 2 ` `! ` = ` 0 ` `): `   ` if ` ` (pix [j] ` `% ` ` 2 ` `! ` ` = ` ` 0 ` `): ` ` ` ` pix [j] ` ` - ` ` = ` ` 1 `   ` elif ` ` (datalist [i] [j] ` ` = ` ` = ` ` ’1’ ` `) ` ` and ` ` (pix [j] ` `% ` ` 2 ` ` = ` ` = ` ` 0 ` `): ` ` ` ` pix [j] ` ` - ` ` = ` ` 1 ` ` `  ` # Eight ^ th pixel of each set says ` ` # whether to stop reading further. ` ` # 0 means keep reading; 1 means ` ` # message ended. ` ` if ` ` (i ` ` = ` ` = ` ` lendata ` ` - ` ` 1 ` `): ` ` if ` ` (pix [` ` - ` ` 1 ` ` ] ` `% ` ` 2 ` ` = ` ` = ` ` 0 ` `): ` ` ` ` pix [` ` - ` ` 1 ` `] ` ` - ` ` = ` ` 1 ` ` else ` `: ` ` if ` ` (pix [` ` - ` ` 1 ` `] ` `% ` ` 2 ` `! ` ` = ` ` 0 ` `): ` ` pix [` ` - ` ` 1 ` `] ` ` - ` ` = ` ` 1 `   ` pix ` ` = ` ` tuple ` ` (pix) ` ` yield ` ` pix [` ` 0 ` `: ` ` 3 ` `] ` ` yield ` ` pix [` ` 3 ` `: ` ` 6 ` `] ` ` yield ` ` pix [` ` 6 ` `: ` ` 9 ` `] `   ` def ` ` encode_enc (newimg, data): ` ` w ` ` = ` ` newimg.size [` ` 0 ` `] ` ` ` ` (x, y) ` ` = ` ` (` ` 0 ` `, ` ` 0 ` `) `   ` for ` ` pixel ` ` in ` ` modPix (newimg.getdata (), data): `   ` # Place modified pixels in a new image ` ` newimg.putpixel ((x, y), pixel) ` ` if ` ` (x ` ` = ` ` = ` ` w ` ` - ` ` 1 ` `): ` ` ` ` x ` ` = ` ` 0 ` ` y ` ` + ` ` = ` ` 1 ` ` else ` `: ` ` x ` ` + ` ` = ` ` 1 `   ` # Encode data into image ` ` def ` ` encode (): ` ` img ` ` = ` ` input ` ` (` ` "Enter image name (with extension):" ` `) ` ` image ` ` = ` ` Image. ` ` open ` ` (img, ` ` ’r’ ` `) `   ` data ` ` = ` ` input ` ` (` ` "Enter data to be encoded:" ` `) ` ` ` ` if ` ` (` ` len ` ` (data) ` ` = ` ` = ` ` 0 ` `): ` ` ` ` raise ` ` ValueError (` `’ Data is empty’ ` `) `   ` ` ` newimg ` ` = ` ` image.copy () ` ` encode_enc (newimg, data) `   ` ` ` new_img_name ` ` = ` ` input ` ` (` ` "Enter the name of new image (with extension):" ` `) ` ` newimg.save (new_img_name, ` ` str ` ` (new_img_name.split (` ` " . "` `) [` ` 1 ` `]. upper ())) `   ` # Decode image data ` ` def ` ` decode (): ` ` img ` ` = ` ` input ` ` (` ` "Enter image name (with extension):" ` `) ` ` image ` ` = ` ` Image. ` ` open ` ` (img, ` ` ’r’ ` `) `   ` data ` ` = ` ` ’’ ` ` imgdata ` ` = ` ` iter ` ` (image.getdata ()) `   ` while ` ` (` ` True ` `): ` ` pixels ` ` = ` ` [value ` ` for ` ` value ` ` in ` ` imgdata .__ next __ () [: ` ` 3 ` `] ` ` + ` ` imgdata .__ next __ () [: ` ` 3 ` ` ] ` ` + `   ` imgdata .__ next __ () [: ` ` 3 ` `]] ` ` ` ` # binary data string ` ` binstr ` ` = ` ` ’’ `   ` for ` ` i ` ` in ` ` pixels [: ` ` 8 ` `]: ` ` if ` ` (i ` `% ` ` 2 ` ` = ` ` = ` ` 0 ` ` ): ` ` binstr ` ` + ` ` = ` ` ’0’ ` ` ` ` else ` `: ` ` binstr ` ` + ` ` = ` ` ’1’ `   ` ` ` data ` ` + ` ` = ` ` chr ` ` (` ` int ` ` (binstr, ` ` 2 ` `)) ` ` if ` ` (pixels [` ` - ` ` 1 ` `] ` `% ` ` 2 ` `! ` ` = ` ` 0 ` `): ` ` return ` ` data `   ` # Main function ` ` def ` ` main (): ` ` ` ` a ` ` = ` ` int ` ` (` ` input ` ` (` ` ":: Welcome to Steganography: : "` ` ` `" 1. Encode 2. Decode "` `)) ` ` ` ` if ` ` (a ` ` = ` ` = ` ` 1 ` `): ` ` encode () `   ` elif ` ` (a ` ` = ` ` = ` ` 2 ` `): ` ` print ` ` (` ` "Decoded word-" ` ` + ` ` decode ()) ` ` else ` `: ` ` ` ` raise ` ` Exception (` `" Enter correct input "` `) `   ` Driver code ` ` if ` ` __ name__ ` ` = ` ` = ` ` ’__main__’ ` `: `   ` # Calling the main function ` ` main () `

Output:

Shop

Learn programming in R: courses

\$

Best Python online courses for 2022

\$

Best laptop for Fortnite

\$

Best laptop for Excel

\$

Best laptop for Solidworks

\$

Best laptop for Roblox

\$

Best computer for crypto mining

\$

Best laptop for Sims 4

\$

Latest questions

NUMPYNUMPY

Common xlabel/ylabel for matplotlib subplots

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

12 answers

NUMPYNUMPY

Flake8: Ignore specific warning for entire file

12 answers

NUMPYNUMPY

glob exclude pattern

12 answers

NUMPYNUMPY

How to avoid HTTP error 429 (Too Many Requests) python

12 answers

NUMPYNUMPY

Python CSV error: line contains NULL byte

12 answers

NUMPYNUMPY

csv.Error: iterator should return strings, not bytes

12 answers

Wiki

Python | How to copy data from one Excel sheet to another

Common xlabel/ylabel for matplotlib subplots

Check if one list is a subset of another in Python

How to specify multiple return types using type-hints

Printing words vertically in Python

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries

Python add suffix / add prefix to strings in a list

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

Python - Move item to the end of the list

Python - Print list vertically