mirror of
https://github.com/cedricbonhomme/Stegano.git
synced 2025-05-12 17:18:30 +02:00
Refacto lsbset.hide to use less memory
This commit is contained in:
parent
cf7209f94c
commit
257d2c2f68
1 changed files with 10 additions and 10 deletions
|
@ -25,6 +25,7 @@ __date__ = "$Date: 2016/03/13 $"
|
||||||
__revision__ = "$Date: 2019/05/31 $"
|
__revision__ = "$Date: 2019/05/31 $"
|
||||||
__license__ = "GPLv3"
|
__license__ = "GPLv3"
|
||||||
|
|
||||||
|
import sys
|
||||||
from typing import IO, Iterator, Union
|
from typing import IO, Iterator, Union
|
||||||
|
|
||||||
from PIL import Image
|
from PIL import Image
|
||||||
|
@ -56,7 +57,7 @@ def hide(
|
||||||
raise Exception("Not a RGB image.")
|
raise Exception("Not a RGB image.")
|
||||||
img = img.convert("RGB")
|
img = img.convert("RGB")
|
||||||
|
|
||||||
img_list = list(img.getdata())
|
encoded = img.copy()
|
||||||
width, height = img.size
|
width, height = img.size
|
||||||
index = 0
|
index = 0
|
||||||
|
|
||||||
|
@ -76,7 +77,12 @@ def hide(
|
||||||
|
|
||||||
while index + 3 <= len_message_bits:
|
while index + 3 <= len_message_bits:
|
||||||
generated_number = next(generator)
|
generated_number = next(generator)
|
||||||
r, g, b, *a = img_list[generated_number]
|
|
||||||
|
col = generated_number % width
|
||||||
|
row = int(generated_number / width)
|
||||||
|
coordinate = (col, row)
|
||||||
|
|
||||||
|
r, g, b, *a = encoded.getpixel(coordinate)
|
||||||
|
|
||||||
# Change the Least Significant Bit of each colour component.
|
# Change the Least Significant Bit of each colour component.
|
||||||
r = tools.setlsb(r, message_bits[index])
|
r = tools.setlsb(r, message_bits[index])
|
||||||
|
@ -85,18 +91,12 @@ def hide(
|
||||||
|
|
||||||
# Save the new pixel
|
# Save the new pixel
|
||||||
if img.mode == "RGBA":
|
if img.mode == "RGBA":
|
||||||
img_list[generated_number] = (r, g, b, *a)
|
encoded.putpixel(coordinate, (r, g, b, *a))
|
||||||
else:
|
else:
|
||||||
img_list[generated_number] = (r, g, b)
|
encoded.putpixel(coordinate, (r, g, b))
|
||||||
|
|
||||||
index += 3
|
index += 3
|
||||||
|
|
||||||
# create empty new image of appropriate format
|
|
||||||
encoded = Image.new(img.mode, (img.size))
|
|
||||||
|
|
||||||
# insert saved data into the image
|
|
||||||
encoded.putdata(img_list)
|
|
||||||
|
|
||||||
return encoded
|
return encoded
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue