gpt4 book ai didi

python - 将原始图像与可能不需要原始图像的编辑图像进行比较的另一种方法

转载 作者:行者123 更新时间:2023-11-30 23:08:29 26 4
gpt4 key购买 nike

不久前,我做了一个 python 脚本来将数据存储在图像中,但是它有一个小问题,我只是想知道是否有人能够想到替代方法。

一个非常基本的想法是它会腌制一些东西,然后在第一个版本中,它直接将 ASCII 数字写为像素(因为所有内容都在 0 到 255 之间)。这会导致图像看起来有点像电视噪音。

当写入实际图像时,它会检测需要调整的每个像素的最小位数,这样人眼就不会注意到它,并且它会分割数据并添加或减去一些位每个像素,第一个像素存储它正在使用的方法。然后,我将 URL 作为文件存储在图像中,并可以通过使用第一个像素中给出的规则将 URL 中的原始图像与当前图像进行比较来反转它。

一些 python 伪代码,以防我没有解释清楚:

original_image = (200, 200, 200, 100, 210, 255...)
stuff_to_store = "test"
#Convert anything into a list of bytes
data_numbers = [bin(ord(x)) for x in cPickle.dumps(stuff_to_store)]

#This is calculated by the code, but for now it's 2
bytes_per_pixel = 2
store_mode = 'subtract'

#Join the bytes and split them every 2nd character
new_bytes = "".join(data_bytes)
new_bytes_split = [new_bytes[i:i+bytes_per_pixel] for i in range(0, len(new_bytes), bytes_per_pixel)]

#Edit the pixels (by subtraction in this case)
pixel_data = []
for i in range(len(original_image)):
pixel_data = original_image[i] - int(new_bytes_split[i])

但是,由于脚本的重点是通过修改像素来存储内容,因此将原始图像 URL 存储为文件感觉有点作弊。我想将 URL 存储为前几个像素,但只要图像不是灰色的,它最终就会出现一条明显的线条。此外,这种方法的效率非常低,因为它需要两个图像才能正常工作,因此如果有人知道如何避免这样做,那就太好了。

原代码为 here如果有人感兴趣,我在学习编写文档之前就这样做了,所以有点难以弄清楚,现在就问这个,因为我计划重写它并希望做得更好。

最佳答案

这是一种将数据嵌入到每 channel 8 位 RGB 图像文件中像素的每个颜色 channel 的最低有效位的方法,使用 PIL 进行图像处理。

下面的代码说明了 Python 中的位流处理。它相当高效(就此类操作在 Python 中的效率而言),但在必要时为了可读性和使用简单性而牺牲了效率。 :)

#! /usr/bin/env python

''' Steganography with PIL (really Pillow)

Encodes / decodes bits of a binary data file into the LSB of each color
value of each pixel of a non-palette-mapped image.

Written by PM 2Ring 2015.02.03
'''

import sys
import getopt
import struct
from PIL import Image


def readbits(bytes):
''' Generate single bits from bytearray '''
r = range(7, -1, -1)
for n in bytes:
for m in r:
yield (n>>m) & 1

def encode(image_bytes, mode, size, dname, oname):
print 'Encoding...'
with open(dname, 'rb') as dfile:
payload = bytearray(dfile.read())

#Prepend encoded data length to payload
datalen = len(payload)
print 'Data length:', datalen

#datalen = bytearray.fromhex(u'%06x' % datalen)
datalen = bytearray(struct.pack('>L', datalen)[1:])
payload = datalen + payload

databits = readbits(payload)
for i, b in enumerate(databits):
image_bytes[i] = (image_bytes[i] & 0xfe) | b

img = Image.frombytes(mode, size, str(image_bytes))
img.save(oname)


def bin8(i):
return bin(i)[2:].zfill(8)

bit_dict = dict((tuple(int(c) for c in bin8(i)), i) for i in xrange(256))

def decode_bytes(data):
return [bit_dict[t] for t in zip(*[iter(c&1 for c in data)] * 8)]

def decode(image_bytes, dname):
print 'Decoding...'
t = decode_bytes(image_bytes[:24])
datalen = (t[0] << 16) | (t[1] << 8) | t[2]
print 'Data length:', datalen

t = decode_bytes(image_bytes[24:24 + 8*datalen])

with open(dname, 'wb') as dfile:
dfile.write(str(bytearray(t)))


def process(iname, dname, oname):
with Image.open(iname) as img:
mode = img.mode
if mode == 'P':
raise ValueError, '%s is a palette-mapped image' % fname
size = img.size
image_bytes = bytearray(img.tobytes())
#del img

print 'Data capacity:', len(image_bytes) // 8 - 24

if oname:
encode(image_bytes, mode, size, dname, oname)
elif dname:
decode(image_bytes, dname)


def main():
#input image filename
iname = None
#data filename
dname = None
#output image filename
oname = None

def usage(msg=None):
s = msg + '\n\n' if msg else ''
s += '''Embed data into or extract data from the low-order bits of an image file.

Usage:

%s [-h] -i input_image [-d data_file] [-o output_image]

To encode, you must specify all 3 file names.
To decode, just specify the input image and the data file names.
If only the the input image is given, its capacity will be printed,
i.e., the maximum size (in bytes) of data that it can hold.

Uses PIL (Pillow) to read and write the image data.
Do NOT use lossy image formats for output, eg JPEG, or the data WILL get scrambled.
The program will abort if the input image is palette-mapped, as such images
are not suitable.
'''
print >>sys.stderr, s % sys.argv[0]
raise SystemExit, msg!=None

try:
opts, args = getopt.getopt(sys.argv[1:], "hi:d:o:")
except getopt.GetoptError, e:
usage(e.msg)

for o, a in opts:
if o == '-h': usage(None)
elif o == '-i': iname = a
elif o == '-d': dname = a
elif o == '-o': oname = a

if iname:
print 'Input image:', iname
else:
usage('No input image specified!')

if dname:
print 'Data file:', dname

if oname:
print 'Output image:', oname

process(iname, dname, oname)


if __name__ == '__main__':
main()

关于python - 将原始图像与可能不需要原始图像的编辑图像进行比较的另一种方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31698185/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com