Hello I'm trying to get this code work:
import os
import cairosvg
from PIL import Image
dir_A = r'C:\Users\Utente\Desktop\Underwear - Copia'
dir_B = r'C:\Users\Utente\Desktop\Grande'
def convert_svg_to_png(svg_path, png_path):
# Convert SVG to PNG using CairoSVG
svg_data = open(svg_path, 'rb').read()
cairosvg.svg2png(file_obj=svg_data, write_to=png_path)
# Load the PNG and save it again to ensure transparency preservation
image = Image.open(png_path)
image.save(png_path)
def process_directory(input_dir, output_dir):
for root, _, files in os.walk(input_dir):
for file in files:
if file.lower().endswith('.svg'):
svg_path = os.path.join(root, file)
png_path = os.path.join(output_dir, file.replace('.svg', '.png'))
convert_svg_to_png(svg_path, png_path)
if __name__ == "__main__":
process_directory(dir_A, dir_B)
print("Finito!")
However it gives me this error: Traceback (most recent call last): File "C:/Users/Utente/AppData/Local/Programs/Python/Python311/dawdwadwadwadadwadwadwadwad.py", line 2, in <module> import cairosvg File "C:\Users\Utente\AppData\Local\Programs\Python\Python311\Lib\site-packages\cairosvg\__init__.py", line 26, in <module> from . import surface # noqa isort:skip File "C:\Users\Utente\AppData\Local\Programs\Python\Python311\Lib\site-packages\cairosvg\surface.py", line 9, in <module> import cairocffi as cairo File "C:\Users\Utente\AppData\Local\Programs\Python\Python311\Lib\site-packages\cairocffi\__init__.py", line 47, in <module> cairo = dlopen( File "C:\Users\Utente\AppData\Local\Programs\Python\Python311\Lib\site-packages\cairocffi\__init__.py", line 44, in dlopen raise OSError(error_message) # pragma: no cover OSError: no library called "cairo-2" was found no library called "cairo" was found no library called "libcairo-2" was found cannot load library 'libcairo.so.2': error 0x7e. Additionally, ctypes.util.find_library() did not manage to locate a library called 'libcairo.so.2' cannot load library 'libcairo.2.dylib': error 0x7e. Additionally, ctypes.util.find_library() did not manage to locate a library called 'libcairo.2.dylib' cannot load library 'libcairo-2.dll': error 0x7e. Additionally, ctypes.util.find_library() did not manage to locate a library called 'libcairo-2.dll'
I tried following the advice found on get cairosvg working in windows however installing the uniconverter msi and clicking it will only show "gathering required informations" and after 3 seconds of loading it closes.
I've pip installed and the prompt gave me:
My only objective is to have a python code that manages to transform svgs in pngs preserving transparency and color. Having 60k files to convert in must'nt be too slow too.
I have really little knowledge of this subject aswell so sorry for stupid errors done by me or obvious things.