Skip to content

Instantly share code, notes, and snippets.

@gustavofonseca
Created November 26, 2014 11:53
Show Gist options
  • Save gustavofonseca/c04e6110a88ee4a56af1 to your computer and use it in GitHub Desktop.
Save gustavofonseca/c04e6110a88ee4a56af1 to your computer and use it in GitHub Desktop.
#coding: utf-8
import os
from os.path import join, getsize
import re
IMG_PATTERN = re.compile(r'^\w{4}-\w{4}-')
def lista_tifs(top):
for dirpath, dirnames, filenames in os.walk(top):
for filename in filenames:
if filename.endswith('.tif') and re.match(IMG_PATTERN, filename):
yield filename, getsize(os.path.normpath(join(dirpath, filename)))
def agrega_tifs(tifs):
artigos = {}
for filename, nbytes in tifs:
try:
artname, seq = filename.rsplit('-', 1)
except ValueError:
continue
artigo = artigos.setdefault(artname, {'total_img': 0, 'total_bytes': 0})
artigo['total_img'] += 1
artigo['total_bytes'] += nbytes
return artigos
if __name__ == '__main__':
total_artigos = total_imagens = total_bytes = 0
print u'-' * 80
# Artigo,Total de imagens,Total em bytes,Tamanho médio
for artigo, info in agrega_tifs(lista_tifs('.')).items():
print u','.join([artigo, unicode(info['total_img']), unicode(info['total_bytes']),
unicode(info['total_bytes'] / info['total_img'])])
total_artigos += 1
total_imagens += info['total_img']
total_bytes += info['total_bytes']
print u'-' * 80
print u'Média de imagens por artigo:', total_imagens / total_artigos
print u'Média de tamanho das imagens (bytes):', total_bytes / total_imagens
print u'-' * 80
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment