图片去除大块黑色区域

在使用MEGAN软件绘制分类学树状图时,如果物种较少,生成的图片会有大片的黑色区域,很不美观。

在使用MEGAN软件绘制分类学树状图时,如果物种较少,生成的图片会有大片的黑色区域,很不美观。那怎样去除呢,请看下面脚本:

脚本运行命令:

python /share/work/wangq/script/work/png_black.py -i /share/nas1/fanyc/project/zx-20171030-47_21_ITS/2/ITS_3/4.Analysis_output/Taxa_summary/MEGAN_tree/Taxonomic_tree
Usage:
  Options: 
     -i <str>  分类学树状图 png图片所在目录,也是输出目录(目录最后不要带"/")
    程序检索目录下所有png文件并处理,输出图片会覆盖原来的图片

处理前图片展示

attachments-2018-06-IADBrDlL5b35d71f7ab14.jpg

结果图展示

attachments-2018-06-sxey9pfs5b35d75994fb5.jpg

脚本代码:

import numpy
import sys, os, argparse, os.path,re,math
from PIL import Image
import matplotlib.pyplot as plt
##############################################
parser = argparse.ArgumentParser(description='This script is used to an error_bar')
parser.add_argument('-i','--in_dir',help='Please input  in_dir directory path',required=True)
args = parser.parse_args()
pathDir =  os.listdir(args.in_dir)
for allDir in pathDir:
    if (re.search(r'png$',allDir)):
        child = os.path.join('%s/%s' % (args.in_dir, allDir))
        img = Image.open(child) # Imgur's naming scheme
        pix = numpy.array(img)        # Convert to array
        black = numpy.array([0,0,0])
        white = numpy.array([255,255,255])
        black1 = numpy.array([0,0,0,255])
        white1 = numpy.array([255,255,255,255])
       #print pix
        
        print pix[1,1]
        pix2 = pix.copy()
        dim  = pix.shape
        #print pix[1,:]==black
        
        if (dim[2] == 3):
            for n in xrange(dim[0]):    
                if (pix[n,:]==black).all(): 
                    pix2[n,:,numpy.newaxis] = white
                    continue
                if ((pix[n,:]==black).sum()>=2.7*dim[1]):    
                    x = 0    
                    for m in xrange(dim[1]):
                        if (pix[n,m,:]==black).all():
                            x = x+1    
                    if (x >= dim[1]*0.9):
                        pix2[n,:,numpy.newaxis] = white
                            
#             for n in xrange(dim[1]):        
#                 if (pix[:,n]==black).all(): 
#                     pix2[:,n,numpy.newaxis] = white    
                    
                    
                    
        if (dim[2] == 4):
            for n in xrange(dim[0]):
                if (pix[n,:]==black1).all(): 
                    pix2[n,:,numpy.newaxis] = white1 
                    continue
                if ((pix[n,:]==black1).sum()>=3.7*dim[1]):    
                    x = 0    
                    for m in xrange(dim[1]):
                        if (pix[n,m,:]==black1).all():
                            x = x+1    
                    if (x >= dim[1]*0.9):
                        pix2[n,:,numpy.newaxis] = white1               
                
#             for n in xrange(dim[1]):    
#                 if (pix[:,n]==black1).all(): 
#                     pix2[:,n,numpy.newaxis] = white1
                
        fig = plt.figure()
        ax = fig.add_axes([0.0, 0.0, 1.0, 1.0], frameon=False, aspect=1)
    
        ax.imshow(pix2)
        ax.set_xticks([])
        ax.set_yticks([])
        plt.savefig(child,dpi=600)
        plt.close()
#.all()   .all()


  


  • 发表于 2018-06-29 14:55
  • 阅读 ( 3245 )
  • 分类:python

0 条评论

请先 登录 后评论
安生水
安生水

340 篇文章

作家榜 »

  1. omicsgene 691 文章
  2. 安生水 340 文章
  3. Daitoue 167 文章
  4. 生物女学霸 120 文章
  5. 红橙子 78 文章
  6. xun 76 文章
  7. rzx 74 文章
  8. CORNERSTONE 72 文章