Hướng dẫn how do i loop an image in a directory in python? - làm cách nào để lặp một hình ảnh trong một thư mục trong python?

Cải thiện bài viết

Lưu bài viết

  • Đọc
  • Bàn luận
  • Cải thiện bài viết

    Lưu bài viết

    Đọc

    Bàn luậnUsing os.listdir

    Trong bài viết này, chúng ta sẽ học cách lặp lại thông qua các hình ảnh trong một thư mục trong Python. & NBSP;

    • Phương pháp 1: Sử dụng Os.ListDiros module to interact with the operating system.
    • Ví dụ 1: Chỉ lặp qua .png chỉlistdir[] function from os to get access to the folders given in quotes.
    • Lúc đầu, chúng tôi đã nhập mô -đun HĐH để tương tác với hệ điều hành.os.listdir[] function, we iterate through the images and printed the names in order.
    • Sau đó, chúng tôi nhập chức năng listDir [] từ HĐH để có quyền truy cập vào các thư mục được đưa ra trong báo giá..png files to be loaded using the endswith[] function.

    Python3

    Sau đó, với sự trợ giúp của hàm Os.ListDir [], chúng tôi lặp lại thông qua các hình ảnh và in các tên theo thứ tự.

    Ở đây chúng tôi chỉ đề cập đến & nbsp; .png sẽ được tải bằng hàm endswith [].

    import os

    from os import listdir

    folder_dir =

    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[]
    
    0

    import0import1import2

    Output::

    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[]
    
    1
    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[]
    
    2
    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[]
    
    3
    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[]
    
    4

    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[]
    
    5
    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[]
    
    6
    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[]
    
    7
    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[]
    
    8
    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[]
    
    9.png, .jpg, .jpeg files to be loaded using the endswith[] function.

    Python3

    Sau đó, với sự trợ giúp của hàm Os.ListDir [], chúng tôi lặp lại thông qua các hình ảnh và in các tên theo thứ tự.

    Ở đây chúng tôi chỉ đề cập đến & nbsp; .png sẽ được tải bằng hàm endswith [].

    import os

    from os import listdir

    folder_dir =

    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[]
    
    0

    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[]
    
    1
    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[]
    
    2
    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[]
    
    3
    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[]
    
    4

    import0import1import2

    Output:

    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[]
    
    5
    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[]
    
    6
    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[]
    
    7
    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[]
    
    8
    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[]
    
    9
    pathlib module

    • Ví dụ 2: lặp qua tất cả các loại hình ảnhpathlib module from Path.
    • Ở đây chúng tôi đã đề cập đến .png, .jpg, .jpeg các tệp được tải bằng hàm endswith [].Path[] function and used it .glob[‘*.png’] function to iterate through all the images present in this folder.

    Python3

    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[]
    
    5
    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[]
    
    6
    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[]
    
    7
    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[]
    
    8from0from1 from2from3from4

    import0from1 from2from8

    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[]
    
    9

    Phương pháp 2: Sử dụng & NBSP; Mô -đun Pathlib

    Lúc đầu, chúng tôi đã nhập mô -đun PATHLIB từ đường dẫn.

    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[]
    
    5import1listdir1

    Output:

    Sau đó, chúng tôi vượt qua hàm thư mục/thư mục bên trong đường dẫn [] và sử dụng chức năng .glob [‘*. PNG,] để lặp qua tất cả các hình ảnh có trong thư mục này. glob.iglob[]

    • from os 4import os 6glob module.
    • folder_dir = os 9glob.iglob[] function we iterate through the images and print the names in order.
    • 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[]
      
      2= import2import3import4.png files to be loaded using the endswith[] function.

    Python3

    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[]
    
    1 import6
    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[]
    
    3 import8

    import0from1 from2from8

    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[]
    
    9

    Phương pháp 2: Sử dụng & NBSP; Mô -đun Pathlib

    folder_dir =

    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[]
    
    0

    import0import1import2

    Output::


    Tôi đang sử dụng Python, Open, Numpy và Scipy. Tôi có một thư mục hình ảnh mà tôi muốn xoay theo một số góc nhất định. Tôi muốn tập lệnh này. Tôi đang sử dụng điều này, OpenCV Python xoay hình ảnh theo độ x xung quanh điểm cụ thể nhưng dường như nó không chính xác như tôi đã hình dung. Tôi nhận được một kế hoạch xoay vòng không hợp lệ được chỉ định, nhưng tôi không nghĩ rằng tôi nên nhận được điều này.

    Đây là mã của tôi trông như thế nào:

    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[]
    

    Đã hỏi ngày 6 tháng 2 năm 2017 lúc 18:00Feb 6, 2017 at 18:00

    Teodorico Levoffteodorico LevoffTeodorico Levoff

    1.5492 Huy hiệu vàng24 Huy hiệu bạc42 Huy hiệu đồng2 gold badges24 silver badges42 bronze badges

    Bạn cần thực sự đọc tệp hình ảnh trước khi xoay chúng. Những gì mã hiện tại của bạn đang làm chỉ là lặp lại thông qua tên của các tệp [và thư mục].

    Os.ListDir [đường dẫn] cung cấp cho bạn một danh sách các nội dung của thư mục [về cơ bản chỉ là tên] và sau đó bạn cần mở các tệp này bằng hàm ndimage.Imread [].

    Điều này sẽ hoạt động:

    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[]
    

    Tái bút: Cách lặp này thông qua nội dung của thư mục chỉ hoạt động nếu chỉ có các tệp trong thư mục và không có thư mục phụ. Os.ListDir [đường dẫn] sẽ trả về tên của bất kỳ tệp nào cũng như các thư mục con.

    Bạn có thể tìm hiểu làm thế nào để chỉ liệt kê các tệp trong một thư mục từ bài đăng này: Cách liệt kê tất cả các tệp của một thư mục?

    Đã trả lời ngày 6 tháng 2 năm 2017 lúc 18:40Feb 6, 2017 at 18:40

    Bài Viết Liên Quan

    Chủ Đề