Question: QUESTION : This is a Python program to embed pdf , txt and doc file in an image , the problem here is that i
QUESTION : This is a Python program to embed pdftxt and doc file in an image the problem here is that i cannot open the extracted file it shows error : failed to load pdf document Note that i am able to embed and extract the data, only the content of the extracted file cannot be read. Also please comment on the code whether it is practical to be used to embed and extract file from an image. I also run this program on windows cmd
Code:
from PIL import Image
import argparse
import os
class Steganography:
@staticmethod
def mergeimage filedata:
imagesize image.size image.size # Total number of RGB values
if lenfiledata imagesize:
raise ValueErrorThe file is too large to be hidden in the image."
pixels image.load
mergedimage Image.newRGB image.size
mergedpixels mergedimage.load
dataindex
datalength lenfiledata
for i in rangeimagesize:
for j in rangeimagesize:
r g b pixelsi j
if dataindex datalength:
r r & xFEfiledatadataindex & x
g g & xFEfiledatadataindex & x
b b & xFEfiledatadataindex & x
if dataindex datalength:
r r & xFDfiledatadataindex & x
g g & xFDfiledatadataindex & x
b b & xFDfiledatadataindex & x
if dataindex datalength:
r r & xFBfiledatadataindex & x
g g & xFBfiledatadataindex & x
if dataindex datalength:
b b & xFBfiledatadataindex & x
g g & xFfiledatadataindex & x
mergedpixelsi jr g b
dataindex
return mergedimage
@staticmethod
def unmergeimage:
pixels image.load
extracteddata bytearray
for i in rangeimagesize:
for j in rangeimagesize:
r g b pixelsi j
byte r & x
byte g & x
byte b & x
byte r & x
byte g & x
byte b & x
byte r & x
byte g & x
extracteddata.appendbyte
return bytesextracteddata
def filetobinaryfilepath:
with openfilepath, rb as file:
return file.read
def binarytofiledata outputpath:
with openoutputpath, wb as file:
file.writedata
def main:
parser argparse.ArgumentParserdescription"Steganography: Hide a file inside an image."
subparsers parser.addsubparsersdest'command'
mergeparser subparsers.addparsermerge help'Merge a file into an image'
mergeparser.addargumentcarrier', requiredTrue, help'Path to the carrier image'
mergeparser.addargumentfile', requiredTrue, help'Path to the file to hide'
mergeparser.addargumentoutput', requiredTrue, help'Path to the output image'
unmergeparser subparsers.addparserunmerge help'Extract a file from an image'
unmergeparser.addargumentimage', requiredTrue, help'Path to the merged image'
unmergeparser.addargumentoutput', requiredTrue, help'Path to the extracted file'
args parser.parseargs
if args.command 'merge':
image Image.openargscarrier
filedata filetobinaryargsfile
printfMerging argsfile into argscarrier
mergedimage Steganography.mergeimage filedata
printfSaving merged image to argsoutput
mergedimage.saveargsoutput
printMerge completed successfully."
elif args.command 'unmerge':
image Image.openargsimage
extracteddata Steganography.unmergeimage
printfExtracting file to argsoutput
binarytofileextracteddata, args.output
printUnmerge completed successfully."
if namemain:
main
Output:
in This PC OSC: Projects Steganography res carrierimage.jpg secret.pdf secret.txt secret.doc venvvirtual environment files textSteg.py
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
