【问题标题】:PIL image.open() not working for my .pngPIL image.open() 不适用于我的 .png
【发布时间】:2013-09-26 14:00:06
【问题描述】:

我使用 PIL 打开图像以提取几个位并将它们写入字符串。应该是,此代码将过滤除具有 (R

from PIL import Image
def extract_bits(color, bitmask):
    bitmask_len = len(bin(bitmask)[2:])
    extracted_bits = bin(color & bitmask)[2:]
    extracted_bits = '0' * (bitmask_len - len(extracted_bits)) + extracted_bits

    return extracted_bits

if __name__ == '__main__':
    img = Image.open('IMG_0707png')
    pixels = list(img.getdata())

    bits = ''
    for i in range(0, len(pixels), 1):
        r = pixels[i][0]
        g = pixels[i][1]
        b = pixels[i][2]

        if not (r <= 1 and g <= 1 and b <= 1): continue

        bits += extract_bits(r, 0x1)
        bits += extract_bits(g, 0x1)
        bits += extract_bits(b, 0x1)

    bits += '0' * (8 - len(bits) % 8)

    text = ''
    for i in range(0, len(bits), 8):
        text += chr(int(bits[i:i+8], 2))
    print text

我查看了这个问题,发现在我的情况下不起作用的解决方案。 img = Image.open(open('IMG_0707.png', 'rb')) 在这两种情况下,我都会得到 ​​p>

File "<stdin>, line 1, in <module>"
File "<string>" line11, im <module>
File "c:\python27\lib\site-packages\PIL\Image.py", line 1980, in open
raise IOError("cannot identify image file")
IOError: cannot identify image file

我也尝试指定确切的路径,但没有运气。

img = Image.open(open("IMG_0707.png", 'rb'))
img = Image.open(open("c:\python27\IMG_0707.png", 'rb'))
img = Image.open(open("c:/python27/IMG_0707.png", 'rb'))

等等。如果有任何帮助,我将不胜感激。 Image, im trying to open

【问题讨论】:

    标签: python png python-imaging-library steganography


    【解决方案1】:

    PNG 已损坏。这是 ImageMagick 不得不说的:

    $ convert IMG_0707.png IMG_0707-new.png 
    convert: IHDR: CRC error `IMG_0707.png' @ error/png.c/MagickPNGErrorHandler/1309.
    convert: corrupt image `IMG_0707.png' @ error/png.c/ReadPNGImage/3294.
    convert: missing an image filename `IMG_0707-new.png' @ error/convert.c/ConvertImageCommand/3011.
    

    【讨论】:

    • 非常感谢。将在 CTF 挑战赛上尝试另一种解决此问题的方法。
    【解决方案2】:
    import Image
    try:
        from StringIO import StringIO
    except ImportError:
        from io import StringIO
    import numpy as np
    
    im = Image.open(StringIO(raw_data))
    

    您好,我遇到了同样的问题,这就是解决方案 raw_data:在我的情况下,它是 base64 解码的 png 文件

    【讨论】:

      猜你喜欢
      • 2012-07-28
      • 2019-11-22
      • 2020-03-10
      • 1970-01-01
      • 2023-03-30
      • 2014-06-28
      • 1970-01-01
      • 2021-07-07
      • 1970-01-01
      相关资源
      最近更新 更多