0

I have six png figures that I want to combine into a single high resolution figure using the following script:

from svgutils.compose import *
os.system("python pngtosvg.py")

def new_tile(self, ncols, nrows):
    dx = self.width.to('px').value/ncols
    dy = self.height.to('px').value/nrows
    ix, iy = 0, 0
    for el in self:
        el.move(dx*ix, dy*iy)
        ix += 1
        if ix >= ncols:
            ix = 0
            iy += 1
        if iy > nrows:
            break
    return self

Figure.tile = new_tile

scale_factor = 2.
myfigure = Figure("20cm", "13.7cm", 
                  SVG("figure_1.svg").scale(scale_factor), 
                  SVG("figure_2.svg").scale(scale_factor), 
                  SVG("figure_3.svg").scale(scale_factor), 
                  SVG("figure_4.svg").scale(scale_factor), 
                  SVG("figure_5.svg").scale(scale_factor), 
                  SVG("figure_6.svg").scale(scale_factor) 
                  ).tile(1, 6)

myfigure.save('figure.svg')
os.system('inkscape --export-png=figure.png figure.svg --export-background=white --export-area-drawing')
os.system("rm /home/username/Desktop/*.svg")

The problem is: Despite the high resolution of individual figures 1 through 6, the resolution of the final combined figure is so low that I can hardly read legends and axes titles. Do you know how to modify my code or probably work with a better package than svgutils to get the best resolution when combining my png figures?

Diziet Asahi
  • 38,379
  • 7
  • 60
  • 75
Rebel
  • 472
  • 8
  • 25
  • Yes, I actually need to convert png files to svg files before using this package and I don't like this idea either. I searched and I found an --export option for inkscape as you suggested. However, the conversion is super slow and takes forever for a DPI=5000 and still not as good as I want it. – Rebel Mar 21 '20 at 20:22
  • So, using this package doesn't make any sense. All is lost in the conversion from png to svg. Just import the png's directly into Inkscape, without converting to svg. – JohanC Mar 21 '20 at 20:29
  • @Johan, But how to combine pngs? I need a single figure showing 6 panels from top to bottom. – Rebel Mar 21 '20 at 20:36
  • 1
    Just drag&drop them into Inkscape? Or any other program that accepts png's? Something simple & free is [Irfanview](https://www.irfanview.com/) or [Gimp](https://www.gimp.org/) or many many others. The most used programmatic tool is [Imagemagick](https://imagemagick.org/index.php) which works via simple command line options. – JohanC Mar 21 '20 at 20:41
  • 1
    For example with Imagemagick, look [here](https://www.imagemagick.org/discourse-server/viewtopic.php?t=20212). Probably something like `montage fig1.png fig2.png ... fig6.png -geometry +0+0 -tile 2x3 tiled_figures.png` – JohanC Mar 21 '20 at 20:50
  • Thank you! How to control the size of each panel in the combined figure using -scale option? – Rebel Mar 21 '20 at 20:57
  • Scaling all figures by a factor of 2 really doesn't add any useful resolution. Take a look at the [manual](http://www.imagemagick.org/Usage/montage/#geometry_size) to check out all the options. – JohanC Mar 21 '20 at 21:03

0 Answers0