Example #1
0
 def _default_image(self):
     '''Method to get default Image'''
     image_path = get_module_resource('hr', 'static/src/img',
                                      'default_image.png')
     image_process = ImageProcess(
         base64.b64encode(open(image_path, 'rb').read()))
     return image_process.resize(max_width=400,
                                 max_height=600).image_base64()
Example #2
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
Example #3
0
 def _resize(self, image, size_x, size_y, fmt):
     image_resize_server = (self.env["ir.config_parameter"].sudo().
                            get_param("storage.image.resize.server"))
     if image_resize_server and image.backend_id.served_by != "odoo":
         values = {
             "url": image.url,
             "width": size_x,
             "height": size_y,
             "fmt": fmt
         }
         url = image_resize_server.format(**values)
         return base64.encodebytes(requests.get(url).content)
     image_process = ImageProcess(image.data)
     return image_process.resize(max_width=size_x,
                                 max_height=size_y).image_base64()
Example #4
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')