Created
June 28, 2024 05:05
-
-
Save nik-hil/9843651d077ef3b31f212952d8f8b3bb to your computer and use it in GitHub Desktop.
Convert images to pdf using python.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import PyPDF2 | |
from reportlab.lib.pagesizes import letter | |
from reportlab.pdfgen import canvas | |
from PIL import Image | |
import argparse | |
def convert_images_to_pdf(image_paths, output_pdf_path): | |
c = canvas.Canvas(output_pdf_path, pagesize=letter) | |
for image_path in image_paths: | |
img = Image.open(image_path) | |
img_width, img_height = img.size | |
c.setPageSize((img_width, img_height)) | |
c.drawImage(image_path, 0, 0, width=img_width, height=img_height) | |
c.showPage() | |
c.save() | |
def main(): | |
parser = argparse.ArgumentParser(description='Convert images to PDF') | |
parser.add_argument('image_paths', nargs='+', help='Input image paths') | |
parser.add_argument('output_pdf_path', help='Output PDF path') | |
args = parser.parse_args() | |
convert_images_to_pdf(args.image_paths, args.output_pdf_path) | |
print(f'PDF created at {args.output_pdf_path}') | |
if __name__ == "__main__": | |
main() | |
# python img2pdf.py image1.jpg image2.jpg output.pdf |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# pip install -r requirements.txt | |
PyPDF2 | |
pillow | |
reportlab |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment