好久不碰代码,文件操作啥的快忘光了(你丫本来就没怎么学吧!)我先放两串代码在这QAQ
拆分PDF
pdftoppm -png ?.pdf s
批量裁剪图片
裁剪图片指令,这个指令是ImageMagick带的一个什么东西,单位电脑能装
convert src.png -crop 长x高+起始像素点横坐标+起始像素低纵坐标 dest.png
然后用ROCEDU教我的无敌Python糊弄糊弄就好啦
import os
for i in range(9):
a="s-0"+str(i+1)+".png"
b="d-0"+str(i+1)+".png"
c="convert "+a+" -crop 630x930+60+0 "+b
print(c)
os.system(c)
由于PDF拆分时,生成文件名称会自动在个位前补零,所以要稍微改动一下下。
合成PDF
网上随便找了一个跑跑
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from PIL import Image
import os
def combine_imgs_pdf(folder_path, pdf_file_path):
"""
合成文件夹下的所有图片为pdf
Args:
folder_path (str): 源文件夹
pdf_file_path (str): 输出路径
"""
files = os.listdir(folder_path)
png_files = []
sources = []
for file in files:
if 'png' in file or 'jpg' or 'JPG' or 'PNG' in file:
png_files.append(folder_path + file)
png_files.sort()
output = Image.open(png_files[0])
if output.mode == "RGBA":
output = output.convert("RGB")
png_files.pop(0)
for file in png_files:
png_file = Image.open(file)
if png_file.mode == "RGBA":
png_file = png_file.convert("RGB")
sources.append(png_file)
output.save(pdf_file_path, "pdf", save_all=True, append_images=sources)
if __name__ == "__main__":
folder = r"/home/uos/Downloads/a/d/"
pdfFile = r"/home/uos/Downloads/a/d/t.pdf"
combine_imgs_pdf(folder, pdfFile)
倒数2.3行要改成自己的路径
标签:linux,files,裁剪,file,PDF,pdf,path,png From: https://www.cnblogs.com/Ziggy29/p/17387213.html