hf1agideia commited on
Commit
afc0d4c
·
verified ·
1 Parent(s): 5cdadee

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +90 -0
app.py ADDED
@@ -0,0 +1,90 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ Hugging Face's logo
2
+ Hugging Face
3
+ Models
4
+ Datasets
5
+ Spaces
6
+ Posts
7
+ Docs
8
+ Enterprise
9
+ Pricing
10
+
11
+
12
+
13
+ Spaces:
14
+
15
+ 13ze
16
+ /
17
+ img-webp-150
18
+
19
+
20
+ like
21
+ 0
22
+ App
23
+ Files
24
+ Community
25
+ img-webp-150
26
+ /
27
+ app.py
28
+
29
+ 13ze's picture
30
+ 13ze
31
+ Update app.py
32
+ f9caddd
33
+ verified
34
+ 10 months ago
35
+ raw
36
+
37
+ Copy download link
38
+ history
39
+ blame
40
+ contribute
41
+ delete
42
+
43
+ 1.94 kB
44
+ import gradio as gr
45
+ from PIL import Image
46
+ import os
47
+ import tempfile
48
+
49
+ def converter_imagens(caminhos_imagens, altura_max, largura_max):
50
+ arquivos_convertidos = []
51
+ for caminho_imagem in caminhos_imagens:
52
+ img = Image.open(caminho_imagem).convert("RGBA") # Garante que a imagem tenha um canal alfa
53
+ largura_orig, altura_orig = img.size
54
+
55
+ # Define as novas dimensões respeitando os limites
56
+ if altura_orig > altura_max or largura_orig > largura_max:
57
+ if altura_orig / altura_max > largura_orig / largura_max:
58
+ nova_altura = altura_max
59
+ nova_largura = int((nova_altura / altura_orig) * largura_orig)
60
+ else:
61
+ nova_largura = largura_max
62
+ nova_altura = int((nova_largura / largura_orig) * altura_orig)
63
+ else:
64
+ nova_largura, nova_altura = largura_orig, altura_orig
65
+
66
+ img = img.resize((nova_largura, nova_altura), Image.LANCZOS)
67
+
68
+ # Define o caminho do arquivo convertido com o mesmo nome, mas com a extensão .webp
69
+ nome_base = os.path.splitext(os.path.basename(caminho_imagem))[0]
70
+ arquivo_temp = tempfile.NamedTemporaryFile(delete=False, suffix=".webp", prefix=nome_base + "_")
71
+ img.save(arquivo_temp, format='WEBP')
72
+ arquivo_temp.close()
73
+
74
+ arquivos_convertidos.append(arquivo_temp.name)
75
+
76
+ return arquivos_convertidos
77
+
78
+ interface = gr.Interface(
79
+ fn=converter_imagens,
80
+ inputs=[
81
+ gr.Files(type="filepath", label="Carregar Imagens"),
82
+ gr.Number(label="Altura Máxima (px)", value=100, precision=0),
83
+ gr.Number(label="Largura Máxima (px)", value=200, precision=0)
84
+ ],
85
+ outputs=gr.Files(label="Imagens Convertidas"),
86
+ title="Conversor de Imagens para .webp",
87
+ description="Carregue imagens para convertê-las para o formato .webp com altura e largura máximas personalizadas."
88
+ )
89
+
90
+ interface.launch()