Advertisement
MizunoBrasil

Converte Webp para jpg ou png

Jun 28th, 2023
833
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.17 KB | None | 0 0
  1. import os
  2. import tkinter as tk
  3. from tkinter import filedialog
  4. from PIL import Image
  5.  
  6. class ImageConverterApp:
  7.     def __init__(self, root):
  8.         self.root = root
  9.         self.root.title("Image Converter")
  10.         self.root.geometry("650x250")
  11.         self.root.resizable(False, False)
  12.         self.root.configure(background="white")
  13.  
  14.         self.file_path = None
  15.  
  16.         self.choose_button = tk.Button(
  17.             self.root, text="Escolher arquivo", font=("Arial", 12), command=self.choose_file
  18.         )
  19.         self.choose_button.pack(pady=20)
  20.  
  21.         self.convert_button = tk.Button(
  22.             self.root, text="Converter", font=("Arial", 12), command=self.convert_image, state=tk.DISABLED
  23.         )
  24.         self.convert_button.pack(pady=20)
  25.  
  26.         self.drop_label = tk.Label(
  27.             self.root, text="Nenhum arquivo selecionado", font=("Arial", 12), bg="white"
  28.         )
  29.         self.drop_label.pack(pady=10)
  30.  
  31.     def choose_file(self):
  32.         self.file_path = filedialog.askopenfilename(filetypes=[("WebP Image", "*.webp")])
  33.         if self.file_path:
  34.             self.drop_label.config(text="Arquivo selecionado: " + os.path.basename(self.file_path))
  35.             self.convert_button.config(state=tk.NORMAL)
  36.         else:
  37.             self.drop_label.config(text="Nenhum arquivo selecionado")
  38.             self.convert_button.config(state=tk.DISABLED)
  39.  
  40.     def convert_image(self):
  41.         save_path = filedialog.asksaveasfilename(
  42.             defaultextension=".png", filetypes=[("PNG Image", "*.png"), ("JPEG Image", "*.jpg")]
  43.         )
  44.         if save_path:
  45.             try:
  46.                 image = Image.open(self.file_path)
  47.                 image.save(save_path)
  48.                 self.drop_label.config(text="Conversão concluída! Arquivo salvo como " + os.path.basename(save_path))
  49.             except:
  50.                 self.drop_label.config(text="Erro ao converter a imagem!")
  51.         else:
  52.             self.drop_label.config(text="Operação cancelada!")
  53.  
  54. root = tk.Tk()
  55. root.title("Image Converter")
  56. root.geometry("800x600")
  57. root.resizable(False, False)
  58. root.configure(background="white")
  59. app = ImageConverterApp(root)
  60. root.mainloop()
  61.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement