1

I have written a thing in python that takes an image as an input, and writes the output as a triangle tessellated version of the input image. For example this image of a kitten:

enter image description here

gets transformed into this:

enter image description here

But when I try to save the image, somehow I get a white border around it which I can't seem to get rid of.

The code that takes care of saving the image is as follows:

my_dpi=100
figSize = imageWidth / float(my_dpi), imageHeight / float(my_dpi)
fig = plt.figure(figsize=figSize)
ax = fig.add_axes([0, 0, 1, 1])
ax.invert_yaxis()
for triangle, eColor, fColor in zip(tris.simplices, triColors, triColors):
    p = Polygon([tris.points[i] for i in triangle], closed=True, facecolor=fColor, edgecolor=eColor)
    ax.add_patch(p)
ax.axis('tight')
ax.set_axis_off()
ax.get_xaxis().set_visible(False)
ax.get_yaxis().set_visible(False)
outfile_name = "%s-%s-%s.png" % (sys.argv[1].split('.')[0], str(tris.simplices.shape[0]), str(i))
fig.savefig(outfile_name, bbox_inches='tight', pad_inches=0, dpi=my_dpi)

tris is just a triangulation object that comes from scipy's delaunay triangulation module. I use that to calculate the color of each triangle and add a polygon patch for each triangle in the for loop. The final set of patches gets written to an image when the figure is saved onto the file. Because of the white background of this website, you might not be able to see the white border, but if you open the output image link, the white border is clearly visible there. I've tried everything that I could think of but can't seem to get rid of this white border. Is there a special way to get rid of it if I'm saving the figure using polygon patches?

pslayer89
  • 307
  • 5
  • 14
  • 1
    What happens if you remove the options `bbox_inches='tight', pad_inches=0,` from the `savefig` command? – ImportanceOfBeingErnest Aug 25 '18 at 12:31
  • 1
    You probably need to set the axes position property to [0,0,1,1]. – Cris Luengo Aug 25 '18 at 12:33
  • 1
    Can you try adding `plt.tight_layout()`? – Sheldore Aug 25 '18 at 14:22
  • @CrisLuengo Doesn't the line `ax = fig.add_axes([0, 0, 1, 1])` does exactly that? I also tried the other things suggested here, but I'm still getting same result. I'm pretty much stumped! :( – pslayer89 Aug 25 '18 at 17:12
  • 1
    Sorry, I hadn’t seen that line. The figure on the screen has no border? Maybe try a different renderer? – Cris Luengo Aug 25 '18 at 17:21
  • If you're referring to the output image in the post, then I'd suggest try opening the image in another tab and then you might be able to see the white border. Also, could you suggest any other renderer that might be solution to this issue? – pslayer89 Aug 26 '18 at 02:44
  • I'd recommend seeing this previous SO question: https://stackoverflow.com/questions/4325733/save-a-subplot-in-matplotlib – PMende Aug 26 '18 at 03:05
  • pslayer89: no, I meant the matplotlib figure you create and save, does it have a border? – Cris Luengo Aug 26 '18 at 04:43
  • Sorry about the late reply, but I took a look at the link @PMende posted, and using `extent.expanded(0.9, 0.9)` in the savefig function seemed to get rid of the white margins on the 4-5 photos I tested with. Although this only seems more of a workaround and less of a concrete solution to the problem. – pslayer89 Aug 29 '18 at 10:33
  • @CrisLuengo since the background of both the `plt.show` and the border margin is white, it's pretty hard to tell whether a border is there or not. – pslayer89 Aug 29 '18 at 10:37

0 Answers0