티스토리 뷰
3.14.파이카메라 미리보기에서 이미지 오버레이
카메라 미리보기 시스템은 여러 레이어 렌더러를 동시에 조작 할 수 있습니다. picamera 라이브러리는 하나의 렌더러 만 카메라의 미리보기 포트에 연결하도록 허용하지만 정적 렌더러를 표시하는 추가 렌더러를 만들 수 있습니다. 이러한 중첩 렌더러는 간단한 사용자 인터페이스를 만드는 데 사용할 수 있습니다.
참고: 오버레이 이미지는 이미지 캡처 또는 비디오 녹화에는 나타나지 않습니다. 카메라 출력에 추가 정보를 삽입해야하는 경우 출력물에 텍스트 오버레이를 참조하십시오.
오버레이 렌더러를 사용하는 한 가지 어려움은 카메라의 블록 크기까지 패딩 된 인코딩되지 않은 RGB 입력을 기대한다는 것입니다. 카메라의 블록 크기는 32x16이므로 렌더러에 제공되는 모든 이미지 데이터의 너비는 32의 배수이고 높이는 16의 배수 여야합니다. 예상되는 특정 RGB 형식은 부호없는 인터리브 바이트입니다. 이 모든 것이 복잡해 보이면 걱정하지 마십시오. 실제로 제작하는 것은 매우 간단합니다.
다음 예제에서는 임의 크기의 이미지를 PIL로로드하고 필요한 크기만큼 채우고 add_overlay () 호출에 대해 인코딩되지 않은 RGB 데이터를 생성하는 방법을 보여줍니다.
import picamera from PIL import Image from time import sleep camera = picamera.PiCamera() camera.resolution = (1280, 720) camera.framerate = 24 camera.start_preview() # Load the arbitrarily sized image img = Image.open('overlay.png') # Create an image padded to the required size with # mode 'RGB' pad = Image.new('RGB', ( ((img.size[0] + 31) // 32) * 32, ((img.size[1] + 15) // 16) * 16, )) # Paste the original image into the padded one pad.paste(img, (0, 0)) # Add the overlay with the padded image as the source, # but the original image's dimensions o = camera.add_overlay(pad.tobytes(), size=img.size) # By default, the overlay is in layer 0, beneath the # preview (which defaults to layer 2). Here we make # the new overlay semi-transparent, then move it above # the preview o.alpha = 128 o.layer = 3 # Wait indefinitely until the user terminates the script while True: sleep(1)
또는 이미지 파일을 소스로 사용하는 대신에 numpy 배열에서 직접 오버레이를 생성 할 수 있습니다. 다음 예제에서는 화면과 동일한 해상도로 numpy 배열을 만든 다음 가운데에 흰색 십자가를 그려 미리보기에서 간단한 십자형으로 오버레이합니다.
import time import picamera import numpy as np # Create an array representing a 1280x720 image of # a cross through the center of the display. The shape of # the array must be of the form (height, width, color) a = np.zeros((720, 1280, 3), dtype=np.uint8) a[360, :, :] = 0xff a[:, 640, :] = 0xff camera = picamera.PiCamera() camera.resolution = (1280, 720) camera.framerate = 24 camera.start_preview() # Add the overlay directly into layer 3 with transparency; # we can omit the size parameter of add_overlay as the # size is the same as the camera's resolution o = camera.add_overlay(np.getbuffer(a), layer=3, alpha=64) try: # Wait indefinitely until the user terminates the script while True: time.sleep(1) finally: camera.remove_overlay(o)
중첩 된 렌더러는 (기본값 인 2의 미리보기 레이어 아래로 이동하여) 숨길 수 있고 반투명하게 (알파 속성을 사용하여) 크기가 조정되어 화면을 채우지 않도록 크기를 조정할 수 있으므로 구성 가능한 렌더러를 구성하는 데 사용할 수 있습니다 간단한 사용자 인터페이스.