python+img2pdf 快速图片转pdf+(img2pdf.ExifOrientationError处理、文件被打开或占用报错处理)

使用img2pdf 将图片转pdf时会出现img2pdf.ExifOrientationError报错:

复制代码
img2pdf.ExifOrientationError: Invalid rotation (0): use --rotation=ifvalid or rotation=img2pdf.Rotation.ifvalid to ignore

这个错误是在使用img2pdf库处理图片时遇到了关于EXIF方向信息的问题(图片的EXIF旋转值为0,而img2pdf不认为这是一个有效的旋转值)。EXIF是数字照片中用于记录画质、拍摄条件等元数据的一种规范,其中包括旋转信息。

处理方式,可更具报错提示添加相关参数(--rotation=ifvalid or rotation=img2pdf.Rotation.ifvalid)解决。使用命令行参数--rotation=ifvalid来指示img2pdf在遇到无效旋转值时采取合适的行动。 如果不想修改旋转值,可以使用--rotation=none来忽略所有旋转信息。 如果想要自定义处理方式,可以使用--rotation=custom并指定每种旋转值对应的处理方法。自动修正所有有效的旋转值的参数rotation=img2pdf.Rotation.ifvalid 或 --rotation=ifvalid。

img2pdf 无法将图片按照指定大小添加到pdf中,会出现生成的pdf中每张图片都是原始图片的大小,但是可以使用其他库将图片统一修改成指定大小后再生成pdf,如和PIL库结合使用,调整图片大小或旋转图片等操作,生成新的图片后再添加到pdf中。完整代码如下:

python 复制代码
import img2pdf, os
from PIL import Image


def image_pdf(pdf_all_path, img_path, img_width: int = 800):
    filenames = [rf'{img_path}\{img_name}' for img_name in os.listdir(img_path) if
                 img_name.endswith('.png') or img_name.endswith('.jpg')]

    # 调整图片大小为统一宽度,并返回新的文件全路径列表
    new_img_fimenames = []
    for image_path in filenames:
        image = Image.open(image_path)
        # 调整图片大小,例如将图片的宽度设置为800像素
        new_height = int(image.height * img_width / image.width)
        resized_image = image.resize((img_width, new_height))
        new_img_path = rf'{image_path.split('.')[0]}_new.{image_path.split('.')[-1]}'
        resized_image.save(new_img_path)
        new_img_fimenames.append(new_img_path)

    # 将统一处理过大小的文件转入到pdf中
    try:
        with open(pdf_all_path, 'wb+') as f:
            content = img2pdf.convert(new_img_fimenames, rotation=img2pdf.Rotation.ifvalid)  # convert函数 用来转PDF
            f.write(content)

            # 删除调整过比例的新图片文件
            for new_img_fimename in new_img_fimenames:  # 操作完成后将调整比例的文件删除
                if os.path.exists(new_img_fimename):
                    os.remove(new_img_fimename)
                else:
                    print('要删除的目标文件不存在')

    except FileExistsError:
        print('文件被占用')
    except FileNotFoundError:
        print('文件不存在')
    except PermissionError:
        print('文件可能被占用或没有访问权限,无法打开文件')
    except IOError:
        print('文件被删除或移动')


if __name__ == '__main__':
    pdf_path = r'C:\Users\Administrator\Desktop\test.pdf'
    img_path = r'C:\Users\Administrator\Desktop\testfile\测试图片'
    image_pdf(pdf_path, img_path)
相关推荐
卷无止境4 分钟前
Python的contextlib与 FastAPI 中的上下文管理
后端·python·fastapi
测试199810 分钟前
接口自动化测试的全面解析与实战指南
自动化测试·软件测试·python·测试工具·职场和发展·测试用例·接口测试
whcyhhh13 分钟前
头歌实践教学平台:数据科学与大数据技术导论(九上)
大数据·python
卷无止境20 分钟前
FastAPI Events 深度解析与工程实践
后端·python·fastapi
FYKJ_201026 分钟前
springboot网上购书商城---附源码15749
java·spring boot·后端·python·spark·django·php
青 春 记 忆33 分钟前
LeetCode 234. 回文链表|Python 解法详解
python·leetcode·链表
招财小梗35 分钟前
沈阳AI企业服务真能降本增效?
大数据·人工智能·python
敲代码还房贷1 小时前
安装wsl报错:HCS_E_HYPERV_NOT_INSTALLED
linux·python·ubuntu
whcyhhh1 小时前
头歌实践教学平台:数据科学与大数据技术导论(九下)
大数据·python
AI导出鸭1 小时前
ChatGPT的LaTeX生成PDF文件复制后数学公式乱码,怎样修改?苹果用户的底层逻辑与优雅解法
人工智能·chatgpt·pdf·ai导出鸭