def __call__( self, fieldname=None, direction='thumbnail', height=None, width=None, scale=None, **parameters ): storage = Storage(self.context) self.box = storage.read(fieldname, scale) if self.box: direction = 'down' else: registry = getUtility(IRegistry) settings = registry.forInterface(ISettings) if scale in settings.cropping_for: direction = 'down' return super(CroppingImageScalingFactory, self).__call__( fieldname=fieldname, direction=direction, height=height, width=width, scale=scale, **parameters )
def __call__(self, **kw): form = self.request.form fieldname = form['fieldname'] scale_id = form['scale'] if 'remove' in form: storage = Storage(self.context) storage.remove(fieldname, scale_id) return 'OK' box = ( int(round(float(form['x']))), int(round(float(form['y']))), int(round(float(form['x']) + float(form['width']))), int(round(float(form['y']) + float(form['height']))), ) self._crop(fieldname, scale_id, box) return 'OK'
def _scale_info(self, fieldname, scale_id, target_size, true_size): scale = dict() scale['id'] = scale_id scale['title'] = scale_id # scale config min_width, min_height = self._min_size(true_size, target_size) # lookup saved crop info storage = Storage(self.context) current_box = storage.read(fieldname, scale_id) if current_box is None: current_box = self._initial_size(true_size, target_size) scale['is_cropped'] = False else: scale['is_cropped'] = True # images original dimensions scale['true_width'] = true_size[0] scale['true_height'] = true_size[1] # images target dimensions scale['target_width'] = target_size[0] scale['target_height'] = target_size[1] # current selected crop scale['current'] = { 'x': current_box[0], 'y': current_box[1], 'w': current_box[2] - current_box[0], 'h': current_box[3] - current_box[1], } scale['aspect_ratio'] = '1.777778' # 16:9 if target_size[0] > 0 and target_size[1] > 0: scale['aspect_ratio'] = '{0:.2f}'.format( float(target_size[0]) / float(target_size[1]) ) scale['can_scale'] = ( target_size[0] <= true_size[0] and target_size[1] <= true_size[1] ) return scale
def _crop(self, fieldname, scale, box): """Delegate to store. """ storage = Storage(self.context) storage.store(fieldname, scale, box) if IImageCroppingDX.providedBy(self.context): return # AT BBB scaling. croputils = IImageCroppingUtils(self.context) data = croputils.get_image_data(fieldname) original_file = BytesIO(data) image = PIL.Image.open(original_file) image_format = image.format or self.DEFAULT_FORMAT cropped_image = image.crop(box) cropped_image_file = BytesIO() cropped_image.save(cropped_image_file, image_format, quality=100) cropped_image_file.seek(0) croputils.save_cropped(fieldname, scale, cropped_image_file)