diff --git a/Image_to_PDF/convert_image_to_pdf.py b/Image_to_PDF/convert_image_to_pdf.py new file mode 100644 index 0000000..c84bdff --- /dev/null +++ b/Image_to_PDF/convert_image_to_pdf.py @@ -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 ") + else: + convert_to_pdf(sys.argv[1]) diff --git a/Image_to_PDF/requirements.txt b/Image_to_PDF/requirements.txt new file mode 100644 index 0000000..31fc281 --- /dev/null +++ b/Image_to_PDF/requirements.txt @@ -0,0 +1 @@ +img2pdf==0.4.0 \ No newline at end of file