Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def _update_foreground(self, pil_image, pos=CENTER, resize=True):
"""Show a PIL image on the foreground.
Only once is bufferized to avoid memory leak.
"""
image_name = id(pil_image)
image_size_max = (2 * self.surface.get_size()[1] // 3, self.surface.get_size()[1])
buff_size, buff_image = self._buffered_images.get(image_name, (None, None))
if buff_image and image_size_max == buff_size:
image = buff_image
else:
if resize:
image = pil_image.resize(sizing.new_size_keep_aspect_ratio(
pil_image.size, image_size_max), Image.ANTIALIAS)
else:
image = pil_image
image = pygame.image.frombuffer(image.tobytes(), image.size, image.mode)
if self._current_foreground:
self._buffered_images.pop(id(self._current_foreground[0]), None)
LOGGER.debug("Add to buffer the image '%s'", image_name)
self._buffered_images[image_name] = (image_size_max, image)
self._current_foreground = (pil_image, pos, resize)
return self.surface.blit(image, self._pos_map[pos](image))
def concatenate_pictures_PIL(portrait, pictures, footer_texts, bg_color, text_color, footer_fonts, inter_width=None):
""" Merge up to 4 PIL images.
"""
with timeit("Create final image with PIL"):
new_width, new_height, inter_width = get_pics_layout_size(pictures, portrait, inter_width)
matrix = Image.new('RGBA', (new_width, new_height))
# Consider that the photo are correctly ordered
offset_generator = get_pics_layout_offset(pictures, portrait, inter_width)
for i in range(len(pictures)):
matrix.paste(pictures[i], next(offset_generator))
final_width, final_height, matrix_width, matrix_height, footer_size = get_final_image_dimensions(
portrait, footer_texts)
matrix = matrix.resize(sizing.new_size_keep_aspect_ratio(
matrix.size, (matrix_width, matrix_height)), Image.ANTIALIAS)
final_image = new_image_with_background(final_width, final_height, bg_color)
final_image.paste(matrix, ((final_width - matrix.size[0]) // 2,
(final_height - footer_size - matrix.size[1]) // 2), mask=matrix)
if footer_size:
draw_footer_text(final_image, portrait, footer_texts, footer_fonts, footer_size, text_color)
return final_image
def get_rect(self):
"""Return a Rect object (as defined in pygame) for resizing preview and images
in order to fit to the defined window.
"""
rect = self._window.get_rect()
res = sizing.new_size_keep_aspect_ratio(self.resolution,
(rect.width - 2 * self._border, rect.height - 2 * self._border))
return pygame.Rect(rect.centerx - res[0] // 2, rect.centery - res[1] // 2, res[0], res[1])
def _image_resize_keep_ratio(self, image, max_w, max_h, crop=False):
"""See upper class description.
"""
if crop:
width, height = sizing.new_size_keep_aspect_ratio(image.size, (max_w, max_h), 'outer')
image = image.resize((width, height), Image.ANTIALIAS)
image = image.crop(sizing.new_size_by_croping(image.size, (max_w, max_h)))
else:
width, height = sizing.new_size_keep_aspect_ratio(image.size, (max_w, max_h), 'inner')
image = image.resize((width, height), Image.ANTIALIAS)
return image, image.size[0], image.size[1]
def _post_process_capture(self, capture_data):
"""Rework capture data.
:param capture_data: couple (frame, effect)
:type capture_data: tuple
"""
frame, effect = capture_data
image = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
# Crop to keep aspect ratio of the resolution
height, width = image.shape[:2]
cropped = sizing.new_size_by_croping_ratio((width, height), self.resolution)
image = image[cropped[1]:cropped[3], cropped[0]:cropped[2]]
# Resize to fit the resolution
height, width = image.shape[:2]
size = sizing.new_size_keep_aspect_ratio((width, height), self.resolution, 'outer')
image = cv2.resize(image, size, interpolation=cv2.INTER_AREA)
if self._capture_hflip:
image = cv2.flip(image, 1)
if effect != 'none':
LOGGER.warning("Effect with OpenCV camera is not implemented")
return Image.fromarray(image)
def _get_preview_image(self):
"""Capture a new preview image.
"""
rect = self.get_rect()
ret, image = self._cam.read()
if not ret:
raise IOError("Can not get camera preview image")
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
# Crop to keep aspect ratio of the resolution
height, width = image.shape[:2]
cropped = sizing.new_size_by_croping_ratio((width, height), self.resolution)
image = image[cropped[1]:cropped[3], cropped[0]:cropped[2]]
# Resize to fit the available space in the window
height, width = image.shape[:2]
size = sizing.new_size_keep_aspect_ratio((width, height), (rect.width, rect.height), 'outer')
image = cv2.resize(image, size, interpolation=cv2.INTER_AREA)
if self._preview_hflip:
image = cv2.flip(image, 1)
if self._overlay is not None:
image = cv2.addWeighted(image, 1, self._overlay, self._overlay_alpha / 255., 0)
return Image.fromarray(image)
:rtype: object
"""
path = get_filename(name)
if not size and not color:
image = pygame.image.load(path).convert()
else:
if osp.isfile(path):
pil_image = Image.open(path)
else:
pil_image = Image.new('RGBA', size, (0, 0, 0, 0))
if color:
pil_image = colorize_pil_image(pil_image, color, bg_color)
if crop:
pil_image = pil_image.crop(sizing.new_size_by_croping_ratio(pil_image.size, size))
pil_image = pil_image.resize(sizing.new_size_keep_aspect_ratio(pil_image.size, size),
Image.ANTIALIAS if antialiasing else Image.NEAREST)
image = pygame.image.frombuffer(pil_image.tobytes(), pil_image.size, pil_image.mode)
if hflip or vflip:
image = pygame.transform.flip(image, hflip, vflip)
if angle != 0:
image = pygame.transform.rotate(image, angle)
return image
def _get_preview_image(self):
"""Capture a new preview image.
"""
rect = self.get_rect()
if self._preview_compatible:
cam_file = self._cam.capture_preview()
image = Image.open(io.BytesIO(cam_file.get_data_and_size()))
# Crop to keep aspect ratio of the resolution
image = image.crop(sizing.new_size_by_croping_ratio(image.size, self.resolution))
# Resize to fit the available space in the window
image = image.resize(sizing.new_size_keep_aspect_ratio(image.size, (rect.width, rect.height), 'outer'))
if self._preview_hflip:
image = image.transpose(Image.FLIP_LEFT_RIGHT)
else:
image = Image.new('RGB', (rect.width, rect.height), color=(0, 0, 0))
if self._overlay:
image.paste(self._overlay, (0, 0), self._overlay)
return image
target_aspect_ratio = float(max_w) / max_h
if crop:
if source_aspect_ratio <= target_aspect_ratio:
h_cropped = int(width / target_aspect_ratio)
x_offset = 0
y_offset = int((float(height) - h_cropped) / 2)
cropped = image[y_offset:(y_offset + h_cropped), x_offset:width]
else:
w_cropped = int(height * target_aspect_ratio)
x_offset = int((float(width) - w_cropped) / 2)
y_offset = 0
cropped = image[y_offset:height, x_offset:(x_offset + w_cropped)]
image = cv2.resize(cropped, (max_w, max_h), interpolation=inter)
else:
width, height = sizing.new_size_keep_aspect_ratio((width, height), (max_w, max_h), 'inner')
image = cv2.resize(image, (width, height), interpolation=cv2.INTER_AREA)
return image, image.shape[1], image.shape[0]
def new_image_with_background(width, height, background):
"""Create a new image with the given background. The background can be
a RGB color tuple or a PIL image.
"""
if isinstance(background, (tuple, list)):
return Image.new('RGB', (width, height), color=background)
else:
image = Image.new('RGB', (width, height))
image.paste(background.resize(sizing.new_size_keep_aspect_ratio(background.size, image.size, 'outer')))
return image