3

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

here is the image

yode
  • 26,686
  • 4
  • 62
  • 167
Omar Qasem
  • 141
  • 4

1 Answers1

13

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"}]

enter image description here

"Add" your information to image

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], 
    Flatten[PadLeft[#, 16] & /@ 
      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[
     BitOr[PadRight[
       FromDigits[#, 2] & /@ 
        Flatten[Reverse[
          Partition[informationBin, imageCapacity, imageCapacity, 
           1, {}]], {2}], imageCapacity + residueBit], 
      carrierImageData], dim], "Byte"]]]

informationImage=AddToImage[img, string]

enter image description here

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...

yode
  • 26,686
  • 4
  • 62
  • 167