# 方法一import cv2
import os
defread_image(directory_name):
i =0#计数器for filename in os.listdir(directory_name):
img = cv2.imread(directory_name +"/"+ filename)
i +=1if i==2:#显示第2张图片
cv2.imshow('img6',img)
cv2.waitKey(100)break
read_image("C:\\Users\\XX\\Downloads\\roses")
# 方法二import cv2
import os
path=("C:\\Users\\XX\\Downloads\\roses")for root,dirs,files in os.walk(path):
i =0for f in files:
i +=1if i==64:
img = cv2.imread(os.path.join(root,f))
cv2.namedWindow('img')
cv2.imshow('img',img)
cv2.waitKey(0)
cv2.destroyAllWindows()breakif i==64:break
# 方法三import cv2
import os
path ="C:\\Users\\XX\\Downloads\\roses"for root,dirs,files in os.walk(path):
i =0for f in files:
i +=1if i==64:
img = cv2.imread(os.path.join(root,f))
cv2.namedWindow('img')
cv2.imshow('img',img)
cv2.waitKey(1000)# cv2.destroyAllWindows()breakif i==64:break
# 方法四import os
from PIL import Image
img_path ='C:\\Users\\XX\\Downloads\\roses\\'
img_list = os.listdir(r'C:\\Users\\XX\\Downloads\\roses')
img = Image.open(img_path + img_list[67])
img.show()