首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Python Pillow和JPEG文件的奇怪问题-像素元组元素不断减少。为什么?

Python Pillow和JPEG文件的奇怪问题-像素元组元素不断减少。为什么?
EN

Stack Overflow用户
提问于 2020-06-23 08:58:02
回答 1查看 57关注 0票数 0

我有一个(非常)的隐写术python程序的草稿,它在图片中对秘密信息进行编码。现在,我只是简单地根据消息的下一个二进制元素(消息转换为ascii十六进制,然后是二进制),将每个3000个像素的第三个元素调高或调低一个。

我的问题有些离题,但我包含了我的所有代码,所以如果有人真的想表现得很好,他们可以简单地复制和复制代码(也许必须通过pip安装pillow,获取图像,并为myascii模块创建一个单独的python脚本,但仅此而已)。

我的问题是:每次运行这段代码时,print(pxwidth-1,height-1)和print(pxwidth-2,height-2)在每个RGB值中都会递减1。我不明白为什么要救我的命。因此,一些终端输出如下所示:

代码语言:javascript
复制
PS C:\Users\xxx> & C:/Users/xxx/AppData/Local/Programs/Python/Python38-32/python.exe c:/Users/xxx/Desktop/Python_stuff/assorted/steganography/steganography.py
['4B', '65', '6C', '6C', '65', '79', '27', '73', '20', '66', '61', '76', '6F', '72', '69', '74', '65', '20', '63', '6F', '6C', '6F', '72', '20', '69', '73', '20', '67', '72', '65', '65', '6E', '2E']
010010110110010101101100011011000110010101111001001001110111001100100000011001100110000101110110011011110111001001101001011101000110010100100000011000110110111101101100011011110111001000100000011010010111001100100000011001110111001001100101011001010110111000101110
(75, 51, 17)
(74, 50, 16)
(73, 37, 13)
PS C:\Users\xxx> & C:/Users/xxx/AppData/Local/Programs/Python/Python38-32/python.exe c:/Users/xxx/Desktop/Python_stuff/assorted/steganography/steganography.py
['4B', '65', '6C', '6C', '65', '79', '27', '73', '20', '66', '61', '76', '6F', '72', '69', '74', '65', '20', '63', '6F', '6C', '6F', '72', '20', '69', '73', '20', '67', '72', '65', '65', '6E', '2E']
010010110110010101101100011011000110010101111001001001110111001100100000011001100110000101110110011011110111001001101001011101000110010100100000011000110110111101101100011011110111001000100000011010010111001100100000011001110111001001100101011001010110111000101110
(74, 50, 16)
(73, 49, 15)
(73, 37, 13)
PS C:\Users\xxx> & C:/Users/xxx/AppData/Local/Programs/Python/Python38-32/python.exe c:/Users/xxx/Desktop/Python_stuff/assorted/steganography/steganography.py
['4B', '65', '6C', '6C', '65', '79', '27', '73', '20', '66', '61', '76', '6F', '72', '69', '74', '65', '20', '63', '6F', '6C', '6F', '72', '20', '69', '73', '20', '67', '72', '65', '65', '6E', '2E']
010010110110010101101100011011000110010101111001001001110111001100100000011001100110000101110110011011110111001001101001011101000110010100100000011000110110111101101100011011110111001000100000011010010111001100100000011001110111001001100101011001010110111000101110
(73, 49, 15)
(72, 48, 14)
(73, 37, 13)

程序和模块如下:

代码语言:javascript
复制
#def encode(myimage):


#def decode(myimage):

import bin_hex_ascii_converter as myascii

myascii.hex_to_dec("34")
#letter_to_ascii_hex(letter):
#hex_to_bin(foo):
from PIL import Image
foo = Image.open("C:/Users/xxx/Pictures/Saved Pictures/funny_image_encoded.jpg")
#foo = Image.open("C:/Users/xxx/Pictures/Saved Pictures/funny_image.jpg")
#myImage.show()

secret_message = "Jimmie's favorite color is green."

msg = []
for let in secret_message:
   msg.append(myascii.letter_to_ascii_hex(let))
print(msg)

msg_bin = ""
for myhex in msg:
   msg_bin += myascii.hex_to_bin(myhex)
print(msg_bin)

px = foo.load()

width = foo.size[0]
height = foo.size[1]

print(px[width-1,height-1])
print(px[width-2,height-2])

secret_separation = 3000
bar = 0
myiter = 0
limit = len(msg_bin)
for i in range(0,height):
   for j in range(0,width):
       if bar % 3000 == 0 and myiter < limit:
           temp = int(msg_bin[myiter])
           if temp == 0:
               temp = -1
           px[j,i] = (px[j,i][0], px[j,i][1], px[j,i][2]+temp)
           myiter += 1
       bar += 1

#px[5,5] = (px[5,5][0], px[5,5][1], px[5,5][2]+1)
foo.save("C:/Users/xxx/Pictures/Saved Pictures/funny_image_encoded.jpg","JPEG")
print(px[5,5])

另外,我作为myascii导入的模块是:

代码语言:javascript
复制
#hex to dec conversion
def hex_to_dec(foo):
    letts = " ABCDEF"
    if foo[0] in letts:
        num1 = 9 + letts.index(foo[0])
    else:
        num1 = int(foo[0])
    if foo[1] in letts:
        num0 = 9 + letts.index(foo[1])
    else:
        num0 = int(foo[1])
    return num1*16 + num0

def ascii_to_letter(foo):
    low_lett = "abcdefghijklmnopqrstuvwxyz"
    upp_lett = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
    #ADD comma and period as well
    if foo == 44:
        return ","
    if foo == 46:
        return "."
    if foo == 32:
        return " "
    if foo == 63:
        return "?"
    if foo > 96 and foo < 123:
        return low_lett[foo - 97]
    if foo > 64 and foo < 91:
        return upp_lett[foo - 65]
    #otherwise
    return "*"

def letter_to_ascii_hex(letter):
    return hex(ord(letter))[2:].upper()

# Returning one byte (8 bits)
def hex_to_bin(foo):
    myarr = ["0000",
    "0001",
    "0010",
    "0011",
    "0100",
    "0101",
    "0110",
    "0111",
    "1000",
    "1001",
    "1010",
    "1011",
    "1100",
    "1101",
    "1110",
    "1111"]

    num2 = foo[0]
    if num2 == "A":
        num2 = "1010"
    elif num2 == "B":
        num2 = "1011"
    elif num2 == "C":
        num2 = "1100"
    elif num2 == "D":
        num2 = "1101"
    elif num2 == "E":
        num2 = "1110"
    elif num2 == "F":
        num2 = "1111"
    else:
        num2 = myarr[int(num2)]

    num1 = foo[1]
    if num1 == "A":
        num1 = "1010"
    elif num1 == "B":
        num1 = "1011"
    elif num1 == "C":
        num1 = "1100"
    elif num1 == "D":
        num1 = "1101"
    elif num1 == "E":
        num1 = "1110"
    elif num1 == "F":
        num1 = "1111"
    else:
        num1 = myarr[int(num1)]

    return num2 + num1

另外,我使用的图片是:https://www.bing.com/images/search?view=detailV2&ccid=FOFci7yV&id=AA468BC3FE90B8D67D34C7884C3578A69D815E52&thid=OIP.FOFci7yV8pJTXQxPI9OYpgHaHk&mediaurl=https%3A%2F%2Fwww.laughtard.com%2Fwp-content%2Fuploads%2F2019%2F02%2F50-Funny-Animal-Pictures-That-You-Need-In-Your-Life-37.jpg&exph=1000&expw=979&q=funny+images&simid=608028465397174105&ck=A56AB62FC7B4F29A99A099EF131B8CDB&selectedindex=0&ajaxhist=0&first=1&scenario=ImageBasicHover&vt=0&sim=11

EN

回答 1

Stack Overflow用户

发布于 2020-06-23 09:48:54

正如马克·兰瑟姆指出的那样--jpeg是有损的文件格式。改用png解决了这个问题。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62525711

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档