import os
import CV2
import matlplotlib.pyplot as plt
from PIL import Image
def classify_image(img_path):
if"B" in img_path:
return "Before"
elif "A" in img_path:
return "After"
else:
return "unknow"
img_folder=''
img_list=os.listdir(img_folder)
Before_images=[]
Before_dic={}
for i in range(len(Before_images)):
Before_dic[i]=Before_images=[i]
After_images=[]
After_dic={}
for i in range(len(After_images)):
After_dic[i]=After_images=[i]
unknow_images=[]
for img_name in img_list:
img_path=os.pat.join(img_folder,img_name)
img_class=classify_image(img_path)
if img_class == "Before":
Before_images.append(img_name)
elif img_class == "After":
After_images.append(img_name)
else:
img_class =="unknow"
unknow_images.append(img_name)
Before_images=sorted(Before_images,key=itemgetter(46,47))
After_images=sorted(After_images,key=itemgetter(46,47))
#利用檔名的第47碼排序
print("Before_images")
print(Before_images)
print("Afterimages")
print(After_images)
for i,img_path in enumerate(all_imgs):
if i ==0 or int(img_path[46,48]!=int(all_imgs[i-1][46:48])):
print('/n')
print(img_path,end='')
##輸出格式對,要轉圖片
以上是我的程式碼,按照檔名分類後排序,他現在輸出是圖片的檔案名(00000111120230617_100001000_11110000010000000011000_A_.tif)
(00000111120230617_100001000_11110000010000000012000_A_.tif)
我輸出只想顯示圖片,該怎麼改?
我卡好久了,我試過plt.show()
fig.axs=plt.subplots
image_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
但是都不行QQ
你的程式碼格式這樣沒有辦法看
看不太出來你是哪裡要show image;
你有把圖片讀出來嗎? 應為不知道你的程式碼裡面做了甚麼?
Opencv 可以直接顯示圖片
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
# 顯示圖片
cv2.imshow('My Image', img)
# 按下任意鍵則關閉所有視窗
cv2.waitKey(0)
cv2.destroyAllWindows()