# Defining a Function to Modify the Image Data of an Image

So say I have certain data that I want to add to Image Data of an Image

How can I write a function in Mathematica that can do that?

Edit: My Purpose is to input data in an Image Data so that I can retrieve it later It doesn't matter how it's added there are no conditions I just need to be able to add it and retrieve it later

• @yode the data could be anything aka any string as for the image I provided it. thank you. – Omar Qasem Dec 12 '16 at 20:53
• But I don't know where you want to "input" and how you will retrieve. – yode Dec 12 '16 at 20:56
• I wanna input my string in the "Output" of the Built-In Function ImageData when I apply it to my image – Omar Qasem Dec 12 '16 at 20:57
• Related – Simon Woods Dec 12 '16 at 22:18

This method based on Steganography.Hope to help.

I suppose you have this string:

string = ExampleData[{"Text", "USConstitution"}]


PREAMBLE We the People of the United States, in Order to form a more perfect Union, establish Justice, insure domestic Tranquility, provide for the common defence, promote the general Welfare, and secure the Blessings of Liberty to ourselves and our Posterity, do ordain and establi....

And you have such image

img=ExampleData[{"TestImage", "Lena"}]


AddToImage[image_, information_] :=
Module[{imageData, dim, residueBit, imageCapacity, informationSize,
bitNumber, carrierImageData, informationBin},
imageData = ImageData[image, "Byte"]; dim = Dimensions[imageData];
residueBit = Mod[Times @@ dim, 16];
imageCapacity = Times @@ dim - residueBit;
informationSize = Length[ToCharacterCode[information]]*16 + 2;
bitNumber = Quotient[informationSize, imageCapacity];
carrierImageData =
Fold[BitClear[#1, #2] &, Flatten[imageData], Range[0, bitNumber]];
informationBin =
Join[IntegerDigits[bitNumber, 2, 2],
IntegerDigits[ToCharacterCode[information], 2]]];
If[bitNumber > 2,
"Your amount of information is too big for encryption.You should \
get a larger image or decrease your information size.",
Image[ArrayReshape[
FromDigits[#, 2] & /@
Flatten[Reverse[
Partition[informationBin, imageCapacity, imageCapacity,
1, {}]], {2}], imageCapacity + residueBit],
carrierImageData], dim], "Byte"]]]



Well,it's seem nothing to happen.Actually I "input" a USConstitution in it.Of course,the image just can take a limit size information.And if you want to export this image to share to other people,you must use .png,.tiff or other uncompressed format.Otherwise you cannot retrieve your information any more.

## Extract that data

ExtractInformation[image_] :=
Module[{imageData = ImageData[image, "Byte"], residueBit,
informationCode, informationCodeLen, outBitNum},
outBitNum = FromDigits[BitGet[Flatten[imageData][[;; 2]], 0], 2];
residueBit = Mod[Times @@ Dimensions[imageData], 16];
informationCode =
Partition[
Flatten[BitGet[
Flatten[imageData][[;;
Times @@ Dimensions[imageData] - residueBit]], #] & /@
Range[0, outBitNum]][[3 ;;]], 16];
informationCodeLen =
SparseArray[informationCode]["NonzeroPositions"][[-1, 1]];
FromCharacterCode[
FromDigits[#, 2] & /@ informationCode[[;; informationCodeLen]]]]

ExtractInformation[informationImage]


PREAMBLE We the People of the United States, in Order to form a more perfect Union, establish Justice, insure domestic Tranquility, provide for the common defence, promote the general Welfare, and secure the Blessings of Liberty to ourselves and our Posterity, do ordain and establish this Co...