Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # bulk_to_jpg.py
- # well... this code seems to work for the most part
- import os
- from tkinter import *
- from tkinter import filedialog
- from PIL import Image
- import shutil
- root = Tk()
- root.title("Convert To .JPG")
- ww = 300
- hh = 80
- root.geometry(f"{ww}x{hh}+0+0")
- canvas = Canvas(root, width=ww, height=hh)
- canvas.pack(side="bottom", pady=3, fill='both', expand=1)
- def active():
- canvas.delete('all')
- canvas.create_text(ww//2, 20, text="PROCESSING...", font='ariel 14', fill='red')
- canvas.update()
- def inactive():
- canvas.delete('all')
- canvas.create_text(ww//2, 20, text="Select Folder To Commence", font='ariel 14', fill='darkgreen')
- canvas.update()
- inactive()
- folder = ''
- def select_folder():
- global folder
- folder = filedialog.askdirectory()
- if folder:
- convert_files(folder)
- EXTENSIONS_TO_CONVERT = {'bmp', 'webp', 'jfif', 'png', 'jpeg', 'jpg', 'tmp'}
- def convert_files(input_dir):
- active()
- os.makedirs(os.path.join(input_dir, 'converted to jpg'), exist_ok=True)
- output_dir = os.path.join(input_dir, 'converted to jpg')
- os.makedirs(os.path.join(input_dir, 'ERROR!'), exist_ok=True)
- errors_dir = os.path.join(input_dir, 'ERROR!')
- for filename in os.listdir(input_dir):
- ext = os.path.splitext(filename)[1][1:]
- try:
- for ttt in EXTENSIONS_TO_CONVERT:
- if ttt in ext:
- file = filename.split('.')[0]
- with Image.open(os.path.join(input_dir, filename)) as im:
- if 'transparency' in im.info:
- im = im.convert('RGBA')
- new_data = []
- for item in im.getdata():
- if item[3] == 0:
- new_data.append((255, 255, 255, 0))
- else:
- new_data.append(item)
- im.putdata(new_data)
- im = im.convert("RGB")
- im.save(os.path.join(output_dir, file + ".jpg"), "JPEG")
- try:
- os.rename(os.path.join(input_dir, filename), os.path.join(input_dir, '00000tmp_'+filename))
- except:
- print("--- unable to rename", filename)
- continue
- except:
- print("skipped file:", filename)
- try:
- shutil.copy(os.path.join(input_dir, filename), os.path.join(errors_dir, filename))
- print("saved in a temp file")
- except:
- print("!!!!! unable save in a temp file!")
- inactive()
- print('done.\n\n')
- button = Button(canvas, text="Select Folder", command=select_folder)
- button.pack(side="bottom", pady=10)
- root.mainloop()
Advertisement
Comments
-
- "FREE MONEY TRANSFER TEST FOR CONFIRMATION OF OUR SERVICE FOR NEW CUSTOMERS"
- "PAYPAL"
- "CASH APP"
- "WESTERN UNION"
- "AMAZON E-GIFT CARDS ALSO AVAILABLE.(E-CODE)"
- *********************************************
- WEB PAGE: https://dinakoenigsteincon.wixsite.com/dinagiftcards
- EMAIL: dinakoenigsteincon42@gmail.com
Add Comment
Please, Sign In to add comment
Advertisement