|
import tarfile |
|
import os |
|
import shutil |
|
|
|
IMAGE_EXTENSION = '.png' |
|
|
|
|
|
if __name__ == "__main__": |
|
|
|
|
|
if os.path.exists('./temp'): |
|
for filename in os.listdir('./temp'): |
|
os.remove('./temp/' + filename) |
|
os.rmdir('./temp') |
|
|
|
|
|
os.makedirs('./temp') |
|
|
|
|
|
|
|
for filename in os.listdir('./data/images'): |
|
|
|
text_filename = filename[:-len(IMAGE_EXTENSION)] + '.txt' |
|
if not os.path.exists('./data/texts/' + text_filename): |
|
continue |
|
|
|
|
|
with open('./data/texts/' + text_filename, 'r') as f: |
|
description = f.read() |
|
|
|
|
|
shutil.copyfile('./data/images/' + filename, './temp/' + description.replace(' ', '_') + IMAGE_EXTENSION) |
|
|
|
|
|
with tarfile.open( "./images.tar.gz", "w:gz") as tar: |
|
for filename in os.listdir('./temp'): |
|
tar.add('./temp/' + filename, arcname=filename) |
|
|