亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何生成結果作為圖形標題以及與繪制圖像相關的文件名?

如何生成結果作為圖形標題以及與繪制圖像相關的文件名?

胡子哥哥 2024-01-04 15:22:56
我需要幫助,將測試結果和文件名放在一起作為評估圖像的標題。我可以將測試結果生成為列表(參見圖片),我可以將測試結果添加到圖像上。我可以將文件名添加到圖像頂部。但是,我不能兩者兼得。不確定我做錯了什么。謝謝。path = '/Users/minny/Desktop/A/png/file2/'ref_images = glob.iglob(path + "*.png")  all_ref_images = []  for ref_path in ref_images:      ref_head, tail = os.path.splitext(ref_path)     image1 = Image.open(ref_path)     imgA= print(calculate_brightness(image1))    #all_ref_images.append([imgA, ref_head])        ref_image = plt.imshow(image1)    fig = plt.figure()            plt.title(os.path.basename(ref_head)), plt.title(calculate_brightness(image1))            #plt.axis("off")def calculate_brightness(image):greyscale_image = image.convert('L')histogram = greyscale_image.histogram()pixels = sum(histogram)brightness = scale = len(histogram)for index in range(0, scale):    ratio = histogram[index] / pixels    brightness += ratio * (-scale + index)return 1 if brightness == 255 else brightness / scale%%capture #gives a standard size image to view inline with the text def display_img(img, cmap=None):     fig = plt.figure(figsize=(3,3))     ax = fig.add_subplot(111)     ax.imshow(img, cmap)  path = '/Users/minny/Desktop/A/png/file2/' ref_images = glob.iglob(path + "*.png")   all_ref_images = []  for ref_path in ref_images:      ref_head, tail = os.path.splitext(ref_path)     image1 = Image.open(ref_path)     imgA= print(calculate_brightness(image1))    all_ref_images.append([imgB, ref_head])                   fig = plt.figure()          ref_image = plt.imshow(image1)    print(os.path.basename(ref_head))            #plt.axis("off")    ref_image = plt.imshow(image1)    image_basename = os.path.basename(ref_head)    title = '\n'.join([image_basename, str(calculate_brightness(image1))])    plt.title(title, loc='left')    dir_name = '/Users/minny/Desktop/A/png/results/'    plt.savefig('{dir_name}/{filename}'.format(dir_name=dir_name, filename=image_basename))
查看完整描述

1 回答

?
慕標5832272

TA貢獻1966條經驗 獲得超4個贊

您可以將圖像路徑的基本名稱與calculate_brightness函數的輸出連接起來,并將結果設置為標題而不覆蓋它們:


ref_image = plt.imshow(image1)

image_basename = os.path.basename(ref_head)

test_results = '\n'.join(map(str, calculate_brightness(image1)))

title = '\n'.join([image_basename, test_results])

plt.title(title, loc='left')

UPD:如果calculate_brightness函數的結果是浮點數,你可以通過這種方式解決你的問題:


ref_image = plt.imshow(image1)

image_basename = os.path.basename(ref_head)

title = '\n'.join([image_basename, str(calculate_brightness(image1))])

plt.title(title, loc='left')

UPD2: 要將圖像保存到指定文件夾,您可以使用 plt.savefig 方法:


dir_name = '/Users/minny/Desktop/A/png/file2/some_directory' # create directory if necessary

plt.savefig('{dir_name}/{filename}'.format(dir_name=dir_name, filename=image_basename))



查看完整回答
反對 回復 2024-01-04
  • 1 回答
  • 0 關注
  • 132 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號