Hướng dẫn read image name from folder python - đọc tên hình ảnh từ thư mục python

Tôi muốn đọc tên hình ảnh như hoa.png không tên.png. Có cách nào mà tôi có thể đọc tất cả các hình ảnh từng hình ảnh giống như chúng ta có thể đọc tệp văn bản với Open ("data.txt", "r") dưới dạng myfile: data = myfile.read (). Splitlines () .. hy vọng Bạn sẽ cho tôi một giải pháp cảm ơn

enter code here from PIL import Image
 import glob
 image_list = []
       for filename in glob.glob('yourpath/*.img'):
       im=Image.open(filename)
       image_list.append(im)

hỏi ngày 1 tháng 7 lúc 14:47Jul 1 at 14:47

Hướng dẫn read image name from folder python - đọc tên hình ảnh từ thư mục python

2

Nếu bạn muốn lấy tất cả tên tệp từ một thư mục, bạn có thể sử dụng os.listdir

import os
filename = os.listdir("YOUR_FOLDER_NAME")

Đã trả lời ngày 3 tháng 7 lúc 23:37Jul 3 at 23:37

Hướng dẫn read image name from folder python - đọc tên hình ảnh từ thư mục python

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

Lưu bài viết

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.ListDirUsing 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.os module to interact with the operating system.
  • 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á.listdir() function from os to get access to the folders given in quotes.
  • 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ự.os.listdir() function, we iterate through the images and printed the names in order.
  • Ở đây chúng tôi chỉ đề cập đến & nbsp; .png sẽ được tải bằng hàm endswith ()..png files to be loaded using the endswith() function.

Python3

import os

from os import listdir

folder_dir ____10

import os
filename = os.listdir("YOUR_FOLDER_NAME")
1

import os
filename = os.listdir("YOUR_FOLDER_NAME")
2
import os
filename = os.listdir("YOUR_FOLDER_NAME")
3
import os
filename = os.listdir("YOUR_FOLDER_NAME")
4
import os
filename = os.listdir("YOUR_FOLDER_NAME")
5

import os
filename = os.listdir("YOUR_FOLDER_NAME")
6
import os
filename = os.listdir("YOUR_FOLDER_NAME")
7
import os
filename = os.listdir("YOUR_FOLDER_NAME")
8
import os
filename = os.listdir("YOUR_FOLDER_NAME")
9os.listdir0

os.listdir1os.listdir2os.listdir3

Output::

Hướng dẫn read image name from folder python - đọc tên hình ảnh từ thư mục python

Ví dụ 2: lặp qua tất cả các loại hình ảnh

Ở đây chúng tôi đã đề cập đến .png, .jpg, .jpeg các tệp được tải bằng hàm endswith ()..png, .jpg, .jpeg files to be loaded using the endswith() function.

Python3

import os

from os import listdir

folder_dir ____10

import os
filename = os.listdir("YOUR_FOLDER_NAME")
1

import os
filename = os.listdir("YOUR_FOLDER_NAME")
2
import os
filename = os.listdir("YOUR_FOLDER_NAME")
3
import os
filename = os.listdir("YOUR_FOLDER_NAME")
4
import os
filename = os.listdir("YOUR_FOLDER_NAME")
5

Ví dụ 2: lặp qua tất cả các loại hình ảnh

Ở đây chúng tôi đã đề cập đến .png, .jpg, .jpeg các tệp được tải bằng hàm endswith ().

import os
filename = os.listdir("YOUR_FOLDER_NAME")
6
import os
filename = os.listdir("YOUR_FOLDER_NAME")
7
import os
filename = os.listdir("YOUR_FOLDER_NAME")
8
import os
filename = os.listdir("YOUR_FOLDER_NAME")
9os1os2 os3os44____45

Output:

Hướng dẫn read image name from folder python - đọc tên hình ảnh từ thư mục python

os.listdir1os2 os3os9os.listdir0pathlib module

  • os.listdir1os.listdir223pathlib module from Path.
  • Phương pháp 2: Sử dụng & NBSP; Mô -đun PathlibPath() function and used it .glob(‘*.png’) function to iterate through all the images present in this folder.

Python3

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

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.

from from5import from7

folder_dir ____10 os 0

import os
filename = os.listdir("YOUR_FOLDER_NAME")
6os.listdir2import2

Output:

Hướng dẫn read image name from folder python - đọc tên hình ảnh từ thư mục python

import os filename = os.listdir("YOUR_FOLDER_NAME") 3import os filename = os.listdir("YOUR_FOLDER_NAME") 0 os 3os 44 glob.iglob()

  • import os
    filename = os.listdir("YOUR_FOLDER_NAME")
    
    2 os 7
    import os
    filename = os.listdir("YOUR_FOLDER_NAME")
    
    4 os 9glob module.
  • Phương pháp 3: Sử dụngglob.iglob ()glob.iglob() function we iterate through the images and print the names in order.
  • Lúc đầu, chúng tôi đã nhập mô -đun GLOB..png files to be loaded using the endswith() function.

Python3

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

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.

from from5import from7

import os
filename = os.listdir("YOUR_FOLDER_NAME")
6
import os
filename = os.listdir("YOUR_FOLDER_NAME")
7
import os
filename = os.listdir("YOUR_FOLDER_NAME")
8
import os
filename = os.listdir("YOUR_FOLDER_NAME")
9os.listdir0

os.listdir1os.listdir2os.listdir3

Output::

Hướng dẫn read image name from folder python - đọc tên hình ảnh từ thư mục python