(Environment) (Cairo) – (1) Installing Required Libraries to Convert SVG Files to PNG

Installing Required Libraries to Convert SVG Files to PNG


The goal was to apply NBP OCR to SVG images and record the output as JSON (text, coordinates).

To achieve this, the first step was to convert the ".svg" file into ".png".

Initially, I thought that simply installing cairo via pip would suffice, but I soon realized that additional steps were necessary.

To avoid wasting time solving issues when I attempted it again later, I decided to document the process and share it.

If you need code references or encounter any setup issues, please feel free to leave a message here: https://github.com/shipjobs/HAND2TEXT/issues.



Environment

Language: Python 3.8.3 64bit
Operating System: Windows 10
Development Environment: Visual Studio Code

To convert an ".svg" file to ".png", if you import the libraries as shown below, you will naturally encounter a reference error:


import cairo from svglib.svglib 
import svg2rlg 
import cairosvg

For those who, like me, need PNG files for NBP OCR or similar tasks, let's install the necessary libraries and set up the required environment.

#-----------------------------------------------------
# 1. cairo 설치  (파이썬 버전과 cpXX 가 맞아야 함 )
#  > https://frhyme.github.io/python-lib/python_a_what_is_svg/
#  > https://wikidocs.net/75429  라이브러리 설치  ,
#  > https://www.lfd.uci.edu/~gohlke/pythonlibs/#pycairo  설치
# 설치 후 바로 import 되지 않으면 VS code 재시작.. 1~2회  필요

#-----------------------------------------------------
# - 파일명 : pycairo-1.20.0-cp38-cp38-win_amd64.whl       --> 파이썬 버전
# > python -m pip install pycairo-1.20.0-cp38-cp38-win_amd64.whl
# > easy_install .\pycairo-1.20.1-cp38-cp38-win_amd64.whl  # python -m pip install 설치 실패시
# - Installing collected packages: pycairo
# - Successfully installed pycairo-1.20.0

#-----------------------------------------------------
# 2. cairosvg 설치
#-----------------------------------------------------
# - Successfully installed pycairo-1.20.0
# (base) pip3 install cairosvg

#-----------------------------------------------------
# 3. svglib 설치
# > ckarh https://boltlessengineer.tistory.com/68
#-----------------------------------------------------
#  - pip3 install svglib

#-----------------------------------------------------
# 4. UniConverter2.0 설치  # Wondershare Uniconverter
#-----------------------------------------------------
#   > UniConverter 설치 경로에서 "dll"하위 디렉토리를 찾음 (ex) C:\Program Files\UniConvertor-2.0rc4\dlls).
#   > 이 "dll"경로를 시스템 경로에 추가
#   > VSCode를 닫고 프로젝트를 다시 로드
#   > 서버를 다시 실행
#   > 사용 : https://videoconverter.wondershare.net/?gclid=Cj0KCQjw--GFBhDeARIsACH_kdYVKcguQgYgqQRmiYjtpQ5O0T1XK4QG7hTkPJFcztkO8jkOeCnmF2gaAqWmEALw_wcB&gclsrc=aw.ds 
#     - 다운로드 : https://downloads.sk1project.net/uniconvertor/2.0rc4/uniconvertor-2.0rc4-win64_headless.msi 
#   > 사용 Code : https://github.com/pygobject/pycairo

#-----------------------------------------------------
# 5. gtk2-runtime-2.24.33-2021-01-30-ts-win64.exe 설치
#   > https://github.com/tschoonj/GTK-for-Windows-Runtime-Environment-Installer
#-----------------------------------------------------

Key Code:

python
cairosvg.svg2png(.....)

The full code can be referenced at:
https://github.com/shipjobs/HAND2TEXT.



    #Excel 로 부터 읽어온 read_data 의  URLlist 
    def read_svg_from_URL_save_png(self,  URLlist  , save_path ='C:\\Mydata\\svg2png\\'):
        
        
        URLlist = URLlist        
        failcnt = 0  #실패 횟수 카운팅

        # 한줄씩 URL 에서 svg 파일을 읽어온다.
        # 웹특성.. 지연 및 진행 사항 체크를 위한 try 수행
        for cnt , row_svg_URL in enumerate(URLlist['제출파일URL']):
            
            file_save_path =  save_path  + str(cnt)  + '.png'
            try:
                cairosvg.svg2png(urlrow_svg_URL , write_to = file_save_path  , background_color="white"dpi = 100
                file_save_path = ""
                
            except urllib.error.HTTPError as e:
                failcnt += 1
                print("HTTP error No: [" , str(failcnt), "]  [" , e.code , "] 5초 대기")
                time.sleep(5)  #import time
                
            except urllib.error.URLError as e:
                print("URL error No: [" , str(failcnt), "]  [" , e.code , "] 종료" )
                exit()
            else:
                failcnt += 1
                print("Other error No: [" , str(failcnt), "] 대기")
                time.sleep(5)






Comments

Popular posts from this blog

[MaritimeCyberTrend] Relationship and prospects between U.S. Chinese maritime operations and maritime cybersecurity

인공지능 서비스 - 챗봇, 사전에 충분한 지식을 전달하고 함께 학습 하기!

[Curriculum] Sungkyunkwan University - Department of Information Security - Course Sequence by Areas of Interest