def test_load_as_pil(self): img_path = self.temp_file_name("path1.png") make_test_img(img_path) img = load_as_pil(img_path) assert isinstance(img, Image.Image) exr_path = get_test_exr() img = load_as_pil(exr_path) assert isinstance(img, Image.Image)
def _update_preview(self, new_chunk_file_path, num_start): img = load_as_pil(new_chunk_file_path) img_current = self._open_preview() img_current = ImageChops.add(img_current, img) img_current.save(self.preview_file_path, "BMP") img.close()
def _update_frame_preview(self, new_chunk_file_path, frame_num, part=1, final=False): num = self.frames.index(frame_num) preview_task_file_path = self._get_preview_task_file_path(num) with handle_image_error(logger), \ handle_none(load_as_pil(new_chunk_file_path), raise_if_none=IOError("load_as_pil failed")) as img: def resize_and_save(img): img_x, img_y = img.size with img.resize((int(round(self.scale_factor * img_x)), int(round(self.scale_factor * img_y))), resample=Image.BILINEAR) as img_resized: img_resized.save(self._get_preview_file_path(num), PREVIEW_EXT) img_resized.save(preview_task_file_path, PREVIEW_EXT) if not final: with self._paste_new_chunk( img, self._get_preview_file_path(num), part, int(self.total_tasks / len(self.frames))) as img_pasted: resize_and_save(img_pasted) else: resize_and_save(img) self.last_preview_path = preview_task_file_path
def update_preview(self, subtask_path, subtask_number): if subtask_number not in self.chunks: self.chunks[subtask_number] = subtask_path try: img = load_as_pil(subtask_path) offset = self.get_offset(subtask_number) if subtask_number == self.perfectly_placed_subtasks + 1: _, img_y = img.size self.perfect_match_area_y += img_y self.perfectly_placed_subtasks += 1 # this is the last task if subtask_number + 1 >= len(self.expected_offsets): height = self.preview_res_y - \ self.expected_offsets[subtask_number] else: height = self.expected_offsets[subtask_number + 1] - \ self.expected_offsets[subtask_number] img = img.resize((self.preview_res_x, height), resample=Image.BILINEAR) if not os.path.exists(self.preview_file_path) \ or len(self.chunks) == 1: img_offset = Image.new( "RGB", (self.preview_res_x, self.preview_res_y)) img_offset.paste(img, (0, offset)) img_offset.save(self.preview_file_path, "BMP") img_offset.close() else: img_current = Image.open(self.preview_file_path) img_current.paste(img, (0, offset)) img_current.save(self.preview_file_path, "BMP") img_current.close() img.close() except Exception: logger.exception("Error in Blender update preview:") return self.last_update_time = time.time() if subtask_number == self.perfectly_placed_subtasks and \ (subtask_number + 1) in self.chunks: self.update_preview(self.chunks[subtask_number + 1], subtask_number + 1)
def update_preview(self, subtask_path, subtask_number): if subtask_number not in self.chunks: self.chunks[subtask_number] = subtask_path with handle_image_error(logger) as handler_result, \ handle_none(load_as_pil(subtask_path), raise_if_none=IOError("load_as_pil failed")) \ as subtask_img: offset = self.get_offset(subtask_number) if subtask_number == self.perfectly_placed_subtasks + 1: _, img_y = subtask_img.size self.perfect_match_area_y += img_y self.perfectly_placed_subtasks += 1 # this is the last task if subtask_number + 1 >= len(self.expected_offsets): height = self.preview_res_y - \ self.expected_offsets[subtask_number] else: height = self.expected_offsets[subtask_number + 1] - \ self.expected_offsets[subtask_number] with subtask_img.resize((self.preview_res_x, height), resample=Image.BILINEAR) \ as subtask_img_resized: def open_or_create_image(): if not os.path.exists(self.preview_file_path) \ or len(self.chunks) == 1: return Image.new( "RGB", (self.preview_res_x, self.preview_res_y)) return Image.open(self.preview_file_path) with open_or_create_image() as preview_img: preview_img.paste(subtask_img_resized, (0, offset)) preview_img.save(self.preview_file_path, PREVIEW_EXT) if not handler_result.success: return if subtask_number == self.perfectly_placed_subtasks and \ (subtask_number + 1) in self.chunks: self.update_preview(self.chunks[subtask_number + 1], subtask_number + 1)
def _update_frame_preview(self, new_chunk_file_path, frame_num, part=1, final=False): num = self.frames.index(frame_num) if final: img = load_as_pil(new_chunk_file_path) scaled = img.resize((int(round(self.res_x * self.scale_factor)), int(round(self.res_y * self.scale_factor))), resample=Image.BILINEAR) scaled.save(self._get_preview_file_path(num), PREVIEW_EXT) scaled.save(self._get_preview_task_file_path(num), PREVIEW_EXT) scaled.close() img.close() else: self.preview_updaters[num].update_preview(new_chunk_file_path, part) self._update_frame_task_preview()
def _update_frame_preview(self, new_chunk_file_path, frame_num, part=1, final=False): num = self.frames.index(frame_num) img = load_as_pil(new_chunk_file_path) if not final: img = self._paste_new_chunk( img, self._get_preview_file_path(num), part, int(self.total_tasks / len(self.frames))) img_x, img_y = img.size img = img.resize((int(round(self.scale_factor * img_x)), int(round(self.scale_factor * img_y))), resample=Image.BILINEAR) img.save(self._get_preview_file_path(num), "BMP") img.save(self._get_preview_task_file_path(num), "BMP") img.close()
def _update_frame_preview(self, new_chunk_file_path, frame_num, part=1, final=False): num = self.frames.index(frame_num) if final: with handle_image_error(logger), \ handle_none(load_as_pil(new_chunk_file_path), raise_if_none=IOError("load_as_pil failed")) \ as img, \ img.resize((int(round(self.res_x * self.scale_factor)), int(round(self.res_y * self.scale_factor))), resample=Image.BILINEAR) as scaled: preview_task_file_path = self._get_preview_task_file_path(num) self.last_preview_path = preview_task_file_path scaled.save(preview_task_file_path, PREVIEW_EXT) scaled.save(self._get_preview_file_path(num), PREVIEW_EXT) else: self.preview_updaters[num].update_preview(new_chunk_file_path, part) self._update_frame_task_preview()
def _update_preview(self, new_chunk_file_path, num_start): with handle_none(load_as_pil(new_chunk_file_path), raise_if_none=IOError("load_as_pil failed")) as img, \ self._open_preview() as img_current, \ ImageChops.add(img_current, img) as img_added: img_added.save(self.preview_file_path, PREVIEW_EXT)