How to remove large black areas from pictures in python
Editor to share with you how to deal with python pictures to remove large black areas, I believe most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!
When using MEGAN software to draw a taxonomic tree, if there are few species, the resulting picture will have a large black area, which is very unattractive.
When using MEGAN software to draw a taxonomic tree, if there are few species, the resulting picture will have a large black area, which is very unattractive. How to remove it, please see the following script:
Script run commands:
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_treeUsage: Options:-I taxonomic tree view the directory where the png picture is located, and also the output directory (do not have "/" at the end of the directory) the program retrieves all png files in the directory and processes The output picture will overwrite the original picture.
Script code:
Import numpyimport sys, os, argparse, os.path,re,mathfrom PIL import Imageimport matplotlib.pyplot as plt#parser = argparse.ArgumentParser (description='This script is used to an error_bar') parser.add_argument Required=True) args = parser.parse_args () pathDir = os.listdir (args.in_dir) for allDir in pathDir: if (re.search (rascpngharmagogy all Dir)): child = os.path.join ('% s bank% s'% (args.in_dir, allDir)) img = Image.open (child) # Imgur's naming scheme pix = numpy.array (img) # Convert to array black = numpy.array 0]) white = numpy.array ([255255255]) black1 = numpy.array ([0Magne0m0255]) white1 = numpy.array ([255255255255]) # print pix print pix [1] pix2 = pix.copy () dim = pix.shape # print pix [1 :] = = black if (dim [2] = = 3): for n in xrange (dim [0]): if (pix [n-force:] = = black). All (): pix2 [n-daginghe-mam numpy.newaxis] = white continue if ((pix [n) :] = = black) .sum () > = 2.7*dim [1]): X = 0 for m in xrange (dim [1]): if (pix [nmemm) :] = = black) .all (): X = xint1 if (x > = dim [1] * 0.9): pix2 [n Reagle Geng Zhi numpy.newaxis] = white # for n in xrange (dim [1]): # if (pix [: N] = = black) .all (): # pix2 [:, nnumpy.newaxis] = white if (dim [2] = = 4): for n in xrange (dim [0]): if (pix [nnumpy:] = = black1). All (): pix2 [n :, numpy.newaxis] = white1 continue if ((pix [NMagne:] = = black1). Sum () > = 3.7*dim [1]): X = 0 for m in xrange (dim [1]): if (pix [NMagnem) :] = = black1) .all (): X = xint1 if (x > = dim [1] * 0.9): pix2 [n Reagle Geng Zhi numpy.newaxis] = white1 # for n in xrange (dim [1]): # if (pix [: N] = = black1) .all (): # pix2 [:, nnumpy.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 () above are all the contents of the article "how to remove large black areas in python" Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!