Hướng dẫn list image in folder python - liệt kê hình ảnh trong thư mục python

Tôi có một loạt các hình ảnh trong Python3 như thế này ...

images = [
    "/var/www/html/myfolder/images/1.jpg",
    "/var/www/html/myfolder/images/441.jpg",
    "/var/www/html/myfolder/images/15.jpg",
    "/var/www/html/myfolder/images/78.jpg",
]

Thay vì chỉ định hình ảnh như thế này, tôi muốn truyền một đường dẫn tuyệt đối và có Python tạo cho tôi danh sách hình ảnh trong số các hình ảnh .jpg có trong đường dẫn đó.

Cách tiếp cận tốt nhất của tôi là gì?

Hỏi ngày 11 tháng 8 năm 2019 lúc 15:13Aug 11, 2019 at 15:13

fightstarr20fightstarr20fightstarr20

11K34 Huy hiệu vàng145 Huy hiệu bạc258 Huy hiệu Đồng34 gold badges145 silver badges258 bronze badges

2

Bạn có thể sử dụng Quả cầu.

If recursive is true, the pattern “**” will match any files and zero or more directories and subdirectories. If the pattern is
1

Trả về một danh sách có thể phát ra tên đường dẫn khớp với tên đường dẫn, phải là một chuỗi chứa đặc tả đường dẫn. Tên pathname có thể là tuyệt đối [như /usr/src/python-1.5/makefile] hoặc quan hệ [như ../../tools//.gif] và có thể chứa các ký tự đại diện theo kiểu vỏ. Các liên kết symlink bị hỏng được bao gồm trong các kết quả [như trong vỏ].

If recursive is true, the pattern “**” will match any files and zero or more directories and subdirectories. If the pattern is

tiếp theo là OS.SEP, chỉ các thư mục và thư mục con phù hợp.

Giả sử con đường cơ bụng của bạn là

If recursive is true, the pattern “**” will match any files and zero or more directories and subdirectories. If the pattern is
2

import glob
images = glob.glob['images/*.jpg']

//docs.python.org/3/library/glob.html#glob.glob

Đã trả lời ngày 11 tháng 8 năm 2019 lúc 15:18Aug 11, 2019 at 15:18

Mô -đun

If recursive is true, the pattern “**” will match any files and zero or more directories and subdirectories. If the pattern is
3 trong Python 3 làm cho điều này dễ dàng:

from pathlib import Path
images = Path["/var/www/html/myfolder/images"].glob["*.jpg"]

Bạn muốn tất cả các hình ảnh JPG đệ quy theo thư mục đó thay thế? Sử dụng

If recursive is true, the pattern “**” will match any files and zero or more directories and subdirectories. If the pattern is
4.

Lưu ý rằng điều này đang tạo ra một mảng các đối tượng đường dẫn. Nếu bạn muốn chuỗi, chỉ cần chuyển đổi chúng:

image_strings = [str[p] for p in images]

Đã trả lời ngày 11 tháng 8 năm 2019 lúc 15:22Aug 11, 2019 at 15:22

mk12mk12mk12

25.5K31 Huy hiệu vàng96 Huy hiệu bạc136 Huy hiệu Đồng31 gold badges96 silver badges136 bronze badges

1

Nếu bạn chỉ định đường dẫn, có một số cách để tìm tất cả các tệp trong thư mục đó. Khi bạn có danh sách đó, bạn có thể chỉ cần lặp lại thông qua nó và tạo hình ảnh.

Xem: Làm cách nào để liệt kê tất cả các tệp của một thư mục?

Một cách tốt để làm điều đó là sử dụng os.listdir:os.listdir:

import os

# specify the img directory path
path = "path/to/img/folder/"

# list files in img directory
files = os.listdir[path]

for file in files:
    # make sure file is an image
    if file.endswith[['.jpg', '.png', 'jpeg']]:
        img_path = path + file

        # load file as image...

Đã trả lời ngày 11 tháng 8 năm 2019 lúc 15:27Aug 11, 2019 at 15:27

GrobbedgrobbedGrobbed

1871 Huy hiệu bạc11 Huy hiệu đồng1 silver badge11 bronze badges

1

Chỉ để quét cấp cao nhất

import os
path = "path/to/img/folder/"
jpgs = [os.path.join[path, file]
        for file in os.listdir[path]
        if file.endswith['.jpg']]

Để quét đệ quy, hãy thay thế dòng cuối cùng bằng

jpgs = [os.path.join[root, file]
        for root, dirs, files in os.walk[path]
        for file in files
        if file.endswith['.jpg']]

Đã trả lời ngày 11 tháng 8 năm 2019 lúc 15:41Aug 11, 2019 at 15:41

import os

images=[]

def getFiles[path]:
    for file in os.listdir[path]:
        if file.endswith[".jpg"]:
            images.append[os.path.join[path, file]]

Danh sách hình ảnh:

filesPath = "/var/www/html/myfolder/images"

getFiles[filesPath]
print[images]

Đã trả lời ngày 11 tháng 8 năm 2019 lúc 15:24Aug 11, 2019 at 15:24

HimanshuhimanshuHimanshu

6461 Huy hiệu vàng8 Huy hiệu bạc18 Huy hiệu đồng1 gold badge8 silver badges18 bronze badges

  • Phương pháp hiện đại là sử dụng pathlib coi các đường dẫn là đối tượng chứ không phải chuỗi. Là một đối tượng, tất cả các đường dẫn sau đó có các phương thức để truy cập các thành phần khác nhau của đường dẫn, [ví dụ:
    If recursive is true, the pattern “**” will match any files and zero or more directories and subdirectories. If the pattern is
    
    5,
    If recursive is true, the pattern “**” will match any files and zero or more directories and subdirectories. If the pattern is
    
    6].
  • If recursive is true, the pattern “**” will match any files and zero or more directories and subdirectories. If the pattern is
    
    3 cũng có:
    • If recursive is true, the pattern “**” will match any files and zero or more directories and subdirectories. If the pattern is
      
      8 TIÊU CHUẨN
    • Phương thức
      If recursive is true, the pattern “**” will match any files and zero or more directories and subdirectories. If the pattern is
      
      9 [ví dụ:
      import glob
      images = glob.glob['images/*.jpg']
      
      0]
  • Mô -đun Pathlib của Python 3: Taming hệ thống tệp

Code:

If recursive is true, the pattern “**” will match any files and zero or more directories and subdirectories. If the pattern is
0

Đã trả lời ngày 11 tháng 8 năm 2019 lúc 15:29Aug 11, 2019 at 15:29

Trenton McKinneyTrenton McKinneyTrenton McKinney

49.4K31 Huy hiệu vàng120 Huy hiệu bạc133 Huy hiệu đồng31 gold badges120 silver badges133 bronze badges

Bài Viết Liên Quan

Chủ Đề