Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions Image_to_PDF/convert_image_to_pdf.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import sys
import img2pdf
import os

def convert_to_pdf(filepath):
output_file = "output.pdf"
supported_formats = (".jpg", ".jpeg", ".png")

if os.path.isdir(filepath):
images = [os.path.join(filepath, fname) for fname in os.listdir(filepath)
if fname.lower().endswith(supported_formats) and os.path.isfile(os.path.join(filepath, fname))]
if images:
with open(output_file, "wb") as f:
f.write(img2pdf.convert(images))
else:
print("No supported image files found in the directory.")
elif os.path.isfile(filepath):
if filepath.lower().endswith(supported_formats):
with open(output_file, "wb") as f:
f.write(img2pdf.convert(filepath))
else:
print("Unsupported file format. Please provide a JPG, JPEG, or PNG file.")
else:
print("Please input a valid file or directory.")

if __name__ == "__main__":
if len(sys.argv) != 2:
print("Usage: python script.py <file_or_directory_path>")
else:
convert_to_pdf(sys.argv[1])
1 change: 1 addition & 0 deletions Image_to_PDF/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
img2pdf==0.4.0