Beispiel #1
0
    def _postprocess_contents(self, values):
        ICP = self.env['ir.config_parameter'].sudo().get_param
        supported_subtype = ICP('base.image_autoresize_extensions', 'png,jpeg,gif,bmp,tif').split(',')

        mimetype = values['mimetype'] = self._compute_mimetype(values)
        _type, _subtype = mimetype.split('/')
        is_image_resizable = _type == 'image' and _subtype in supported_subtype
        if is_image_resizable and (values.get('datas') or values.get('raw')):
            is_raw = values.get('raw')

            # Can be set to 0 to skip the resize
            max_resolution = ICP('base.image_autoresize_max_px', '1920x1920')
            if str2bool(max_resolution, True):
                try:
                    img = fn_quality = False
                    if is_raw:
                        img = ImageProcess(False, verify_resolution=False)
                        img.image = Image.open(io.BytesIO(values['raw']))
                        img.original_format = (img.image.format or '').upper()
                        fn_quality = img.image_quality
                    else:  # datas
                        img = ImageProcess(values['datas'], verify_resolution=False)
                        fn_quality = img.image_quality_base64

                    w, h = img.image.size
                    nw, nh = map(int, max_resolution.split('x'))
                    if w > nw or h > nh:
                        img.resize(nw, nh)
                        quality = int(ICP('base.image_autoresize_quality', 80))
                        values[is_raw and 'raw' or 'datas'] = fn_quality(quality=quality)
                except UserError as e:
                    # Catch error during test where we provide fake image
                    # raise UserError(_("This file could not be decoded as an image file. Please try with a different file."))
                    _logger.info('Post processing ignored : %s', e)
        return values
Beispiel #2
0
 def _compute_app_icon(self):
     """ Computes a squared image based on the favicon to be used as mobile webapp icon.
         App Icon should be in PNG format and size of at least 512x512.
     """
     for website in self:
         image = ImageProcess(website.favicon)
         w, h = image.image.size
         square_size = w if w > h else h
         image.crop_resize(square_size, square_size)
         image.image = image.image.resize((512, 512))
         image.operationsCount += 1
         website.app_icon = image.image_base64(output_format='PNG')