拍照的试卷合成工具,把图片从A3转A4,还可以附加不用转的文件,最后合成PDF

查看 81|回复 6
作者:dreamrise   
拍照的试卷合成工具(拍照的图片本身建议先用夸克之类的美化处理一下)
把图片从A3转A4,还可以附加不用转的文件,最后合成PDF
把A3图片放入doc_path下的img目录,A4图片放入plus目录
如果需要调整顺序,请在放入前起好名字
输出会按文件名进行排序,
import os
from PIL import Image
import img2pdf
class A4Pdf:
    def __init__(self, doc_path):
        self.doc_path = doc_path
        # 创建相关目录
        self.img_path = os.path.join(self.doc_path, "img")
        self.plus_path = os.path.join(self.doc_path, "plus")
        self.a4_path = os.path.join(self.doc_path, "a4")
        for path in [
            self.img_path,
            self.plus_path,
            self.a4_path,
        ]:
            if not os.path.exists(path):
                os.makedirs(path)
    def checkfile(self):
        img_files = os.listdir(self.img_path)
        print(self.img_path, img_files)
        img_files = os.listdir(self.plus_path)
        print(self.plus_path, img_files)
    def a3toa4(self):
        try:
            img_files = os.listdir(self.img_path)
            for idx, i in enumerate(img_files):
                # i = img_files[idx]
                if i.endswith(".jpg") or i.endswith(".png"):
                    infile = os.path.join(self.img_path, i)
                    # print(infile)
                    img = Image.open(infile)
                    size_img = img.size
                    # 准备将图片切割成2张小图片,这里后面的2是开根号以后的数,比如你想分割为9张,将2改为3即可
                    weight = int(size_img[0] // 2)
                    height = int(size_img[1])
                    for k in range(2):
                        box = (weight * k, 0, weight * (k + 1), height)
                        region = img.crop(box)
                        # 输出路径
                        outfile = "{}/{}-{}.jpg".format(self.a4_path, idx, k)
                        print(f"A3TOA4:{outfile}")
                        region.save(outfile)
        except Exception as e:
            print(f"A3TOA4出现错误: {e}")
    def merge_pdf(self, pdf_name):
        with open(pdf_name, "wb") as f:
            imgs = []
            for fname in os.listdir(self.a4_path):
                if not fname.endswith(".jpg") and not fname.endswith(".png"):
                    continue
                path = os.path.join(self.a4_path, fname)
                imgs.append(path)
            for fname in os.listdir(self.plus_path):
                if not fname.endswith(".jpg") and not fname.endswith(".png"):
                    continue
                path = os.path.join(self.plus_path, fname)
                imgs.append(path)
            # 输出会按文件名进行排序
            imgs.sort(key=lambda x: os.path.basename(x))
            # 按文件名进行排序
            print("merge_pdf:", imgs)
            f.write(img2pdf.convert(imgs))
        print("输出文件", pdf_name)
def proc():
    doc_path = r"D:\EMC2\w\PDF分割-去水印记录\0403"
    """
    把A3图片放入doc_path下的img目录,A4图片放入plus目录
    如果需要调整顺序,请在放入前起好名字
    输出会按文件名进行排序,
    """
    # 输出的文件名
    pdf_name = os.path.join(doc_path, "合成.pdf")
    # 建个实例
    task = A4Pdf(doc_path)
    # A3图片是左右对切
    task.a3toa4()
    # 合成文件
    task.merge_pdf(pdf_name)
if __name__ == "__main__":
    proc()

图片, 放入

blindcat   

学习一下
czy051014   

学习一下
cxincn   

学习一下,看怎样实现
dingbin99   

学习一下
yzh223   

不错,其实可以考虑加个界面。网上下载的娃的试卷好多都是a3幅面的pdf,准备自己写个,转成a4幅面的好打印
dreamrise
OP
  


yzh223 发表于 2024-4-7 08:02
不错,其实可以考虑加个界面。网上下载的娃的试卷好多都是a3幅面的pdf,准备自己写个,转成a4幅面的好打印

已经写过一个,不过代码没调整好(基于别人的),下次再发.
您需要登录后才可以回帖 登录 | 立即注册

返回顶部