-
Notifications
You must be signed in to change notification settings - Fork 0
/
pack_textures.py
141 lines (109 loc) · 5.58 KB
/
pack_textures.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#!/usr/bin/env python3
from PIL import Image
from tkinter import ttk
import tkinter as tk
from tkinter.filedialog import askopenfilename
import argparse
def pack_textures(size:int, routes, save_filename):
"""
Generate a packed texture image from a list of image routes.
Args:
size (int): The size of each individual texture image.
routes (list): A list of image routes. Each route represents the file path to an image.
save_filename (str): The file name under which the packed texture image will be saved.
Returns:
None
Raises:
FileNotFoundError: If any of the image routes provided do not exist.
Notes:
- The packed texture image will have a size of size * 2 by size * 2.
- Each individual texture image will be pasted onto the packed texture image at a position determined by the index of the image route in the routes list.
- If an image route is an empty string, a blank texture image of size by size will be pasted at the corresponding position on the packed texture image.
- The packed texture image will be saved as a PNG file under the provided save_filename.
- The packed texture image will be displayed after it is saved.
"""
print('Message from pack textures')
result = Image.new('RGBA',[size * 2, size * 2])
positions = ([0,0],[size,0],[0,size],[size, size])
for i in range(0,4):
if routes[i] == '':
result.paste(Image.new('L',[size,size]),positions[i])
else:
result.paste(Image.open(routes[i],'r').resize([size,size]), positions[i])
result.save(save_filename,'png')
result.show()
class App():
def __init__(self, window) -> None:
"""
Initializes the class with a window object.
Parameters:
window (object): The window object to be used for the application.
Returns:
None
"""
self.win = window
self.win.config(width=800, height=600)
self.win.title('Texture channel mixer')
# Etiqueta y botones
instructions = ttk.Label(self.win,text='Este programa combina de 1 a 4 imágenes como un pack')
instructions.grid(column= 0,row= 0, sticky='WE', columnspan=2)
instructions1 = ttk.Label(self.win,text='** Se creará una imagen blanca por cada ruta faltante')
instructions1.grid(column= 0,row= 11, sticky='WE', columnspan=2)
filetypes = [('texture files',r'*.png *.tif'),('all files','*.*')]
# Canal R
r_filename = tk.StringVar()
r_label = ttk.Label(self.win,text='Primera imagen - superior izquierda')
r_label.grid(column= 0,row= 1, sticky='WE')
r_entry = ttk.Entry(self.win, textvariable=r_filename)
r_entry.grid(column= 0, row= 2, sticky='WE',padx= 5,columnspan=2)
r_button = ttk.Button(self.win,text='Examine',command= lambda: r_filename.set(askopenfilename(filetypes= filetypes)))
r_button.grid(column= 2, row= 2)
# Canal G
g_filename = tk.StringVar()
g_label = ttk.Label(self.win,text='Segunda imagen - superior derecha')
g_label.grid(column= 0, row= 3, sticky='WE')
g_entry = ttk.Entry(self.win,textvariable=g_filename)
g_entry.grid(column= 0, row= 4, sticky='WE',padx= 5, columnspan=2)
g_button = ttk.Button(self.win, text='Examine',command=lambda: g_filename.set(askopenfilename(filetypes=filetypes)))
g_button.grid(column= 2, row= 4)
# Canal B
b_filename = tk.StringVar()
b_label = ttk.Label(self.win,text='Tercera imagen - inferior izquierda')
b_label.grid(column= 0, row= 5, sticky='WE')
b_entry = ttk.Entry(self.win, textvariable=b_filename)
b_entry.grid(column= 0, row= 6, sticky='WE',padx= 5, columnspan=2)
b_button = ttk.Button(self.win, text='Examine',command=lambda: b_filename.set(askopenfilename(filetypes=filetypes)))
b_button.grid(column= 2, row= 6)
# Canal A
a_filename = tk.StringVar()
a_label = ttk.Label(self.win,text='Cuarta imagen - inferior derecha')
a_label.grid(column= 0, row= 7, sticky='WE')
a_entry = ttk.Entry(self.win, textvariable=a_filename)
a_entry.grid(column= 0, row= 8, sticky='WE',padx= 5, columnspan=2)
a_button = ttk.Button(self.win, text='Examine',command=lambda: a_filename.set(askopenfilename(filetypes=filetypes)))
a_button.grid(column= 2, row= 8)
# Aplicacion
apply_label = ttk.Label(self.win,text='Ruta y nombre del nuevo archivo resultante y tamaño')
apply_label.grid(column= 0, row= 9, sticky='WE')
apply_entry = ttk.Entry(self.win)
apply_entry.grid(column= 0, row= 10, sticky='WE',padx= 5)
sizes = [256,512,1024,2048,4096]
size_drop = tk.Spinbox(self.win, values= sizes)
size_drop.grid(column= 1, row= 10)
apply_button = ttk.Button(self.win, text='Aplicar',command=lambda:pack_textures(int(size_drop.get()),[r_filename.get(),g_filename.get(),b_filename.get(),a_filename.get()],apply_entry.get())) # .save(f'{apply_entry.getvar()}.png', 'png')
apply_button.grid(column= 2, row= 10)
def main():
# Ventana
root = tk.Tk()
application = App(root)
root.mainloop()
parser = argparse.ArgumentParser('Texture Atlas packer')
if __name__ == '__main__':
parser.add_argument('--size',type=int)
parser.add_argument('--path',nargs='+',type=str)
parser.add_argument('--name',type=str)
args = parser.parse_args()
if args.size:
pack_textures(args.size,args.path,args.name)
else:
main()