def parse_geometry(geometry, ratio=None): """ Parses a geometry string syntax and returns a (width, height) tuple """ m = geometry_pat.match(geometry) def syntax_error(): return ThumbnailParseError('Geometry does not have the correct ' 'syntax: %s' % geometry) if not m: raise syntax_error() x = m.group('x') y = m.group('y') if x is None and y is None: raise syntax_error() if x is not None: x = int(x) if y is not None: y = int(y) # calculate x or y proportionally if not set but we need the image ratio # for this if ratio is not None: ratio = float(ratio) if x is None: x = toint(y * ratio) elif y is None: y = toint(x / ratio) return x, y
def parse_geometry(geometry, ratio=None, options=None): """ Parses a geometry string syntax and returns a (width, height) tuple """ m = geometry_pat.match(geometry) def syntax_error(): return ThumbnailParseError("Geometry does not have the correct syntax: %s" % geometry) if not m: raise syntax_error() x = m.group("x") y = m.group("y") if x is None and y is None: raise syntax_error() if x is not None: x = int(x) if y is not None: y = int(y) # calculate x or y proportionally if not set but we need the image ratio # for this if options and ratio is not None: ratio = float(ratio) if x is None or (ratio < 1.0 and options.get("crop", "noop") == "noop"): x = toint(y * ratio) elif y is None or (ratio > 1.0 and options.get("crop", "noop") == "noop"): y = toint(x / ratio) return x, y
def scale(self, image, geometry, options): """ Wrapper for ``_scale`` """ upscale = options['upscale'] x_image, y_image = map(float, self.get_image_size(image)) factor = self._calculate_scaling_factor(x_image, y_image, geometry, options) if factor < 1 or upscale: width = toint(x_image * factor) height = toint(y_image * factor) image = self._scale(image, width, height) return image
def scale(self, image, geometry, options): """ Wrapper for ``_scale`` """ upscale = options['upscale'] x_image, y_image = map(float, self.get_image_size(image)) # calculate scaling factor factors = (geometry[0] / x_image, geometry[1] / y_image) factor = max(factors) if geometry[0] > geometry[1] else min(factors) if factor < 1 or upscale: width = toint(x_image * factor) height = toint(y_image * factor) image = self._scale(image, width, height) return image
def scale(self, image, geometry, options): """ Wrapper for ``_scale`` """ crop = options['crop'] upscale = options['upscale'] x_image, y_image = map(float, self.get_image_size(image)) # calculate scaling factor factors = (geometry[0] / x_image, geometry[1] / y_image) factor = max(factors) if crop else min(factors) if factor < 1 or upscale: width = toint(x_image * factor) height = toint(y_image * factor) image = self._scale(image, width, height) return image
def _get_dummy_image_data(self, width, height): """ Returns useful data for subclass ``dummy_image`` method """ w = min(width, height) / 2.0 x0 = toint(0.5 * (width - w)) y0 = toint(0.5 * (height - w)) x1 = toint(0.5 * (width + w)) y1 = toint(0.5 * (height + w)) return { 'canvas_color': (255, 255, 255), 'line_color': (200, 200, 200), 'lines': [(x0, y0, x1, y1), (x0, y1, x1, y0)], 'rectangle': (0, 0, width - 1, height -1), }
def _get_dummy_image_data(self, width, height): """ Returns useful data for subclass ``dummy_image`` method """ w = min(width, height) / 2.0 x0 = toint(0.5 * (width - w)) y0 = toint(0.5 * (height - w)) x1 = toint(0.5 * (width + w)) y1 = toint(0.5 * (height + w)) return { 'canvas_color': (255, 255, 255), 'line_color': (200, 200, 200), 'lines': [(x0, y0, x1, y1), (x0, y1, x1, y0)], 'rectangle': (0, 0, width - 1, height - 1), }
def _create_alternative_resolutions(self, source, geometry_string, options, name): """ Creates the thumbnail by using default.engine with multiple output sizes. Appends @<ratio>x to the file name. """ if not options['alternative_resolutions']: return source_image = default.engine.get_image(source) ratio = default.engine.get_image_ratio(source_image, options) geometry_orig = parse_geometry(geometry_string, ratio) file_type = name.split('.')[len(name.split('.'))-1] for res in options['alternative_resolutions']: geometry = (toint(geometry_orig[0]*res), toint(geometry_orig[1]*res)) thumbnail_name = name.replace(".%s" % file_type, "@%sx.%s" % (res, file_type)) source_image = default.engine.get_image(source) image = default.engine.create(source_image, geometry, options) thumbnail = ImageFile(thumbnail_name, default.storage) default.engine.write(image, options, thumbnail) size = default.engine.get_image_size(image) thumbnail.set_size(size)