libvips / pyvips

python binding for libvips using cffi
MIT License
649 stars 50 forks source link

How to directly save svg string to png image? #511

Closed sunwoo76 closed 1 month ago

sunwoo76 commented 1 month ago

Hello :)

I have the following code as below:

def save_svgstr(svg_string, outpath):
    # Open a new file in write mode
    with open(outpath, 'w') as file:
        # Write the SVG string to the file
        file.write(svg_string)
    file.close()

svgstr = '<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="670.0" height="300.0" viewBox="0 0 670.0 300.0"> <defs> <pattern id="pattern_box" preserveAspectRatio="none" width="100%" height="100%" viewBox="0 0 670.0 300.0"> <image width="670.0" height="300.0" xlink:href="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAABAAAAAHKCAIAAAASNK6hAAAJTUlEQVR4nO3XMQHAIADAsDEPuMAB/n3hAo4mCvp2zL0+AACg4X8dAAAA3GMAAAAgxAAAAECIAQAAgBADAAAAIQYAAABCDAAAAIQYAAAACDEAAAAQYgAAACDEAAAAQIgBAACAEAMAAAAhBgAAAEIMAAAAhBgAAAAIMQAAABBiAAAAIMQAAABAiAEAAIAQAwAAACEGAAAAQgwAAACEGAAAAAgxAAAAEGIAAAAgxAAAAECIAQAAgBADAAAAIQYAAABCDAAAAIQYAAAACDEAAAAQYgAAACDEAAAAQIgBAACAEAMAAAAhBgAAAEIMAAAAhBgAAAAIMQAAABBiAAAAIMQAAABAiAEAAIAQAwAAACEGAAAAQgwAAACEGAAAAAgxAAAAEGIAAAAgxAAAAECIAQAAgBADAAAAIQYAAABCDAAAAIQYAAAACDEAAAAQYgAAACDEAAAAQIgBAACAEAMAAAAhBgAAAEIMAAAAhBgAAAAIMQAAABBiAAAAIMQAAABAiAEAAIAQAwAAACEGAAAAQgwAAACEGAAAAAgxAAAAEGIAAAAgxAAAAECIAQAAgBADAAAAIQYAAABCDAAAAIQYAAAACDEAAAAQYgAAACDEAAAAQIgBAACAEAMAAAAhBgAAAEIMAAAAhBgAAAAIMQAAABBiAAAAIMQAAABA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xAAAAECIAQAAgBADAAAAIQYAAABCDAAAAIQYAAAACDEAAAAQYgAAACDEAAAAQIgBAACAEAMAAAAhBgAAAEIMAAAAhBgAAAAIMQAAABBiAAAAIMQAAABAiAEAAIAQAwAAACEGAAAAQgwAAACEGAAAAAgxAAAAEGIAAAAgxAAAAECIAQAAgBADAAAAIQYAAABCDAAAAIQYAAAACDEAAAAQYgAAACDEAAAAQIgBAACAEAMAAAAhBgAAAEIMAAAAhBgAAAAIMQAAABBiAAAAIMQAAABAiAEAAIAQAwAAACEGAAAAQgwAAACEGAAAAAgxAAAAEHIAN7wEEEP2NcgAAAAASUVORK5CYII="/> </pattern> </defs> <rect id="rect_box" width="670.0" height="300.0" fill="url(#pattern_box)"/> </svg>'

save_svgstr(svgstr, "./test.svg")
image = pyvips.Image.new_from_file("./test.svg")
image.write_to_file(("./test.png")

When I convert svg string to png format, I always save svg string to svg file and load using pyvips library. Finally, save with PIL Image library.

How can I directly save the svg string to png image?

Thank you!

jcupitt commented 1 month ago

Hello @sunwoo76,

I would write eg.:

#!/usr/bin/env python3

import pyvips

x = pyvips.Image.new_from_buffer(b"""
    <svg viewBox="0 0 128 128">
        <rect width="128" height="128" fill="#efe0b966" />
    </svg>
""", "")

x.write_to_file("x.png")
sunwoo76 commented 1 month ago

Hello @sunwoo76,

I would write eg.:

#!/usr/bin/env python3

import pyvips

x = pyvips.Image.new_from_buffer(b"""
    <svg viewBox="0 0 128 128">
        <rect width="128" height="128" fill="#efe0b966" />
    </svg>
""", "")

x.write_to_file("x.png")

Thank you for replying :)

However, when I save the some svg strings the following error has occurred:

image = pyvips.Image.new_from_buffer(svg_str)
TypeError: Image.new_from_buffer() missing 1 required positional argument: 'options'

Can this issue be occurred by the lower version of libvips or pyvips? Thanks:

jcupitt commented 1 month ago

You need the "" final argument.

    </svg>
""", "")