Search code examples
pythonimagedigital-signaturesteganographypiexif

Embed a data inside JPG file without altering the image in python


i have 289 bytes of data that i want to embed to a jpg file. Of course without altering the image. Also, i can extract that data from the image and the picture is still original. To proof the originality, the hash value (SHA 256) of the image before and after embedding proses is still the same value.

i've tried using stegano library and piexif. this is the code that using piexif

exif_dict = piexif.load(pathImg + 'testImage.jpg')
exif_dict["thumbnail"] = signature # Signature is the 289 bytes of data
exif_bytes = piexif.dump(exif_dict)
piexif.insert(exif_bytes, pathImg + 'testImage.jpg')

and i got an error piexif._exceptions.InvalidImageDataError: Given data isn't JPEG.


Solution

  • I solved this by myself and use the code below to insert the value

    import piexif
    
    exif_dict = piexif.load('testImageSigned.jpg')
    exif_dict["0th"][piexif.ImageIFD.Copyright] = hashValue
    exif_bytes = piexif.dump(exif_dict)
    piexif.insert(exif_bytes, 'testImageSigned.jpg')
    

    And here is the code to extract:

    exif_dict = piexif.load('testImageSigned.jpg')
    hashValue2 = exif_dict["0th"][piexif.ImageIFD.Copyright]
    

    Here is the exif tag documentation and there describe the data type.

    And here is the piexif library documentation.

    Hope this can help you