2017-02-06 13 views
0

私はPython、Open、Numpy、およびScipyを使用しています。私はある角度で回転したい画像のディレクトリを持っています。私はこれを脚本化したい。私はこれを使用しています、OpenCV Python rotate image by X degrees around specific pointしかし、それはまさに私が構想したようにパイプラインには見えません。無効なローテーションプランが指定されていますが、これを取得する必要はないと思います。あなたが実際にそれらを回転させる前の画像ファイルを読み込む必要がありイメージのディレクトリをループし、x度回転してディレクトリに保存します。

from scipy import ndimage 
import numpy as np 
import os 
import cv2 

def main(): 
    outPath = "C:\Miniconda\envs\.." 
    path = "C:\Miniconda\envs\out\.." 
    for image_to_rotate in os.listdir(path): 
     rotated = ndimage.rotate(image_to_rotate, 45) 
     fullpath = os.path.join(outPath, rotated) 

    if __name__ == '__main__': 
    main() 

答えて

5

:ここ

は私のコードは次のようになります。あなたの現在のコードがやっていることは、ファイル(とディレクトリ)の名前を繰り返すことです。

os.listdir(パス)はあなたのフォルダ(基本的には名前だけ)の内容のリストを与えると、あなたはndimage.imread()機能を使用してこれらのファイルを開く必要があります。

これは動作するはずです:

from scipy import ndimage, misc 
import numpy as np 
import os 
import cv2 

def main(): 
    outPath = "C:\Miniconda\envs\.." 
    path = "C:\Miniconda\envs\out\.." 

    # iterate through the names of contents of the folder 
    for image_path in os.listdir(path): 

     # create the full input path and read the file 
     input_path = os.path.join(path, image_path) 
     image_to_rotate = ndimage.imread(input_path) 

     # rotate the image 
     rotated = ndimage.rotate(image_to_rotate, 45) 

     # create full output path, 'example.jpg' 
     # becomes 'rotate_example.jpg', save the file to disk 
     fullpath = os.path.join(outPath, 'rotated_'+image_path) 
     misc.imsave(fullpath, rotated) 

if __name__ == '__main__': 
    main() 

をPS:ディレクトリ内のファイルなしサブディレクトリのみが存在する場合、フォルダの内容を反復処理するこの方法はのみ動作します。 os.listdir(パス)は、すべてのファイルの名前とサブディレクトリを返します。

この投稿のディレクトリにあるファイルのみをリストする方法を知ることができます。How to list all files of a directory?

関連する問題