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 pdf,txt 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 merge(image, file_data):
image_size = image.size[0]* image.size[1]*3 # Total number of RGB values
if len(file_data)*8> image_size:
raise ValueError("The file is too large to be hidden in the image.")
pixels = image.load()
merged_image = Image.new('RGB', image.size)
merged_pixels = merged_image.load()
data_index =0
data_length = len(file_data)
for i in range(image.size[0]):
for j in range(image.size[1]):
r, g, b = pixels[i, j]
if data_index < data_length:
r =(r & 0xFE)|((file_data[data_index]>>7) & 0x01)
g =(g & 0xFE)|((file_data[data_index]>>6) & 0x01)
b =(b & 0xFE)|((file_data[data_index]>>5) & 0x01)
if data_index +1< data_length:
r =(r & 0xFD)|((file_data[data_index +1]>>4) & 0x02)
g =(g & 0xFD)|((file_data[data_index +1]>>3) & 0x02)
b =(b & 0xFD)|((file_data[data_index +1]>>2) & 0x02)
if data_index +2< data_length:
r =(r & 0xFB)|((file_data[data_index +2]>>1) & 0x04)
g =(g & 0xFB)|((file_data[data_index +2]>>0) & 0x04)
if data_index +3< data_length:
b =(b & 0xFB)|((file_data[data_index +3]>>1) & 0x08)
g =(g & 0xF7)|((file_data[data_index +3]>>0) & 0x08)
merged_pixels[i, j]=(r, g, b)
data_index +=4
return merged_image
@staticmethod
def unmerge(image):
pixels = image.load()
extracted_data = bytearray()
for i in range(image.size[0]):
for j in range(image.size[1]):
r, g, b = pixels[i, j]
byte =(r & 0x01)<<7
byte |=(g & 0x01)<<6
byte |=(b & 0x01)<<5
byte |=(r & 0x02)<<4
byte |=(g & 0x02)<<3
byte |=(b & 0x02)<<2
byte |=(r & 0x04)<<1
byte |=(g & 0x04)<<0
extracted_data.append(byte)
return bytes(extracted_data)
def file_to_binary(file_path):
with open(file_path, 'rb') as file:
return file.read()
def binary_to_file(data, output_path):
with open(output_path, 'wb') as file:
file.write(data)
def main():
parser = argparse.ArgumentParser(description="Steganography: Hide a file inside an image.")
subparsers = parser.add_subparsers(dest='command')
merge_parser = subparsers.add_parser('merge', help='Merge a file into an image')
merge_parser.add_argument('--carrier', required=True, help='Path to the carrier image')
merge_parser.add_argument('--file', required=True, help='Path to the file to hide')
merge_parser.add_argument('--output', required=True, help='Path to the output image')
unmerge_parser = subparsers.add_parser('unmerge', help='Extract a file from an image')
unmerge_parser.add_argument('--image', required=True, help='Path to the merged image')
unmerge_parser.add_argument('--output', required=True, help='Path to the extracted file')
args = parser.parse_args()
if args.command == 'merge':
image = Image.open(args.carrier)
file_data = file_to_binary(args.file)
print(f"Merging {args.file} into {args.carrier}")
merged_image = Steganography.merge(image, file_data)
print(f"Saving merged image to {args.output}")
merged_image.save(args.output)
print("Merge completed successfully.")
elif args.command == 'unmerge':
image = Image.open(args.image)
extracted_data = Steganography.unmerge(image)
print(f"Extracting file to {args.output}")
binary_to_file(extracted_data, args.output)
print("Unmerge completed successfully.")
if __name__=="__main__":
main()
Output:
in This PC -> OS(C:)-> Projects Steganography/ res/ carrier_image.jpg secret.pdf secret.txt secret.doc venv/...(virtual environment files) text_Steg.py

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!