PIL.UnidentifiedImageError PIL.UnidentifiedImageError: cannot identify image file 'd:\\a/images/.pdf'

PIL.UnidentifiedImageError PIL.UnidentifiedImageError: cannot identify image file 'd:\\a/images/.pdf'

from PIL import Image
import PyPDF2
tess.pytesseract.tesseract_cmd = r'C:\Users\szaid\AppData\Local\Programs\Tesseract-OCR\tesseract'
translator=Translator()

project_dir = os.path.dirname(os.path.abspath(__file__))

app = Flask(__name__,
            static_url_path='',
            static_folder='static',
            template_folder='template')
photos = UploadSet('photos', IMAGES)
pdf = UploadSet('pdf', DOCUMENTS)
app.config['DEBUG'] = True
app.config['UPLOAD_FOLDER']= 'images'

class Get(object):
        def __init__(self, file): enter code here
                if render_template('image.html'):
                        self.file = tess.image_to_string(Image.open(project_dir + '/images/'+ file))
                elif render_template('pdf.html'):
                        self.file = PyPDF2.PdfFileReader(project_dir+'/pdff/'+ file)

在下面,这是将文件保存在 'images' 文件夹中的后端代码,但它给我一个错误。 我的意思是有两个不同的 html 页面,在这两个页面中,都有一个上传文件和图像的选项。但不知道如何在烧瓶中管理...

@app.route('/pdf', methods=["GET","POST"])
def pdf():
        if request.method == 'POST':
                
                if 'pdf' not in request.files:
                        return 'there is no photo'
                name1 = request.form['pdf-name'] + '.pdf'
                pdf = request.files['pdf']
                path = os.path.join(app.config['UPLOAD_FOLDER'], name1)
                pdf.save(path)
                TextObject = Get(name1)
                return TextObject.file

        return render_template('pdf.html')

error

您收到此错误是因为您的 PDF 文件路径有误。您已添加文件扩展名,但路径缺少文件名。您的路径中的斜线也有问题。正确的路径应该是:

"D:\images\yourFileName.pdf"