Advertisement
MizunoBrasil

converte-extensão-webp-para-jpg-e-apaga-o-webp

May 27th, 2024 (edited)
412
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.22 KB | None | 0 0
  1. # Após escolher o diretório de saída e gerar o jpg o arquivo webp é deletado do hd
  2.  
  3. import tkinter as tk
  4. from tkinter import filedialog, messagebox
  5. from PIL import Image
  6. from datetime import datetime
  7. import os
  8.  
  9. def convert_image():
  10.     filepath = filedialog.askopenfilename(filetypes=[("WebP files", "*.webp")])
  11.     if filepath:
  12.         image = Image.open(filepath)
  13.        
  14.         def save_image(save_path):
  15.             try:
  16.                 # Salva a imagem convertida
  17.                 image.save(save_path)
  18.                 messagebox.showinfo("Sucesso", f"Imagem convertida e salva como {save_path}")
  19.                
  20.                 # Excluir o arquivo de origem .webp
  21.                 os.remove(filepath)
  22.                
  23.             except Exception as e:
  24.                 messagebox.showerror("Erro", f"Ocorreu um erro ao salvar a imagem: {e}")
  25.        
  26.         # Obtém a data e hora atual
  27.         current_datetime = datetime.now()
  28.         formatted_datetime = current_datetime.strftime("%d-%m-%Y-%H-%M-%S")
  29.        
  30.         # Cria o nome do arquivo com a data e hora formatadas
  31.         default_filename = f"{formatted_datetime}.jpg"
  32.        
  33.         # Pede ao usuário para escolher a localização de destino com o nome do arquivo padrão
  34.         save_path = filedialog.asksaveasfilename(defaultextension=".jpg", initialfile=default_filename, filetypes=[("JPEG files", "*.jpg")], title="ESCOLHA A PASTA E O NOME DO ARQUIVO QUE DESEJA SALVAR")
  35.         if save_path:
  36.             save_image(save_path)
  37.  
  38. root = tk.Tk()
  39. root.title("Conversor de imagem .webp para .jpg")
  40. root.geometry("500x150")
  41.  
  42. # Centralizar a janela inicial
  43. root.update_idletasks()
  44. screen_width = root.winfo_screenwidth()
  45. screen_height = root.winfo_screenheight()
  46. position_right = int(screen_width/2 - root.winfo_width()/2)
  47. position_down = int(screen_height/2 - root.winfo_height()/2)
  48. root.geometry(f"+{position_right}+{position_down}")
  49.  
  50. btn_convert = tk.Button(root, text="Carregar arquivo", command=convert_image)
  51. btn_convert.pack(pady=20)
  52.  
  53. # Adiciona a mensagem "Março 2024, Mizuno" no canto inferior esquerdo
  54. mensagem_label = tk.Label(root, text="Última versão: 27/05/2024 - Mizuno")
  55. mensagem_label.place(x=10, y=130)
  56.  
  57. root.mainloop()
  58.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement