0

I'm trying to convert a bunch of SVG pictures to JPG with ImageMagick. The problem is that the SVGs have been created with ex units, and the conversion process creates output files having 1px size.

This is an example of SVG:

<svg xmlns:xlink="http://www.w3.org/1999/xlink" width="5.641ex" height="2.009ex" style="vertical-align: -0.671ex;" viewBox="0 -576.1 2428.8 865.1" role="img" focusable="false" xmlns="http://www.w3.org/2000/svg" aria-labelledby="MathJax-SVG-1-Title">
<title id="MathJax-SVG-1-Title">x comma y comma z</title>
<defs aria-hidden="true">
<path stroke-width="1" id="E1-MJMATHI-78" d="M52 289Q59 331 106 386T222 442Q257 442 286 424T329 379Q371 442 430 442Q467 442 494 420T522 361Q522 332 508 314T481 292T458 288Q439 288 427 299T415 328Q415 374 465 391Q454 404 425 404Q412 404 406 402Q368 386 350 336Q290 115 290 78Q290 50 306 38T341 26Q378 26 414 59T463 140Q466 150 469 151T485 153H489Q504 153 504 145Q504 144 502 134Q486 77 440 33T333 -11Q263 -11 227 52Q186 -10 133 -10H127Q78 -10 57 16T35 71Q35 103 54 123T99 143Q142 143 142 101Q142 81 130 66T107 46T94 41L91 40Q91 39 97 36T113 29T132 26Q168 26 194 71Q203 87 217 139T245 247T261 313Q266 340 266 352Q266 380 251 392T217 404Q177 404 142 372T93 290Q91 281 88 280T72 278H58Q52 284 52 289Z"></path>
<path stroke-width="1" id="E1-MJMAIN-2C" d="M78 35T78 60T94 103T137 121Q165 121 187 96T210 8Q210 -27 201 -60T180 -117T154 -158T130 -185T117 -194Q113 -194 104 -185T95 -172Q95 -168 106 -156T131 -126T157 -76T173 -3V9L172 8Q170 7 167 6T161 3T152 1T140 0Q113 0 96 17Z"></path>
<path stroke-width="1" id="E1-MJMATHI-79" d="M21 287Q21 301 36 335T84 406T158 442Q199 442 224 419T250 355Q248 336 247 334Q247 331 231 288T198 191T182 105Q182 62 196 45T238 27Q261 27 281 38T312 61T339 94Q339 95 344 114T358 173T377 247Q415 397 419 404Q432 431 462 431Q475 431 483 424T494 412T496 403Q496 390 447 193T391 -23Q363 -106 294 -155T156 -205Q111 -205 77 -183T43 -117Q43 -95 50 -80T69 -58T89 -48T106 -45Q150 -45 150 -87Q150 -107 138 -122T115 -142T102 -147L99 -148Q101 -153 118 -160T152 -167H160Q177 -167 186 -165Q219 -156 247 -127T290 -65T313 -9T321 21L315 17Q309 13 296 6T270 -6Q250 -11 231 -11Q185 -11 150 11T104 82Q103 89 103 113Q103 170 138 262T173 379Q173 380 173 381Q173 390 173 393T169 400T158 404H154Q131 404 112 385T82 344T65 302T57 280Q55 278 41 278H27Q21 284 21 287Z"></path>
<path stroke-width="1" id="E1-MJMATHI-7A" d="M347 338Q337 338 294 349T231 360Q211 360 197 356T174 346T162 335T155 324L153 320Q150 317 138 317Q117 317 117 325Q117 330 120 339Q133 378 163 406T229 440Q241 442 246 442Q271 442 291 425T329 392T367 375Q389 375 411 408T434 441Q435 442 449 442H462Q468 436 468 434Q468 430 463 420T449 399T432 377T418 358L411 349Q368 298 275 214T160 106L148 94L163 93Q185 93 227 82T290 71Q328 71 360 90T402 140Q406 149 409 151T424 153Q443 153 443 143Q443 138 442 134Q425 72 376 31T278 -11Q252 -11 232 6T193 40T155 57Q111 57 76 -3Q70 -11 59 -11H54H41Q35 -5 35 -2Q35 13 93 84Q132 129 225 214T340 322Q352 338 347 338Z"></path>
</defs>
<g stroke="currentColor" fill="currentColor" stroke-width="0" transform="matrix(1 0 0 -1 0 0)" aria-hidden="true">
 <use xlink:href="#E1-MJMATHI-78" x="0" y="0"></use>
 <use xlink:href="#E1-MJMAIN-2C" x="572" y="0"></use>
 <use xlink:href="#E1-MJMATHI-79" x="1017" y="0"></use>
 <use xlink:href="#E1-MJMAIN-2C" x="1515" y="0"></use>
 <use xlink:href="#E1-MJMATHI-7A" x="1960" y="0"></use>
</g>
</svg>

I run the following command:

convert -verbose my_img.svg my_img.jpg

which outputs the following:

'inkscape' '/tmp/magick-17797-lltik3v5qAT' --export-png='/tmp/magick-177971kXIvj8Qa32W' --export-dpi='96,96' --export-background='rgb(100%,100%,100%)' --export-background-opacity='1' > '/tmp/magick-17797RkytiD0vSj5W' 2>&1
/tmp/magick-177971kXIvj8Qa32W PNG 1x1 1x1+0+0 8-bit sRGB 142B 0.000u 0:00.000
my_img.svg SVG 1x1 1x1+0+0 8-bit sRGB 142B 0.000u 0:00.000
my_img.svg=>my_img.jpg SVG 1x1 1x1+0+0 8-bit Bilevel Gray 160B 0.000u 0:00.000

Can I somehow specify the x-height of the font?

Are there any alternatives to convert these SVGs to JPG?

7
  • This might answer your problem: stackoverflow.com/a/35490303/1005422
    – AAGD
    Nov 30 '20 at 12:13
  • No, unfortunately it doesn't work... With that command the overall image size is correct, but it is just an empty image. ImageMagick and Inkscape don't recognize ex units.
    – Davide_sd
    Nov 30 '20 at 12:33
  • Have you tried editing the files in a text editor to simply change these units from ex to perhaps something like cm?
    – Billy Kerr
    Nov 30 '20 at 12:34
  • Unfortunately, that's not an option because it distorts the image.
    – Davide_sd
    Nov 30 '20 at 12:42
  • @Davide_sd - I just tried this and it doesn't seem to distort the image. You need to leave the values the same, just change "ex" to "cm".
    – Billy Kerr
    Nov 30 '20 at 17:19
1

ImageMagick's convert function is going to call Inkscape for SVG images, which doesn't know anything about relative unit measure (such as ex). Nonetheless, browsers are very good with those units. Meets svgexport, a Node.js module and command-line utility which is perfect for this job. I converted a few hundreds images in a matter of minutes.

0

You could try to manually delete the string width="5.641ex" height="2.009ex". Then Inkscape will open it.

Or, if by any chance you're on a Mac, you could use Sketch to open & export it.


Edit: I tried ImageMagick (7.0.10-44 Q16 x86_64) with some tweaks, which worked well for me:

  1. Change all currentColor to #000 (or whatever color you prefer).

  2. Try convert -resize 600x600 test.svg test.png which sets a size limit, and shouldn't mess with the aspect ratio.

'inkscape' '/var/folders/fm/gnq733s10f7gvvycgwszj_zh0000gn/T/magick-Qa0hI5LRc6zC98FNV7GoQCNZzsQ6BS4q' --export-filename='/var/folders/fm/gnq733s10f7gvvycgwszj_zh0000gn/T/magick-vRcf_LEuHf9-WimVju5bmTyMdsbxUiIv.png' --export-dpi='96' --export-background='rgb(100%,100%,100%)' --export-background-opacity='0.99999999999900002212' > '/var/folders/fm/gnq733s10f7gvvycgwszj_zh0000gn/T/magick-H8RifjkiJ7Jy_RCjj67tU_lBppSIhTZ5' 2>&1
mvg:/var/folders/fm/gnq733s10f7gvvycgwszj_zh0000gn/T/magick-Joh3Z8CN7YgiF9PW0EZESkuovvsFM2DM=>/var/folders/fm/gnq733s10f7gvvycgwszj_zh0000gn/T/magick-Joh3Z8CN7YgiF9PW0EZESkuovvsFM2DM MVG 2429x865 2429x865+0+0 16-bit sRGB 3262B 0.770u 0:00.108
test.svg MVG 2429x865 2429x865+0+0 16-bit sRGB 3262B 0.000u 0:00.000
test.svg=>test.png MVG 2429x865=>600x214 600x214+0+0 16-bit sRGB 3262B 0.620u 0:00.079
1
  • It works, but it creates too big output files. In fact, with bigger SVG images ImageMagick crashes: width or height exceeds limit, even if I specify a resize value.
    – Davide_sd
    Nov 30 '20 at 14:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.