Python3 - 解析jpeg尺寸信息

5 投票
4 回答
4552 浏览
提问于 2025-04-16 16:47

我正在尝试写一个Python函数,从jpeg文件中提取宽度和高度。目前我写的代码是这样的:

import struct

image = open('images/image.jpg','rb')
image.seek(199)
#reverse hex to deal with endianness...
hex = image.read(2)[::-1]+image.read(2)[::-1]
print(struct.unpack('HH',hex))
image.close()

不过,这里有几个问题。首先,我需要在文件中查找,确定从哪里读取数据(在ff c0 00 11 08之后),其次,我还需要避免读取到嵌入的缩略图数据。有没有什么建议?

4 个回答

1

我的建议是:使用PIL(也就是Python图像库)。

>>> import Image
>>> img= Image.open("test.jpg")
>>> print img.size
(256, 256)

如果不想用PIL,可以试试Hachoir,这是一个纯Python的库;特别是hachoir-metadata,看起来能满足你的需求。

4

我在Python3中尝试了很多解决方案,但都没能成功,因为字节和字符串的处理方式有了变化。基于Acorn的解决方案,我想出了这个方法,在我这里Python3能正常工作:

import struct
import io

height = -1
width = -1

dafile = open('test.jpg', 'rb')
jpeg = io.BytesIO(dafile.read())
try:

    type_check = jpeg.read(2)
    if type_check != b'\xff\xd8':
      print("Not a JPG")
    else:
      byte = jpeg.read(1)

      while byte != b"":

        while byte != b'\xff': byte = jpeg.read(1)
        while byte == b'\xff': byte = jpeg.read(1)

        if (byte >= b'\xC0' and byte <= b'\xC3'):
          jpeg.read(3)
          h, w = struct.unpack('>HH', jpeg.read(4))
          break
        else:
          jpeg.read(int(struct.unpack(">H", jpeg.read(2))[0])-2)

        byte = jpeg.read(1)

      width = int(w)
      height = int(h)

      print("Width: %s, Height: %s" % (width, height))
finally:
    jpeg.close()
3

这个函数的JPEG部分可能会对你有帮助:http://code.google.com/p/bfg-pages/source/browse/trunk/pages/getimageinfo.py

jpeg.read(2)
b = jpeg.read(1)
try:
    while (b and ord(b) != 0xDA):
        while (ord(b) != 0xFF): b = jpeg.read(1)
        while (ord(b) == 0xFF): b = jpeg.read(1)
        if (ord(b) >= 0xC0 and ord(b) <= 0xC3):
            jpeg.read(3)
            h, w = struct.unpack(">HH", jpeg.read(4))
            break
        else:
            jpeg.read(int(struct.unpack(">H", jpeg.read(2))[0])-2)
        b = jpeg.read(1)
    width = int(w)
    height = int(h)
except struct.error:
    pass
except ValueError:
    pass

撰写回答