这是一个错误吗?它演示了当您使用 libtiff 从打开的 tiff 文件句柄中提取图像时会发生什么。它适用于 python 2.x,不适用于 python 3.2.3
import os
# any file will work here, since it's not actually loading the tiff
# assuming it's big enough for the seek
filename = "/home/kostrom/git/wiredfool-pillow/Tests/images/multipage.tiff"
def test():
fp1 = open(filename, "rb")
buf1 = fp1.read(8)
fp1.seek(28)
fp1.read(2)
for x in range(16):
fp1.read(12)
fp1.read(4)
fd = os.dup(fp1.fileno())
os.lseek(fd, 28, os.SEEK_SET)
os.close(fd)
# this magically fixes it: fp1.tell()
fp1.seek(284)
expect_284 = fp1.tell()
print ("expected 284, actual %d" % expect_284)
test()
我认为错误的输出是: 预期 284,实际 -504
取消注释 fp1.tell() 会产生一些……副作用……稳定 py3 句柄,我不知道为什么。如果有人可以测试其他版本的 python3,我也将不胜感激。
请您参考如下方法:
不,这不是错误。 Python 3 io library ,它为您提供来自 open() 调用的文件对象,为您提供一个缓冲文件对象。对于二进制文件,你会得到一个(子类)io.BufferedIOBase .
Python 2 文件对象要原始得多,尽管您可以使用 io library还有。
通过在操作系统级别进行搜索,您将绕过缓冲区并破坏内部状态。一般来说,正如医生对提示捏他的皮肤很疼的病人说的那样:不要那样做。
如果您迫切需要这样做,至少可以通过 io.RawIOBase class 使用底层原始文件对象(io.BufferedIO.raw attribute 的子类) :
fp1 = open(filename, "rb").raw
