简体   繁体   English

如何生成结果作为图形标题以及与绘制图像连接的文件名?

[英]How to generate result as the figure title along with filename connected with the plotted image?

I need help please, to bring the test results and the file name together as the title over the image that is evaluated.我需要帮助,将测试结果和文件名放在一起作为评估图像的标题。 I can generate the test results as a list (see image), I can add the test results over the image.我可以将测试结果生成为列表(见图),我可以将测试结果添加到图像上。 I can add the file name over the top of the image.我可以在图像顶部添加文件名。 But, I cannot do both.但是,我不能两者兼而有之。 Not sure what I am doing wrong.不知道我做错了什么。 Thank you.谢谢你。 在此处输入图片说明

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))

You can concatenate basename of image's path with output of calculate_brightness function and set result as title without overwriting them:您可以将图像路径的基本名称与 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 : If result of calculate_brightness function is a float number, you can solve your problem by this way: 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: For saving images to specified folder you can use plt.savefig method: 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))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM