Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/278.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/xamarin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 以图片形式阅读PDF_Python_Python 3.x_Pdf_Pixel - Fatal编程技术网

Python 以图片形式阅读PDF

Python 以图片形式阅读PDF,python,python-3.x,pdf,pixel,Python,Python 3.x,Pdf,Pixel,我有一些pdf,我想把它们作为图片来阅读,以获得所有像素的信息 因此,我首先尝试将pdf转换为jpeg: from pdf2image import convert_from_path img = convert_from_path('mypdf.pdf') 这很有效。现在我将尝试获取像素信息,但我有一个错误: import matplotlib.pyplot as plt pixel_img = plt.imread(img[0]) TypeError: Object does not a

我有一些pdf,我想把它们作为图片来阅读,以获得所有像素的信息

因此,我首先尝试将pdf转换为jpeg:

from pdf2image import convert_from_path
img = convert_from_path('mypdf.pdf')
这很有效。现在我将尝试获取像素信息,但我有一个错误:

import matplotlib.pyplot as plt
pixel_img = plt.imread(img[0])

TypeError: Object does not appear to be a 8-bit string path or a Python file-like object
我不理解它,因为当我使用plt.imread()读取原始的.jpeg文件时,它似乎可以工作。img是一个PIL对象,所以它不应该是一个“类似python文件的对象”吗

我还尝试使用PIL包(img作为PIL对象),并尝试使用不同的方法读取(但我得到的是另一个错误):

这不是我想要的,因为只需将pdf另存为jpg即可。但我不想保存它,我只想阅读它并获取像素信息


谢谢

convert\u from\u path
返回PIL图像列表,因此您不能将其视为文件

以下内容将PDF页面转换为PIL图像,将第一页/图像转换为numpy数组(便于访问像素),并获取位置y=10、x=15处的像素:

from pdf2image import convert_from_path
import numpy as np

images = convert_from_path('test.pdf')

# to numpy array
image = np.array(images[0])

# get pixel at position y=10, x=15
# where pix is an array of R, G, B.
# e.g. pix[0] is the red part of the pixel
pix = image[10,15]

您的导入是反向的。PIL图像不是文件,您可以将其转换为文件:可能的副本是否有帮助:?但有了它,我只将文件保存为jpg。我想要的是读取它并获取像素信息,而不是将其另存为。jpgimages[0]是标准的PIL图像。也许这将帮助您获得像素:。另一种可能是将其转换为numpy数组,该数组可能更容易使用,也可能不容易使用(
np_image=numpy.array(images[0])
)。我更新了答案,以演示如何获得单个像素
from pdf2image import convert_from_path
import numpy as np

images = convert_from_path('test.pdf')

# to numpy array
image = np.array(images[0])

# get pixel at position y=10, x=15
# where pix is an array of R, G, B.
# e.g. pix[0] is the red part of the pixel
pix = image[10,15]