Advertisement
trungore10

python_image_to_pdf

Aug 19th, 2019
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.44 KB | None | 0 0
  1. import os
  2. from fpdf import FPDF
  3. from PIL import Image
  4.  
  5. cwd = os.getcwd();
  6. PNG_Folder = cwd + "\\PNG_Folder";
  7. PDF_Folder = cwd + "\\PDF_Folder";
  8.  
  9. def Convert_PNG(link, Name):
  10.     Folder = PNG_Folder + "\\" + Name;
  11.  
  12.     if not os.path.isdir(Folder):
  13.         os.makedirs(Folder);
  14.  
  15.     num = 0;
  16.     for image in os.listdir(link):
  17.         num = num + 1;
  18.         path = link + "\\" + image;
  19.  
  20.         try:
  21.             im = Image.open(path);
  22.         except:
  23.             continue;
  24.  
  25.         New = im.convert("RGB");
  26.         New.save( Folder + "\\" + str(num) + ".png" );
  27.  
  28.  
  29. def Convert_Image_To_Invalid_Form():
  30.     print("CONVERT TO INVALID FORM...");
  31.  
  32.     File_Image = cwd + "\\" + "Folder";
  33.  
  34.     for dirs in os.listdir(File_Image):
  35.         link = File_Image + "\\" + dirs;
  36.         if os.path.isdir(link):
  37.             Convert_PNG(link, dirs);
  38.  
  39.     print("DONE");
  40.  
  41. def process(link, Name):
  42.     print("CONVERTING " + Name + "...");
  43.  
  44.     pdf = FPDF();
  45.     for image in os.listdir(link):
  46.         path = link + "\\" + image;
  47.         pdf.add_page();
  48.         pdf.image(path, 0, 0, 210, 297);
  49.  
  50.     Result = PDF_Folder + "\\" + "ORE_" + Name + ".pdf";
  51.     pdf.output(Result, "F");
  52.  
  53.     print("DONE");
  54.  
  55. def Compress_Image_To_PDF():
  56.     for dirs in os.listdir(PNG_Folder):
  57.         link = PNG_Folder + "\\" + dirs;
  58.         process(link, dirs);
  59.  
  60. def main():
  61.     Convert_Image_To_Invalid_Form();
  62.     Compress_Image_To_PDF();
  63.  
  64. if __name__ == "__main__":
  65.     if not os.path.isdir(PNG_Folder):
  66.         os.makedirs(PNG_Folder);
  67.  
  68.     if not os.path.isdir(PDF_Folder):
  69.         os.makedirs(PDF_Folder);
  70.  
  71.     main();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement