Advertisement
here2share

# bulk_to_jpg.py

Mar 23rd, 2023 (edited)
1,204
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.35 KB | None | 0 0
  1. # bulk_to_jpg.py
  2. # well... this code seems to work for the most part
  3.  
  4. import os
  5. from tkinter import *
  6. from tkinter import filedialog
  7. from PIL import Image
  8. import shutil
  9.  
  10. root = Tk()
  11. root.title("Convert To .JPG")
  12. ww = 300
  13. hh = 80
  14. root.geometry(f"{ww}x{hh}+0+0")
  15. canvas = Canvas(root, width=ww, height=hh)
  16. canvas.pack(side="bottom", pady=3, fill='both', expand=1)
  17.  
  18. def active():
  19.     canvas.delete('all')
  20.     canvas.create_text(ww//2, 20, text="PROCESSING...", font='ariel 14', fill='red')
  21.     canvas.update()
  22.  
  23. def inactive():
  24.     canvas.delete('all')
  25.     canvas.create_text(ww//2, 20, text="Select Folder To Commence", font='ariel 14', fill='darkgreen')
  26.     canvas.update()
  27. inactive()
  28.  
  29. folder = ''
  30. def select_folder():
  31.     global folder
  32.     folder = filedialog.askdirectory()
  33.     if folder:
  34.         convert_files(folder)
  35.  
  36. EXTENSIONS_TO_CONVERT = {'bmp', 'webp', 'jfif', 'png', 'jpeg', 'jpg', 'tmp'}
  37.  
  38. def convert_files(input_dir):
  39.     active()
  40.     os.makedirs(os.path.join(input_dir, 'converted to jpg'), exist_ok=True)
  41.     output_dir = os.path.join(input_dir, 'converted to jpg')
  42.     os.makedirs(os.path.join(input_dir, 'ERROR!'), exist_ok=True)
  43.     errors_dir = os.path.join(input_dir, 'ERROR!')
  44.     for filename in os.listdir(input_dir):
  45.         ext = os.path.splitext(filename)[1][1:]
  46.         try:
  47.             for ttt in EXTENSIONS_TO_CONVERT:
  48.                 if ttt in ext:
  49.                     file = filename.split('.')[0]
  50.                     with Image.open(os.path.join(input_dir, filename)) as im:
  51.                         if 'transparency' in im.info:
  52.                             im = im.convert('RGBA')
  53.                             new_data = []
  54.                             for item in im.getdata():
  55.                                 if item[3] == 0:
  56.                                     new_data.append((255, 255, 255, 0))
  57.                                 else:
  58.                                     new_data.append(item)
  59.                             im.putdata(new_data)
  60.                         im = im.convert("RGB")
  61.                         im.save(os.path.join(output_dir, file + ".jpg"), "JPEG")
  62.                         try:
  63.                             os.rename(os.path.join(input_dir, filename), os.path.join(input_dir, '00000tmp_'+filename))
  64.                         except:
  65.                             print("--- unable to rename", filename)
  66.                     continue
  67.         except:
  68.             print("skipped file:", filename)
  69.             try:
  70.                 shutil.copy(os.path.join(input_dir, filename), os.path.join(errors_dir, filename))
  71.                 print("saved in a temp file")
  72.             except:
  73.                 print("!!!!! unable save in a temp file!")
  74.                
  75.     inactive()
  76.     print('done.\n\n')
  77.    
  78. button = Button(canvas, text="Select Folder", command=select_folder)
  79. button.pack(side="bottom", pady=10)
  80.  
  81. root.mainloop()
Advertisement
Comments
  • MoneyTransfer
    1 year
    # text 0.31 KB | 0 0
    1. "FREE MONEY TRANSFER TEST FOR CONFIRMATION OF OUR SERVICE FOR NEW CUSTOMERS"
    2.  
    3.  
    4. "PAYPAL"
    5. "CASH APP"
    6. "WESTERN UNION"
    7.  
    8.  
    9. "AMAZON E-GIFT CARDS ALSO AVAILABLE.(E-CODE)"
    10. *********************************************
    11.  
    12. WEB PAGE: https://dinakoenigsteincon.wixsite.com/dinagiftcards
    13. EMAIL: dinakoenigsteincon42@gmail.com
Add Comment
Please, Sign In to add comment
Advertisement