3
$\begingroup$

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

$\endgroup$
11
  • 2
    $\begingroup$ How to "add"?You should give more information for your question.Provide your data and your image,please. $\endgroup$ – yode Dec 12 '16 at 20:44
  • $\begingroup$ @yode the data could be anything aka any string as for the image I provided it. thank you. $\endgroup$ – Omar Qasem Dec 12 '16 at 20:53
  • $\begingroup$ But I don't know where you want to "input" and how you will retrieve. $\endgroup$ – yode Dec 12 '16 at 20:56
  • $\begingroup$ I wanna input my string in the "Output" of the Built-In Function ImageData when I apply it to my image $\endgroup$ – Omar Qasem Dec 12 '16 at 20:57
  • 1
    $\begingroup$ Related $\endgroup$ – Simon Woods Dec 12 '16 at 22:18
13
$\begingroup$

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

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.