python pillow 라이브러리 사용
라즈베리 파이에서 이미지를 출력하는 방법은 여러 가지가 있습니다. 여기에서는 Python과 Pillow(PIL) 라이브러리를 사용하여 이미지를 출력하는 간단한 예제를 제시하겠습니다. 이전에 설명한 USB 프린터 또는 네트워크 프린터가 이미 라즈베리 파이에 연결되어 있다고 가정합니다.
Pillow(PIL) 라이브러리 설치:
bashCopy code
sudo pip3 install pillow
Python 스크립트 작성: 다음은 이미지를 출력하는 간단한 Python 스크립트의 예제입니다. 스크립트를 작성하고 저장합니다.
pythonCopy code
from PIL import Image, ImageDraw, ImageFont
# 이미지 크기 및 배경 색상 설정
width, height = 300, 200
background_color = "white"
# 이미지 생성
image = Image.new("RGB", (width, height), background_color)
draw = ImageDraw.Draw(image)
# 텍스트 추가 (예: "Hello, Raspberry Pi!")
text = "Hello, Raspberry Pi!"
font = ImageFont.load_default()
text_width, text_height = draw.textsize(text, font)
text_position = ((width - text_width) // 2, (height - text_height) // 2)
draw.text(text_position, text, fill="black", font=font)
# 이미지 저장 (선택 사항)
image.save("output_image.png")
# 프린터 출력
printer_name = "your_printer_name" # CUPS에서 설정한 프린터 이름
image.show() # 이미지를 먼저 표시
image.print(printer_name)
참고: 스크립트 내에서 **your_printer_name
**을 실제 프린터의 이름으로 변경해야 합니다.
스크립트 실행:
bashCopy code
python3 your_script_name.py
**your_script_name.py
**는 작성한 Python 스크립트의 파일 이름입니다.
이 스크립트는 라즈베리 파이에서 이미지를 생성하고 프린터로 출력하는 간단한 예제입니다. Pillow(PIL) 라이브러리는 이미지 처리를 위한 강력한 도구이며, 위의 스크립트를 수정하여 다양한 이미지 출력 작업을 수행할 수 있습니다.
[라즈베리파이 + 프린터 [참고 자료 2]](https://oceancoding.blogspot.com/2020/02/pdf.html)