2017-06-28 10 views
0

kaggleに7zファイルをインストールしました。 しかし、python3で7zファイルを使う方法はわかりません。 私は7zファイルを解凍し、img(7z)をnumpyの配列に変更したいと思います。7zファイルの解凍を行い、img(7z)をnumpyの配列に変更したい

私を助けてください。

https://www.kaggle.com/rhammell/planesnet

+2

[7zファイルの内容をPythonで読み取る方法](https://stackoverflow.com/questions/32797851/how-to-read-contents-of-7z-file-using-python) – Steve

答えて

0

これを試してみてください。

import subprocess 
import os 
import cv2 # pip install cv2 

def get_images(path_to_7z_file): 
    # in windows would be something like C:\Program Files\7-Zip\7z.exe 
    # for linux /usr/local/bin/7z. check it using "which" command 
    exe_7z = r'path/to/7z/executable' 
    # x is for extracting, you can use other 7zip flags, check "7z --help" command 
    subprocess_args = ('%s x %s' % (exe_7z, s7z_file)).split(' ') 
    process_7z = subprocess.Popen(subprocess_args) 
    ret_code = process_7z.wait() 
    if ret_code != 0: 
     # maybe the system could not find the 7z executable 
     # or the 7z file could not be opened 
     print 'error in 7z command' 
    else: 
     img_files = [ 
      f for f in os.listdir('.') 
      if (os.path.isfile(f) and 
       f.split('.')[1].lower() in ['png', 'jpg']) # other extensions 
     ] 
     # img_files holds the list of images within the 7z archive 
     for img_file in img_files: 
      img = cv2.imread(img_file) 
      # at this point, img is a numpy.array 

はあなたがPillowまたはcv2は、画像を扱うために使用することができます。詳細はquestionにチェックしてください。

+0

pip opencv-pythonをインストールしますか? – Kim

+0

はい。私の悪い。 :) –

関連する問題