【问题标题】:How to prefix image processing result with the name of the image in Python?如何在 Python 中使用图像名称作为图像处理结果的前缀?
【发布时间】:2013-08-26 02:27:03
【问题描述】:
def generalarea(self):
  for filename in glob.iglob ('*.tif'):
    img = np.asarray(Image.open(filename).convert('L'))                            
    img = 1 * (img < 127)
    garea = (img == 0).sum()
    print garea

def areasplit(self):
  for filename in glob.iglob ('*.tif'):    
    img = np.asarray(Image.open(filename).convert('L'))                            
    img = 1 * (img < 127)
    areasplit = np.split(img.ravel(), 24) # here we are splitting converted to 1D array
  for i in areasplit:
   sarea = (i == 0).sum()
   print sarea

这两种方法处理工作目录中的 .tif 图像。我需要将前缀 IMAGENAME 添加到数字结果中(如:firstimage、6786876876 或 secondimage___67876876)。如何实现这个想法?

【问题讨论】:

    标签: image-processing python-2.7 numpy scipy


    【解决方案1】:

    例如,对于第一个函数,而不是 print garea,您可以:

    print "%s___%d" % (filename, garea)
    

    或者"%s, %d",如果你想要一个逗号。请参阅string formatting operations 上的 Python 文档。

    【讨论】:

      【解决方案2】:

      % 样式格式将被弃用,最终......所以他们说。

      最好使用新样式 (doc)

      print "{fname}__{garea}".format(garea=garea, fname=filename)
      

      【讨论】:

        猜你喜欢
        • 2013-10-18
        • 1970-01-01
        • 2014-10-19
        • 2020-10-28
        • 2019-04-09
        • 2016-04-11
        • 2014-06-24
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多