def test_load_img(self): exr_img = load_img(get_test_exr()) assert isinstance(exr_img, EXRImgRepr) assert exr_img.get_size() == (10, 10) img_path = self.temp_file_name("img.jpg") pil_img = get_pil_img_repr(img_path) assert isinstance(pil_img, PILImgRepr) assert pil_img.get_size() == (10, 10) assert load_img("notexisting") is None
def test_put_collected_files_together(self): output_name = self.temp_file_name("output.exr") exr1 = _get_test_exr() exr2 = _get_test_exr(alt=True) assert path.isfile(exr1) assert path.isfile(exr2) assert load_img(output_name) is None self.task.res_x = 10 self.task.res_y = 20 self.task._put_collected_files_together(output_name, [exr1, exr2], "paste") assert load_img(output_name) is not None
def test_put_img_together_exr_to_exr(self): self.bt.output_format = "EXR" self.bt.output_file += ".EXR" for chunks in [1, 5, 7, 11, 13, 31, 57]: res_y = 0 self.bt.collected_file_names = {} for i in range(1, chunks + 1): # Subtask numbers start from 1. y = randrange(1, 100) res_y += y file1 = self.temp_file_name('chunk{}.exr'.format(i)) exr = OpenEXR.OutputFile(file1, OpenEXR.Header(self.bt.res_x, y)) data = array.array('f', [1.0] * (self.bt.res_x * y)).tostring() exr.writePixels({ 'R': data, 'G': data, 'B': data, 'F': data, 'A': data }) exr.close() self.bt.collected_file_names[i] = file1 self.bt.res_y = res_y self.bt._put_image_together() self.assertTrue(path.isfile(self.bt.output_file)) img = load_img(self.bt.output_file) img_x, img_y = img.get_size() self.assertTrue(self.bt.res_x == img_x and res_y == img_y)
def check_size(file_, res_x, res_y): # allow +/-1 difference in y size - workaround for blender inproperly rounding floats img = load_img(file_) if img is None: return False img_x, img_y = img.get_size() return (img_x == res_x) and (abs(img_y - res_y) <= 1)
def _update_preview_from_exr(self, new_chunk_file): if self.preview_exr is None: self.preview_exr = load_img(new_chunk_file) else: self.preview_exr = blend(self.preview_exr, load_img(new_chunk_file), 1.0 / self.num_add) img_current = self._open_preview() img = self.preview_exr.to_pil() scaled = ImageOps.fit(img, (int(round(self.scale_factor * self.res_x)), int(round(self.scale_factor * self.res_y))), method=Image.BILINEAR) scaled.save(self.preview_file_path, PREVIEW_EXT) img.close() scaled.close() img_current.close()
def test_finalize_alpha(self): collector = RenderingTaskCollector() collector.add_alpha_file(_get_test_exr()) collector.add_alpha_file(_get_test_exr(alt=True)) img_path = self.temp_file_name("img1.png") make_test_img(img_path) img_repr = load_img(img_path) collector.finalize_alpha(img_repr.img)
def test_put_image_together(self): task = self._get_frame_task(False) task.output_format = "exr" task.output_file = self.temp_file_name("output.exr") task.res_x = 10 task.res_y = 20 exr_1 = Path(__file__).parent.parent.parent / "rendering" / "resources" / "testfile.EXR" exr_2 = Path(__file__).parent.parent.parent / "rendering" / "resources" / "testfile2.EXR" task.collected_file_names["abc"] = str(exr_1) task.collected_file_names["def"] = str(exr_2) task._put_image_together() img_repr = load_img(task.output_file) assert isinstance(img_repr, EXRImgRepr)
def advance_verify_img(file_, res_x, res_y, start_box, box_size, compare_file, cmp_start_box): try: img = load_img(file_) cmp_img = load_img(compare_file) if img is None or cmp_img is None: return False if img.get_size() != (res_x, res_y): return False def _box_too_small(box): return box[0] <= 0 or box[1] <= 0 def _box_too_big(box): return box[0] > res_x or box[1] > res_y if _box_too_small(box_size) or _box_too_big(box_size): logger.error("Wrong box size for advanced verification " "{}".format(box_size)) if isinstance(img, PILImgRepr) and isinstance(cmp_img, PILImgRepr): return compare_imgs(img, cmp_img, start1=start_box, start2=cmp_start_box, box=box_size) else: return compare_imgs(img, cmp_img, max_col=1, start1=start_box, start2=cmp_start_box, box=box_size) except Exception: logger.exception("Cannot verify images {} and {}".format( file_, compare_file)) return False
def finalize(self): """ Connect all collected files and return final image :return Image.Image: """ if len(self.accepted_img_files) == 0: return None img_repr = load_img(self.accepted_img_files[0]) if isinstance(img_repr, EXRImgRepr): final_img = self.finalize_exr(img_repr) self.finalize_alpha(final_img) else: final_img = self.finalize_pil() return final_img
def test_put_frame_together(self): task = self._get_frame_task(True) task.output_format = "exr" task.outfilebasename = "output" task.output_file = self.temp_file_name("output.exr") task.frames = [3, 5] task.total_tasks = 4 task.res_x = 10 task.res_y = 20 exr_1 = Path(__file__).parent.parent.parent / "rendering" / "resources" / "testfile.EXR" exr_2 = Path(__file__).parent.parent.parent / "rendering" / "resources" / "testfile2.EXR" task.frames_given["5"] = {"abc": str(exr_1), "def": str(exr_2)} task._put_frame_together(5, 1) out_path = os.path.join(self.path, "output0005.exr") img_repr = load_img(out_path) assert isinstance(img_repr, EXRImgRepr)
def test_compare_pil_imgs(self): img_path1 = self.temp_file_name("img1.jpg") img_path2 = self.temp_file_name("img2.jpg") make_test_img(img_path1) make_test_img(img_path2) assert compare_pil_imgs(img_path1, img_path2) img = load_img(img_path1) img.set_pixel((0, 0), [253, 1, 1]) img.img.save(img_path1) assert compare_pil_imgs(img_path1, img_path2) make_test_img(img_path1, color=(200, 0, 0)) assert not compare_pil_imgs(img_path1, img_path2) make_test_img(img_path1, size=[11, 10], color=(255, 0, 0)) with self.assertLogs(logger, level="INFO"): assert not compare_pil_imgs(img_path1, img_path2) with self.assertLogs(logger, level="INFO"): assert not compare_pil_imgs(img_path2, get_test_exr())
def finalize_exr(self, exr_repr): final_img = exr_repr.to_pil() if self.paste: if not self.width or not self.height: self.width, self.height = final_img.size self.height *= len(self.accepted_img_files) img = Image.new('RGB', (self.width, self.height)) final_img = self._paste_image(img, final_img, 0) img.close() for i, img_path in enumerate(self.accepted_img_files[1:], start=1): img = load_img(img_path) rgb8_im = img.to_pil() if not self.paste: final_img = ImageChops.add(final_img, rgb8_im) else: final_img = self._paste_image(final_img, rgb8_im, i) rgb8_im.close() return final_img
def check_size(file_, res_x, res_y): img = load_img(file_) if img is None: return False return img.get_size() == (res_x, res_y)