def _parse_args(self, **kwargs): """ Parse all params and set object attributes. Priority of configs: __init__ argument > config for class > class attribute > default config """ default_config = self.flat_dict(deepcopy(safeget(render_config, self._render_config_default_key)) or {}) class_config = self.flat_dict(deepcopy(safeget(render_config, self._render_config_key)) or {}) # update by class variables default_config.update(self._get_self_attributes()) # update by secondary config for this class if self._render_config_secondary_key: class_secondary_config = self.flat_dict(deepcopy(safeget(render_config, self._render_config_secondary_key)) or {}) default_config.update(class_secondary_config) # update by config for this class default_config.update(class_config) # update by params passed to __init__ default_config.update(kwargs) # get rid of protected attributes default_config = dict((k, default_config[k]) for k in default_config if not k.startswith('_')) # set attributes values for (key, value) in default_config.items(): setattr(self, key, value) return default_config
def pt_to_px(points): """ Converts points to pixels. """ global ppi if not ppi: ppi = int(safeget(render_config, "render.ppi", 75)) / 75.0 return int(points * ppi)
def px_to_pt(points): """ Converts pixels to points. """ global ppi_inv if not ppi_inv: ppi_inv = 75.0 / int(safeget(render_config, "render.ppi", 75)) return float(points) * ppi_inv
def generate(self, data, input_path, output_dir, overwrite=False, *args, **kwargs): """ Generate png image with given data and configuration. Args: data: Kwargs: font_type (str): Name of font type, ex. 'FreeMono'. size (int): Font size in points. typeface (str): Font typeface, ex. 'bold italic'. Returns: Image. Rendered image. """ View.renderer = "to_png" default_png_config = { 'ppi': 300, 'scale': 1 } png_config = safeget(render_config, "png", default_png_config) render_config["render"] = png_config image = super(PNGBuilder, self).generate(data, input_path, output_dir) result = [] output_file_path = "" current_time_str = datetime.now().strftime("%Y%m%d%H%M%S") # save all generated images for (file_nr, img) in enumerate(image): img_name, raw_image = img.get_image_with_name() # scale ratio = png_config.get("scale") if ratio: ratio = float(ratio) assert 0 < ratio, "Ratio should be greater then 0." if ratio != 1: new_dimensions = tuple([int(x * ratio) for x in raw_image.size]) raw_image = raw_image.resize(new_dimensions, Image.ANTIALIAS) if output_dir: if not img_name: img_name = "sdgen {0} {1}".format(current_time_str, file_nr) file_name = img_name.lower().replace(' ', '_') + '.png' output_file_path = os.path.join(output_dir, file_name) if not overwrite and os.path.exists(output_file_path): raise IOError("File %s exists" % output_file_path) raw_image.save(output_file_path) result.append((img_name, raw_image)) return result
def get_font_path(font_name, style="Regular"): """ Search for passed font in directories from config If style has more than one word, it can be passed with any order. """ global fonts_paths lower_font_name = font_name.strip().lower().replace(' ', '') splitted_font_name = font_name.strip().lower().split() lower_style = style.lower().strip() # possible style names (lowercased and un-spaced) lower_styles = _get_all_styles(lower_style) # font id, consisted of lower font name and sorted (by splitted name) lower font style font_id = (lower_font_name, lower_styles[0]) # check for patch in "cache" if font_id in fonts_paths: return fonts_paths[font_id] # check for fonts, that has font_name (lowercased) in filename (lowercased) paths = [] for directory in safeget(config, 'fonts.directories', []): for (dirpath, dirnames, filenames) in os.walk(directory): files = filter(lambda filename: any(spl in filename.lower() for spl in splitted_font_name), filenames) paths.extend([os.path.join(dirpath, dirnames[0] if dirnames else '', file_) for file_ in files]) # load candidate fonts and check if it's name and style match paths = list(set(paths)) # unique proper_path = None for font_path in paths: # load font f = ImageFont.truetype(font_path, 10) # size can be any # lowercase and un-space font name and style imgfont_family = f.font.family.lower().replace(' ', '') imgfont_style = f.font.style.lower().replace(' ', '') # check for match if imgfont_family == lower_font_name and imgfont_style in lower_styles: proper_path = font_path break # save font path in cache fonts_paths[font_id] = proper_path return proper_path