To use svg2rlg to convert a portion of an SVG image into a PNG with a specified percentage size, you can follow the steps below:
pip install svglib
pip install reportlab
pip install Pillow
from svglib.svglib import svg2rlg
from reportlab.graphics import renderPM
from PIL import Image
drawing = svg2rlg('input.svg')
drawing_width = drawing.getWidth()
drawing_height = drawing.getHeight()
x = 0
y = 0
width = int(drawing_width * 0.5) # 50% width
height = int(drawing_height * 0.5) # 50% height
portion_drawing = drawing[x:x+width, y:y+height]
pm = renderPM.drawToPIL(portion_drawing)
percentage_size = 50 # 50%
resize_width = int(pm.width * percentage_size / 100)
resize_height = int(pm.height * percentage_size / 100)
pm = pm.resize((resize_width, resize_height))
pm.save('output.png')
This will create a PNG image containing the portion of the SVG image that you specified, resized to the desired percentage size.
Asked: 2023-05-15 10:09:26 +0000
Seen: 9 times
Last updated: May 15 '23