1

I'm trying to read an svg file and convert it to png. I'm able to save the file and upload it properly:

import matplotlib.pyplot as plt
from cairo import svg2png
from scipy import misc
svg2png(url = r'https://dev.w3.org/SVG/tools/svgweb/samples/svg-files/410.svg',write_to='test.png')
im = misc.imread('test.png')
plt.imshow(im)

I would like to skip the saving part. I tried this:

import cv2
I = svg2png(url = r'https://dev.w3.org/SVG/tools/svgweb/samples/svg-files/410.svg')
nparr = np.fromstring(I, np.uint8)
img_np = cv2.imdecode(nparr, cv2.IMREAD_COLOR) 
im = cv2.cvtColor(img_np,cv2.COLOR_BGR2RGB)

but it is not the same as the image in the first case since it is not loading the 4th dimension of the png (i.e. the mask) and I get a black border around the image.

Any ideas how to solve this issue?

Eyal S.
  • 1,141
  • 4
  • 17
  • 29
  • 1
    The question is about converting the image to png and plotting it without saving it to disk. The link above does not answer this question since the png image is saved to disk. – Eyal S. Nov 07 '17 at 07:14
  • yeah, so `im = cv2.imdecode(nparr, cv2.IMREAD_UNCHANGED)` preserves all the 4 dims. – ixaxaar Mar 12 '18 at 11:29

0 Answers0