在使用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文件并处理,输出图片会覆盖原来的图片
脚本代码:
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()
如果觉得我的文章对您有用,请随意打赏。你的支持将鼓励我继续创作!