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
Exemple #2
0
    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)
Exemple #3
0
    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()
Exemple #4
0
 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)
Exemple #5
0
 def test_raise(self):
     with self.assertRaises(RuntimeError):
         with handle_none(None, raise_if_none=RuntimeError("fail")):
             # this should not be executed
             assert False
Exemple #6
0
 def test_none(self):
     with handle_none(None) as inner_context_manager:
         assert inner_context_manager is None
Exemple #7
0
 def test_correct(self):
     context_manager = self.ExampleContextManager()
     with handle_none(context_manager) as inner_context_manager:
         assert inner_context_manager is context_manager