반응형
작성일: 2025년 2월 2일

 

PDF 파일에 있는 각 페이지를 각각의 그림 파일(PNG)로 변환하고자 할 때, 아래처럼 따라하면 된다.

 

 

PyMuPDF 파이썬 패키지 설치하기

$ pip install pymupdf

 

 

PDF 파일을 이미지 파일(PNG)로 변환하기 위한 코드 작성하기

## File name: main.py

import sys, pymupdf

fname = sys.argv[1]
doc = pymupdf.open(fname)

for page in doc:  
    pix = page.get_pixmap(dpi=300)  ## 참고: 300 DPI 해상도로 이미지를 변환.
    pix.save("page-%i.png" % page.number)  # PNG 포맷의 이미지 파일로 저장

 

 

Python 코드 실행하기

$ python3 main.py myexample.pdf

 

위 python code를 실행하면,  page-1.png  page-2.png ... 처럼 각 페이지 단위로 그림 파일이 생성된다.

 

 

PyMuPDF 파이썬 패키지 - 공식 매뉴얼

https://pymupdf.readthedocs.io/en/latest/recipes-images.html#how-to-make-images-from-document-pages

 

 

반응형
작성일: 2025년 1월 12일

 

문서를 읽다보면, 한 화면씩 넘기면서 캡처한 내용을 PDF 파일로 만들어야 할 경우가 있다. (즉, eBook처럼 만들어야 하는 경우)

그리고 이 PDF 파일에서 문자열 검색이 가능하도록 OCR을 적용해야 할 경우도 발생한다.

그럴 때 아래와 같이 Python Script를 작성해서 실행하면 된다.

 

Step 1: 화면 캡처해서 PNG 파일로 저장하기

아래 Python source code 처럼 작성하고, -R 옵션 부분만 수정해서 사용하면 된다.

-R 옵션은 캡처할 화면의 위치(X, Y 좌표)이다.

그리고 maxPages는 내가 만들 PDF 파일의 전체 페이지이다.

#!/opt/homebrew/bin/python3

import os
import time
import pyautogui

## 숫자 745를 2로 나눈 이유는 2 Page를 한 화면으로 캡처하기 위함.
maxPage = (745 // 2) + 1

time.sleep(5)

for idx in range(maxPage):
    ## 숫자 110,170,1785,1205는 캡처할 화면의 위치에 대한 X-Y 좌표 값이다.
    ## 캡처할 때마다 이 숫자를 조정해서 사용해야 한다.
    my_cmd = "screencapture -R110,170,1785,1205 p_%04d.png" % idx
    print("cmd:", my_cmd)
    os.system(my_cmd)

    ## 오른쪽 화살표 키를 누르면서 한 화면씩 캡처하기.
    pyautogui.press('right')
    time.sleep(1)

 

 

Step 2: 여러 개의 PNG 파일을 PDF 파일로 변환하기

Python source code를 작성하기 전에 "img2pdf" package를 설치한다.

$ pip3 install img2pdf

 

자세한 내용은 아래 Web document를 참고;

https://pypi.org/project/img2pdf/

 

 

img2pdf 명령어도 제공하기 때문에 python source code를 작성하지 않아도 image 파일을 PDF 파일로 변환할 수 있다.

그러나 여러 image 파일간의 순서(sorting) 같은 것도 필요해서 아래와 같이 source code를 작성해야 할 필요가 있을 것이다.

#!/opt/homebrew/bin/python3

import os
import img2pdf

# Replace the directory path with the folder containing JPEG images to be converted
directory_path = "."

image_files = [idx for idx in sorted(os.listdir(directory_path)) if idx.endswith(".png")]

# Convert the list of JPEG images to a single PDF file
pdf_data = img2pdf.convert(image_files)

# Write the PDF content to a file (make sure you have write permissions for the specified file)
with open("my-book.pdf", "wb") as file:
    file.write(pdf_data)

 

위에서 작성한 python code를 실행한다.

$ ./my-img2pdf

 

참고: 위와 같이 PDF 파일로 변환하면, 글자가 그림으로 표현되기 때문에 "문자열" 검색이 되도록 하려면 OCR을 적용해야 한다.

 

 

Step 3: PDF 파일에 OCR 적용하기

OCR 관련 소프트웨어 패키지를 설치하기

OCRmyPDF 패키지와 다국어 관련 패키지 tesseract-lang을 설치한다.

$ brew install ocrmypdf

## 영어 이외에 언어(예: 한국어, 일본어, 중국어)를 사용하려면 아래와 같이 tesseract-lang 패키지를 설치
$ brew install tesseract-lang

기존 PDF 파일을 OCR 적용된 PDF 파일로 변환하기

## "eng+kor" 옵션을 설정하여 Image에 있는 영어, 한국어를 인식하도록 함.
$ ocrmypdf -l eng+kor my-book.pdf my-book-ocr.pdf

 

Apple m2 CPU를 사용했을 경우, 만약 500 Page 분량이면 ocymypdf 명령이 완료되기 까지 10분 정도 소요된다. 

그리고 8개의 CPU Core 사용률이 모두 95% 이상이 된다.

 

자세한 내용은 아래 웹 문서를 참고;

https://github.com/ocrmypdf/OCRmyPDF

 


 

이렇게 하면, 여러 개의 그림 파일(.jpg, .png, .gif, .tiff 등)을 1개의 PDF 파일로 만들 수 있다. 나만의 eBook이 만들어지는 셈이다.

 

반응형

 

작성일: 2024년 12월 16일

 

eBook(PDF) - Learning eBPF 

https://isovalent.com/books/learning-ebpf/

 

O'Reilly book: Learning eBPF by Liz Rice

The O'Reilly book Learning eBPF by Liz Rice now available for download!

isovalent.com

 

참고: 위 책에 소개된 예제 코드는 아래 github에서 열람할 것.

https://github.com/lizrice/learning-ebpf?tab=readme-ov-file

 

GitHub - lizrice/learning-ebpf: Learning eBPF, published by O'Reilly - out now! Here's where you'll find a VM config for the exa

Learning eBPF, published by O'Reilly - out now! Here's where you'll find a VM config for the examples, and more - lizrice/learning-ebpf

github.com

 

libbpfgo-beginners

https://github.com/lizrice/libbpfgo-beginners

 

GitHub - lizrice/libbpfgo-beginners: Basic eBPF examples in Golang using libbpfgo

Basic eBPF examples in Golang using libbpfgo. Contribute to lizrice/libbpfgo-beginners development by creating an account on GitHub.

github.com

 

 

 

책 저자 - Liz Rice

https://www.lizrice.com/#6

 

Liz Rice - containers, eBPF, security, Kubernetes, software engineering

Liz Rice is a software engineer and entrepreneur based in London, UK. As Chief Open Source Officer for eBPF experts Isovalent, she travels the world speaking about containers, security and distributed systems. Her programming language of choice is Golang,

www.lizrice.com

 

 

 

 

eBPF - Official Web Site

 

https://ebpf.io/get-started/

 

eBPF - Introduction, Tutorials & Community Resources

eBPF is a revolutionary technology that can run sandboxed programs in the Linux kernel without changing kernel source code or loading a kernel module.

ebpf.io

 

 

 

 

 

 

 

 

 

 

 

 

반응형

 


작성일: 2025년 2월 22일

 

잔잔하게 음악 들으면서 글을 읽어보세요.  https://youtu.be/UKzWrVBaZ4M?si=DypTskDwy6VuByCY

 

 

 

 

수백 페이지 분량의 PDF 문서를 읽다보면, 특정 페이지 몇 장만 골라서 저장하고 싶을 때가 있다.

PDF 편집기 같은 유료 프로그램이 있다면, 원하는대로 편집해서 저장할 수 있지만

돈을 지출하지 않고 PDF 문서에서 몇 페이지만 추출하여 저장하고 싶다면,

인쇄 버튼을 누르고 추출하고 싶은 페이지 번호만 입력하고, PDF 문서로 출력하기를 선택하면 된다.

 

내 느낌인지는 모르겠지만, 이렇게 PDF 문서를 "PDF 문서로 저장"하면 약간 품질이 떨어지는 것 같다. ^^


 

+ Recent posts