Hướng dẫn get image in folder python - lấy hình ảnh trong thư mục python

Tôi đang cố gắng nhập tất cả các hình ảnh bên trong một thư mục [vị trí thư mục được biết đến].

path = /home/user/mydirectory

Tôi đã biết một cách để tìm ra độ dài của thư mục.

Điều tôi không chắc chắn là làm thế nào tôi có thể nhập hình ảnh [sử dụng PIL/Pillow] vào danh sách hoặc từ điển, vì vậy chúng có thể được thao tác đúng cách.

Dan

1.1643 huy hiệu vàng13 Huy hiệu bạc34 Huy hiệu đồng3 gold badges13 silver badges34 bronze badges

Hỏi ngày 15 tháng 10 năm 2014 lúc 21:23Oct 15, 2014 at 21:23

5

Tôi sẽ bắt đầu bằng cách sử dụng Glob:

from PIL import Image
import glob
image_list = []
for filename in glob.glob['yourpath/*.gif']: #assuming gif
    im=Image.open[filename]
    image_list.append[im]

Sau đó làm những gì bạn cần làm với danh sách hình ảnh của bạn [Image_List].

Đã trả lời ngày 15 tháng 10 năm 2014 lúc 21:33Oct 15, 2014 at 21:33

user1269942user1269942user1269942

3.69422 Huy hiệu bạc33 Huy hiệu đồng22 silver badges33 bronze badges

8

from PIL import Image
import os, os.path

imgs = []
path = "/home/tony/pictures"
valid_images = [".jpg",".gif",".png",".tga"]
for f in os.listdir[path]:
    ext = os.path.splitext[f][1]
    if ext.lower[] not in valid_images:
        continue
    imgs.append[Image.open[os.path.join[path,f]]]
   

Dan

1.1643 huy hiệu vàng13 Huy hiệu bạc34 Huy hiệu đồng3 gold badges13 silver badges34 bronze badges

Đã trả lời ngày 15 tháng 10 năm 2014 lúc 21:33Oct 15, 2014 at 21:33

3.69422 Huy hiệu bạc33 Huy hiệu đồngTony Suffolk 66

Tony Suffolk 66tony Suffolk 663 gold badges29 silver badges33 bronze badges

4

Làm cách nào để đọc một thư mục trong Python?

Nhập mô -đun ..

Thêm đường dẫn của thư mục ..

  • Thay đổi thư mục ..
  • Nhận danh sách một tệp từ một thư mục ..
  • Làm cách nào để đọc một thư mục trong Python?

    Nhập mô -đun ..

    Thêm đường dẫn của thư mục ..

    Thay đổi thư mục ..

    Nhận danh sách một tệp từ một thư mục ..Using os.listdir

    Lặp lại thông qua danh sách tệp và kiểm tra xem phần mở rộng của tệp có ở hay không. Định dạng TXT hay không ..

    • Nếu tệp văn bản tồn tại, hãy đọc tệp bằng cách xử lý tệp ..os module to interact with the operating system.
    • Xem thảo luậnlistdir[] function from os to get access to the folders given in quotes.
    • Cải thiện bài viếtos.listdir[] function, we iterate through the images and printed the names in order.
    • Lưu bài viết.png files to be loaded using the endswith[] function.

    Python3

    Đọc

    Bàn luận

    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.ListDir

    from PIL import Image
    import glob
    image_list = []
    for filename in glob.glob['yourpath/*.gif']: #assuming gif
        im=Image.open[filename]
        image_list.append[im]
    
    3 from8
    from PIL import Image
    import glob
    image_list = []
    for filename in glob.glob['yourpath/*.gif']: #assuming gif
        im=Image.open[filename]
        image_list.append[im]
    
    5 os 0

    from PIL import Image
    import os, os.path
    
    imgs = []
    path = "/home/tony/pictures"
    valid_images = [".jpg",".gif",".png",".tga"]
    for f in os.listdir[path]:
        ext = os.path.splitext[f][1]
        if ext.lower[] not in valid_images:
            continue
        imgs.append[Image.open[os.path.join[path,f]]]
       
    
    2
    from PIL import Image
    import os, os.path
    
    imgs = []
    path = "/home/tony/pictures"
    valid_images = [".jpg",".gif",".png",".tga"]
    for f in os.listdir[path]:
        ext = os.path.splitext[f][1]
        if ext.lower[] not in valid_images:
            continue
        imgs.append[Image.open[os.path.join[path,f]]]
       
    
    3
    from PIL import Image
    import os, os.path
    
    imgs = []
    path = "/home/tony/pictures"
    valid_images = [".jpg",".gif",".png",".tga"]
    for f in os.listdir[path]:
        ext = os.path.splitext[f][1]
        if ext.lower[] not in valid_images:
            continue
        imgs.append[Image.open[os.path.join[path,f]]]
       
    
    4

    Output::

    Phương pháp 3: Sử dụngglob.iglob []

    Lúc đầu, chúng tôi đã nhập mô -đun GLOB..png, .jpg, .jpeg files to be loaded using the endswith[] function.

    Python3

    Đọc

    Bàn luận

    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.ListDir

    Ví dụ 1: Chỉ lặp qua .png chỉ

    Lúc đầu, chúng tôi đã nhập mô -đun HĐH để tương tác với hệ điều hành.

    from PIL import Image
    import os, os.path
    
    imgs = []
    path = "/home/tony/pictures"
    valid_images = [".jpg",".gif",".png",".tga"]
    for f in os.listdir[path]:
        ext = os.path.splitext[f][1]
        if ext.lower[] not in valid_images:
            continue
        imgs.append[Image.open[os.path.join[path,f]]]
       
    
    2
    from PIL import Image
    import os, os.path
    
    imgs = []
    path = "/home/tony/pictures"
    valid_images = [".jpg",".gif",".png",".tga"]
    for f in os.listdir[path]:
        ext = os.path.splitext[f][1]
        if ext.lower[] not in valid_images:
            continue
        imgs.append[Image.open[os.path.join[path,f]]]
       
    
    3
    from PIL import Image
    import os, os.path
    
    imgs = []
    path = "/home/tony/pictures"
    valid_images = [".jpg",".gif",".png",".tga"]
    for f in os.listdir[path]:
        ext = os.path.splitext[f][1]
        if ext.lower[] not in valid_images:
            continue
        imgs.append[Image.open[os.path.join[path,f]]]
       
    
    4

    Output:

    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á.pathlib module

    • 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ự.pathlib module from Path.
    • Ở đây chúng tôi chỉ đề cập đến & nbsp; .png sẽ đượ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 os6import os8

    from PIL import Image
    import glob
    image_list = []
    for filename in glob.glob['yourpath/*.gif']: #assuming gif
        im=Image.open[filename]
        image_list.append[im]
    
    0____11 from1

    from PIL import Image
    import glob
    image_list = []
    for filename in glob.glob['yourpath/*.gif']: #assuming gif
        im=Image.open[filename]
        image_list.append[im]
    
    4
    from PIL import Image
    import glob
    image_list = []
    for filename in glob.glob['yourpath/*.gif']: #assuming gif
        im=Image.open[filename]
        image_list.append[im]
    
    1 from4from5from6

    from PIL import Image
    import glob
    image_list = []
    for filename in glob.glob['yourpath/*.gif']: #assuming gif
        im=Image.open[filename]
        image_list.append[im]
    
    3 from8
    from PIL import Image
    import glob
    image_list = []
    for filename in glob.glob['yourpath/*.gif']: #assuming gif
        im=Image.open[filename]
        image_list.append[im]
    
    5 os 0

    from PIL import Image
    import glob
    image_list = []
    for filename in glob.glob['yourpath/*.gif']: #assuming gif
        im=Image.open[filename]
        image_list.append[im]
    
    7
    from PIL import Image
    import os, os.path
    
    imgs = []
    path = "/home/tony/pictures"
    valid_images = [".jpg",".gif",".png",".tga"]
    for f in os.listdir[path]:
        ext = os.path.splitext[f][1]
        if ext.lower[] not in valid_images:
            continue
        imgs.append[Image.open[os.path.join[path,f]]]
       
    
    3os 3

    Output:

    Phương pháp 3: Sử dụngglob.iglob [] glob.iglob[]

    • Lúc đầu, chúng tôi đã nhập mô -đun GLOB.glob module.
    • Sau đó, với sự trợ giúp của hàm glob.iglob [], chúng tôi lặp lại thông qua các hình ảnh và in tên theo thứ tự.glob.iglob[] function we iterate through the images and print the names in order.
    • Ở đây chúng tôi đã đề cập đến các tệp .png sẽ được tải bằng hàm endswith []..png files to be loaded using the endswith[] function.

    Python3

    import os 5

    from PIL import Image
    import glob
    image_list = []
    for filename in glob.glob['yourpath/*.gif']: #assuming gif
        im=Image.open[filename]
        image_list.append[im]
    
    0____11 from1

    from PIL import Image
    import glob
    image_list = []
    for filename in glob.glob['yourpath/*.gif']: #assuming gif
        im=Image.open[filename]
        image_list.append[im]
    
    4
    from PIL import Image
    import glob
    image_list = []
    for filename in glob.glob['yourpath/*.gif']: #assuming gif
        im=Image.open[filename]
        image_list.append[im]
    
    1 from4from5from6

    from PIL import Image
    import glob
    image_list = []
    for filename in glob.glob['yourpath/*.gif']: #assuming gif
        im=Image.open[filename]
        image_list.append[im]
    
    3 from8
    from PIL import Image
    import glob
    image_list = []
    for filename in glob.glob['yourpath/*.gif']: #assuming gif
        im=Image.open[filename]
        image_list.append[im]
    
    5 os 0

    from PIL import Image
    import os, os.path
    
    imgs = []
    path = "/home/tony/pictures"
    valid_images = [".jpg",".gif",".png",".tga"]
    for f in os.listdir[path]:
        ext = os.path.splitext[f][1]
        if ext.lower[] not in valid_images:
            continue
        imgs.append[Image.open[os.path.join[path,f]]]
       
    
    2
    from PIL import Image
    import os, os.path
    
    imgs = []
    path = "/home/tony/pictures"
    valid_images = [".jpg",".gif",".png",".tga"]
    for f in os.listdir[path]:
        ext = os.path.splitext[f][1]
        if ext.lower[] not in valid_images:
            continue
        imgs.append[Image.open[os.path.join[path,f]]]
       
    
    3
    from PIL import Image
    import os, os.path
    
    imgs = []
    path = "/home/tony/pictures"
    valid_images = [".jpg",".gif",".png",".tga"]
    for f in os.listdir[path]:
        ext = os.path.splitext[f][1]
        if ext.lower[] not in valid_images:
            continue
        imgs.append[Image.open[os.path.join[path,f]]]
       
    
    4

    Output::


    Làm thế nào để tôi có được tất cả các hình ảnh trong một thư mục trong Python?

    Lúc đầu, chúng tôi đã nhập mô -đun PATHLIB từ đường dẫn. Sau đó, chúng tôi chuyển chức năng thư mục/thư mục bên trong [] 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.imported the pathlib module from Path. Then we pass the directory/folder inside Path[] function and used it . glob['*. png'] function to iterate through all the images present in this folder.

    Làm cách nào để đọc nhiều hình ảnh trong một thư mục trong Python?

    Đọc và viết nhiều hình ảnh từ một thư mục trong Python [Skimage, .....
    Skimage: Từ Nhập mục Ioimg = io.Imread [....
    OpenCV: Nhập CV2# Để đọc hình ảnh là colorcv2_img = cv2.ImRead [....
    Nhập khẩu pydicom pydicom dưới dạng DICOM.....
    Skimage từ SkiMage Nhập ioio.imsave ["write_brain_image.png", img].

    Làm thế nào để Jupyter Notebook đọc hình ảnh từ một thư mục?

    Để đọc hình ảnh bằng OpenCV, tôi đã xác định chức năng Load_IMAGES_FROM_FOLDER, có một đường dẫn mà hình ảnh được lưu trữ dưới dạng tham số đầu vào, trong bước tiếp theo CV2.Imread Function Đọc tất cả các tệp trong một thư mục và nối chúng vào hình ảnh = [] Danh sách sau đó trả về danh sách hình ảnh.

    Làm cách nào để đọc một thư mục trong Python?

    Approach:..
    Nhập mô -đun ..
    Thêm đường dẫn của thư mục ..
    Thay đổi thư mục ..
    Nhận danh sách một tệp từ một thư mục ..
    Lặp lại thông qua danh sách tệp và kiểm tra xem phần mở rộng của tệp có ở hay không.Định dạng TXT hay không ..
    Nếu tệp văn bản tồn tại, hãy đọc tệp bằng cách xử lý tệp ..

    Bài Viết Liên Quan

    Chủ Đề