Convert SVG to transparent PNG with antialiasing, using ImageMagick

后端 未结 8 881
耶瑟儿~
耶瑟儿~ 2020-12-07 13:06

I want to convert SVG images to PNG files with transparent background and anti-aliased edges (using semi-transparent pixels). Unfortunately I can\'t get ImageMagick to do th

8条回答
  •  感情败类
    2020-12-07 14:01

    I add a rect as background. The embed CSS hide the background. Then I catch its color for setting the transparent attribute of ImageMagick.


    SVG file:

    
    
    
    
        
    
    
    
    
        OK
    
    
    
    

    bash script

    #!/bin/bash
    
    
    BASE_DIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null && pwd )
    SVG_DIR="$BASE_DIR/import"
    PNG_DIR="$BASE_DIR/export"
    
    for f in `ls $SVG_DIR/*.svg`
    do
        g="$PNG_DIR/$(basename "$f" .svg).png"
        BGCOLOR=`grep 'id="background"' $f \
            | sed 's/.* fill="\([^"]*\)".*/\1/'`
    
        convert $f -transparent "$BGCOLOR" $g
    done
    

提交回复
热议问题