def test_image_info_svg_length(make_svg): w, h = 100, 100 svg_with_unit_px = make_svg(size_unit="px", w=w, h=h) svg_no_unit = make_svg(size_unit=None, w=w, h=h) info_with_unit_px = get_image_info(svg_with_unit_px) info_no_unit = get_image_info(svg_no_unit) expected = "svg", 100, 100 assert info_with_unit_px == expected assert info_no_unit == expected
def test_image_info_svg_declaration(make_svg): w, h = 100, 100 svg_with_xml_decl = make_svg(with_declaration=True, h=h, w=w) svg_no_xml_decl = make_svg(with_declaration=False, h=h, w=w) info_svg_no_xml_decl = get_image_info(svg_no_xml_decl) info_svg_with_xml_decl = get_image_info(svg_with_xml_decl) expected = "svg", w, h assert info_svg_with_xml_decl == expected assert info_svg_no_xml_decl == expected
def test_thumbnail_dimensions_real(builder): builder.build_all() for t, dimensions in _THUMBNAILS.items(): image_file = os.path.join(builder.destination_path, t) with open(image_file, "rb") as f: _format, width, height = get_image_info(f) assert (width, height) == dimensions
def build_artifact(self, artifact): ctx = get_ctx() plugin = ctx.env.plugins["thumbnail-generator"] config = plugin.config artifact.ensure_dir() AttachmentBuildProgram.build_artifact(self, artifact) if not config: return source_img = artifact.source_obj.attachment_filename with open(source_img, "rb") as f: _, w, h = get_image_info(f) # For every section in the config, we need to generate one image. for item, conf in config.items(): width = int(conf["max_width"]) height = int(conf.get("max_height", "0")) if not height: _, height = compute_dimensions(width, None, w, h) df = artifact.source_obj.url_path ext_pos = df.rfind(".") dst_filename = "%s-%s.%s" % (df[:ext_pos], item, df[ext_pos + 1:]) def closure(dst_filename, source_img, width, height, resize_image=True): # We need this closure, otherwise variables get updated and this # doesn't work at all. @ctx.sub_artifact(artifact_name=dst_filename, sources=[source_img]) def build_thumbnail_artifact(artifact): artifact.ensure_dir() if not resize_image: shutil.copy2(source_img, artifact.dst_filename) else: process_image( ctx, source_img, artifact.dst_filename, width, height, quality=85, extra_params=[ "-strip", "-interlace", "Plane", ], ) # If the image is larger than the max_width, resize it, otherwise # just copy it. resize_image = w > width or h > height closure(dst_filename, source_img, width, height, resize_image)
def _get_image_info(self): rv = getattr(self, '_image_info', None) if rv is None: with open(self.attachment_filename, 'rb') as f: self._image_info = rv = get_image_info(f) return rv
def _get_image_info(self): rv = getattr(self, '_image_info', None) if rv is None: with open(self.attachment_filename, 'rb') as f: rv = self._image_info = get_image_info(f) return rv