Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Need help reading image files in tar file using python

I have a tar file which has number of folders within it. In each folder there are number of image files. I need to write a python script which will read each image file and perform some action on the image (ex: thresholding etc.,) and save the image file in the directory I specified. This process need to be done without un-tarring the tar file.

t = tarfile.open('example.tar', 'r')
for member in t.getmembers():
    f = t.extractfile(member)

While I am trying to print f, it's returning None type. What am I doing wrong?

like image 659
Harathi Avatar asked Nov 08 '22 09:11

Harathi


1 Answers

Simply use this function.

import tarfile

#simple function to extract the train data
#tar_file : the path to the .tar file
#path : the path where it will be extracted
def extract(tar_file, path):
    opened_tar = tarfile.open(tar_file)

    if tarfile.is_tarfile(tar_file):
        opened_tar.extractall(path)
    else:
        print("The tar file you entered is not a tar file")
like image 194
Khubaib Raza Avatar answered Nov 15 '22 04:11

Khubaib Raza