【发布时间】:2014-06-25 14:26:22
【问题描述】:
我正在尝试使用 Python 更新 JPEG 文件中嵌入的 JFIF 缩略图。
这是应该实现此目的的(有点骇人听闻的)方法:
def set_thumbnail(self, data):
# Data of the updated thumbnail
data = bytearray(data)
# Get offset of the old thumbnail data
offset = (self._exif_start +
self._unpack('I', self._get_tag_offset(0x201)+8))
# Get size of the old thumbnail
old_size = self._unpack('I', self._get_tag_offset(0x202)+8)
try:
# Strip everything between the JFIF APP1 and the quant table
jfif_start = data.index('\xff\xe0')
quant_start = data.index('\xff\xdb')
stripped_data = data[0:jfif_start] + data[quant_start:]
except ValueError:
stripped_data = data
# Writes the new length to the buffer
self._pack('I', self._get_tag_offset(0x202)+8, len(stripped_data))
# Writes the new data to the image buffer
self._buf[offset:offset+old_size] = stripped_data
当我重写旧缩略图时,此功能可以正常工作,即缩略图数据的大小不会改变。但是,一旦我对其应用一些转换(例如裁剪或旋转)并再次存储它,生成的文件似乎不再有效。
我上传了original image 和one with an updated thumbnail 以便更好地比较。
我从中得到的错误,例如identify 如下:
identify.im6: Invalid JPEG file structure: two SOI markers `/tmp/thumb_rotated.jpg' @ error/jpeg.c/JPEGErrorHandler/316.
在比较两张图片时,0x202 大小标签中的值与嵌入的缩略图数据的大小相匹配,文件也相应变大。
【问题讨论】:
标签: python image jpeg exif jfif