Created
December 2, 2020 13:58
-
-
Save villares/872404b6b2464d795a4d57ce00805150 to your computer and use it in GitHub Desktop.
Linhas com imagens
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from __future__ import unicode_literals , division | |
imagens = [] # lista vazia | |
w, h = 350, 500 | |
def setup(): | |
global colunas, linhas | |
size(2150, 1000) | |
colunas, linhas = width // w, height // h | |
print('Posições na grade: ' + str(colunas * linhas)) | |
def draw(): | |
background(0) | |
contador = 0 | |
# Desenha uma grade com as imagens na lista `imagens` | |
for l in range(linhas): | |
y = 10 + l * h | |
largura, x = 0, 0 | |
for c in range(colunas): | |
if contador < len(imagens): | |
x = x + largura | |
img = imagens[contador][1] | |
fator = h / img.height | |
largura = img.width * fator | |
image(img, x, y, largura, img.height * fator) | |
contador += 1 | |
def keyPressed(): | |
if key == 'o': | |
selectFolder("Selecione uma pasta", "adicionar_imagens") | |
if key == ' ': | |
imagens[:] = [] | |
if key == 'p': | |
saveFrame('####.png') | |
def adicionar_imagens(selection): | |
if selection == None: | |
print("Seleção cancelada.") | |
else: | |
dir_path = selection.getAbsolutePath() | |
print("Pasta selecionada: " + dir_path) | |
for file_name, file_path in lista_imagens(dir_path): | |
img = loadImage(file_path) | |
img_name = file_name.split('.')[0] | |
print("imagem " + img_name + " carregada.") | |
imagens.append((img_name, img)) | |
print('Número de imagens: ' + str(len(imagens))) | |
def lista_imagens(dir=None): | |
""" | |
Devolve uma a lista de tuplas com os nomes dos arquivos de imagem e os caminhos | |
completos para cada uma das images na pasta `dir` ou na pasta /data/ do sketch. | |
Requer a função imgext() para decidir quais extensões aceitar. | |
""" | |
from os import listdir | |
from os.path import isfile, join | |
data_path = dir or sketchPath('data') | |
try: | |
f_list = [(f, join(data_path, f)) for f in listdir(data_path) | |
if isfile(join(data_path, f)) and imgext(f)] | |
except Exception as e: | |
print("Erro ({0}): {1}".format(e.errno, e.strerror)) | |
return [] | |
return f_list | |
def imgext(file_name): | |
ext = file_name.split('.')[-1] | |
# extensões dos formatos de imagem que o Processing aceita! | |
valid_ext = ('jpg', | |
'png', | |
'jpeg', | |
'gif', | |
'tif', | |
'tga', | |
) | |
return ext.lower() in valid_ext |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment