-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpdf_conversion.py
29 lines (24 loc) · 1.14 KB
/
pdf_conversion.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import os
import subprocess
def convert_images_to_pdf():
# Walk through all files in the current directory and its subdirectories
for path, subdirs, files in os.walk('.'):
# Skip directories that contain subdirectories
if subdirs:
continue
# Get the name of the current directory
name = os.path.basename(path)
# Create an output directory within the current directory
output_dir = os.path.join(path, "output")
os.makedirs(output_dir, exist_ok=True)
# Define the command to crop images using ImageMagick's 'mogrify'
crop_command = f"mogrify -path \"{output_dir}\" -crop 1672x789+124+197 \"{path}\"/*.jpg"
# Define the command to convert cropped images to a PDF using ImageMagick's 'convert'
convert_command = f"convert \"{output_dir}/*.jpg\" \"{output_dir}/{name}.pdf\""
# Execute the cropping command
subprocess.run(crop_command, shell=True)
# Execute the conversion command
subprocess.run(convert_command, shell=True)
if __name__ == "__main__":
# Run the function to convert images to PDF
convert_images_to_pdf()