[我正在尝试使用PyPDF2从pdf提取图像,但是当我的代码得到它时,该图像与实际应有的外观完全不同,请看下面的示例:
但这实际上应该是这样的:
<< img src =“ https://image.soinside.com/eyJ1cmwiOiAiaHR0cHM6Ly9pLmliYi5jby9waFFYRnpUL2ltZzI5LmpwZyJ9” alt =“文本”>
这是我正在使用的pdf:
https://www.hbp.com/resources/SAMPLE%20PDF.pdf
这是我的代码:
pdf_filename = "SAMPLE.pdf"
pdf_file = open(pdf_filename, 'rb')
cond_scan_reader = PyPDF2.PdfFileReader(pdf_file)
page = cond_scan_reader.getPage(0)
xObject = page['/Resources']['/XObject'].getObject()
i = 0
for obj in xObject:
# print(xObject[obj])
if xObject[obj]['/Subtype'] == '/Image':
if xObject[obj]['/Filter'] == '/DCTDecode':
data = xObject[obj]._data
img = open("{}".format(i) + ".jpg", "wb")
img.write(data)
img.close()
i += 1
并且由于我需要将图像保持在彩色模式下,如果是CMYK,我不能将其转换为RBG,因为我需要这些信息。另外,我正在尝试从pdf图像中获取dpi,该信息是否始终存储在图像中?在此先感谢