[IMP] tools,base,*: remove intermediary image functions
* = hr, im_livechat, mail, payment, purchase, web, web_editor, web_unsplash, website_profile, website_slides Since the merge of all image tools into one function, the intermediary functions are not needed anymore. task-1958000 PR: #31811
This commit is contained in:
@@ -106,7 +106,7 @@ class Employee(models.Model):
|
||||
@api.model
|
||||
def _default_image(self):
|
||||
image_path = get_module_resource('hr', 'static/src/img', 'default_image.png')
|
||||
return tools.image_resize_image_big(base64.b64encode(open(image_path, 'rb').read()))
|
||||
return tools.image_process(base64.b64encode(open(image_path, 'rb').read()), size=tools.IMAGE_BIG_SIZE)
|
||||
|
||||
# resource and user
|
||||
# required on the resource, make sure required="True" set in the view
|
||||
|
||||
@@ -21,7 +21,7 @@ class ImLivechatChannel(models.Model):
|
||||
|
||||
def _default_image(self):
|
||||
image_path = modules.get_module_resource('im_livechat', 'static/src/img', 'default.png')
|
||||
return tools.image_resize_image_big(base64.b64encode(open(image_path, 'rb').read()))
|
||||
return tools.image_process(base64.b64encode(open(image_path, 'rb').read()), size=tools.IMAGE_BIG_SIZE)
|
||||
|
||||
def _default_user_ids(self):
|
||||
return [(6, 0, [self._uid])]
|
||||
|
||||
@@ -64,7 +64,7 @@ class Channel(models.Model):
|
||||
|
||||
def _get_default_image(self):
|
||||
image_path = modules.get_module_resource('mail', 'static/src/img', 'groupdefault.png')
|
||||
return tools.image_resize_image_big(base64.b64encode(open(image_path, 'rb').read()))
|
||||
return tools.image_process(base64.b64encode(open(image_path, 'rb').read()), size=tools.IMAGE_BIG_SIZE)
|
||||
|
||||
@api.model
|
||||
def default_get(self, fields):
|
||||
|
||||
@@ -7,11 +7,10 @@ from datetime import datetime
|
||||
from dateutil import relativedelta
|
||||
import pprint
|
||||
|
||||
from odoo import api, exceptions, fields, models, _
|
||||
from odoo.tools import consteq, float_round, image_resize_images, image_resize_image, ustr
|
||||
from odoo import api, exceptions, fields, models, _, SUPERUSER_ID
|
||||
from odoo.tools import consteq, float_round, image_resize_images, image_process, ustr
|
||||
from odoo.addons.base.models import ir_module
|
||||
from odoo.exceptions import ValidationError
|
||||
from odoo import api, SUPERUSER_ID
|
||||
from odoo.tools.misc import DEFAULT_SERVER_DATETIME_FORMAT
|
||||
from odoo.tools.misc import formatLang
|
||||
|
||||
@@ -525,16 +524,16 @@ class PaymentIcon(models.Model):
|
||||
for vals in vals_list:
|
||||
if 'image' in vals:
|
||||
image = ustr(vals['image'] or '').encode('utf-8')
|
||||
vals['image_payment_form'] = image_resize_image(image, size=(45,30))
|
||||
vals['image'] = image_resize_image(image, size=(64,64))
|
||||
vals['image_payment_form'] = image_process(image, size=(45,30))
|
||||
vals['image'] = image_process(image, size=(64,64))
|
||||
return super(PaymentIcon, self).create(vals_list)
|
||||
|
||||
@api.multi
|
||||
def write(self, vals):
|
||||
if 'image' in vals:
|
||||
image = ustr(vals['image'] or '').encode('utf-8')
|
||||
vals['image_payment_form'] = image_resize_image(image, size=(45,30))
|
||||
vals['image'] = image_resize_image(image, size=(64,64))
|
||||
vals['image_payment_form'] = image_process(image, size=(45,30))
|
||||
vals['image'] = image_process(image, size=(64,64))
|
||||
return super(PaymentIcon, self).write(vals)
|
||||
|
||||
class PaymentTransaction(models.Model):
|
||||
|
||||
@@ -7,7 +7,7 @@ from collections import OrderedDict
|
||||
from odoo import http
|
||||
from odoo.exceptions import AccessError, MissingError
|
||||
from odoo.http import request
|
||||
from odoo.tools import image_resize_image
|
||||
from odoo.tools import image_process
|
||||
from odoo.tools.translate import _
|
||||
from odoo.addons.portal.controllers.portal import pager as portal_pager, CustomerPortal
|
||||
from odoo.addons.web.controllers.main import Binary
|
||||
@@ -28,7 +28,7 @@ class CustomerPortal(CustomerPortal):
|
||||
def resize_to_48(b64source):
|
||||
if not b64source:
|
||||
b64source = base64.b64encode(Binary().placeholder())
|
||||
return image_resize_image(b64source, size=(48, 48))
|
||||
return image_process(b64source, size=(48, 48))
|
||||
|
||||
values = {
|
||||
'order': order,
|
||||
|
||||
@@ -36,7 +36,7 @@ import odoo
|
||||
import odoo.modules.registry
|
||||
from odoo.api import call_kw, Environment
|
||||
from odoo.modules import get_module_path, get_resource_path
|
||||
from odoo.tools import limited_image_resize, topological_sort, html_escape, pycompat
|
||||
from odoo.tools import image_process, topological_sort, html_escape, pycompat
|
||||
from odoo.tools.mimetypes import guess_mimetype
|
||||
from odoo.tools.translate import _
|
||||
from odoo.tools.misc import str2bool, xlwt, file_open
|
||||
@@ -1036,12 +1036,9 @@ class Binary(http.Controller):
|
||||
image_base64 = base64.b64encode(self.placeholder(image=placeholder))
|
||||
headers = self.force_contenttype(headers, contenttype='image/png')
|
||||
if not (width or height):
|
||||
suffix = 'big' if field == 'image' else field.split('_')[-1]
|
||||
if suffix in ('small', 'medium', 'large', 'big'):
|
||||
image_base64 = getattr(odoo.tools, 'image_resize_image_%s' % suffix)(image_base64)
|
||||
width, height = odoo.tools.image_guess_size_from_field_name(field)
|
||||
|
||||
image_base64 = limited_image_resize(
|
||||
image_base64, width=width, height=height, crop=crop)
|
||||
image_base64 = image_process(image_base64, (width, height), crop=crop)
|
||||
|
||||
content = base64.b64decode(image_base64)
|
||||
headers.append(('Content-Length', len(content)))
|
||||
|
||||
@@ -164,7 +164,7 @@ class Web_Editor(http.Controller):
|
||||
def _image_to_attachment(self, res_model, res_id, image_base64, name, datas_fname, disable_optimization=None):
|
||||
Attachments = request.env['ir.attachment']
|
||||
if not disable_optimization:
|
||||
image_base64 = tools.image_optimize_for_web(image_base64)
|
||||
image_base64 = tools.image_process(image_base64, verify_resolution=True)
|
||||
attachment = Attachments.create({
|
||||
'name': name,
|
||||
'datas_fname': datas_fname,
|
||||
|
||||
@@ -97,7 +97,7 @@ class Web_Unsplash(http.Controller):
|
||||
logger.exception("Timeout: " + str(e))
|
||||
continue
|
||||
|
||||
image_base64 = tools.image_optimize_for_web(image_base64)
|
||||
image_base64 = tools.image_process(image_base64)
|
||||
mimetype = guess_mimetype(base64.b64decode(image_base64))
|
||||
# append image extension in name
|
||||
query += mimetypes.guess_extension(mimetype) or ''
|
||||
|
||||
@@ -32,18 +32,10 @@ class WebsiteProfile(http.Controller):
|
||||
return user.website_published and user.karma > 0
|
||||
return False
|
||||
|
||||
def _get_default_avatar(self, field, headers, width, height):
|
||||
def _get_default_avatar(self):
|
||||
img_path = modules.get_module_resource('web', 'static/src/img', 'placeholder.png')
|
||||
with open(img_path, 'rb') as f:
|
||||
image = f.read()
|
||||
content = base64.b64encode(image)
|
||||
dictheaders = dict(headers) if headers else {}
|
||||
dictheaders['Content-Type'] = 'image/png'
|
||||
if not (width or height):
|
||||
suffix = field.split('_')[-1] if '_' in field else 'large'
|
||||
if suffix in ('small', 'medium', 'large', 'big'):
|
||||
content = getattr(tools, 'image_resize_image_%s' % suffix)(content)
|
||||
return content
|
||||
return base64.b64encode(f.read())
|
||||
|
||||
def _check_user_profile_access(self, user_id):
|
||||
user_sudo = request.env['res.users'].sudo().browse(user_id)
|
||||
@@ -99,10 +91,11 @@ class WebsiteProfile(http.Controller):
|
||||
return werkzeug.wrappers.Response(status=304)
|
||||
|
||||
if not image_base64:
|
||||
image_base64 = self._get_default_avatar(field, headers, width, height)
|
||||
image_base64 = self._get_default_avatar()
|
||||
if not (width or height):
|
||||
width, height = tools.image_guess_size_from_field_name(field)
|
||||
|
||||
image_base64 = tools.limited_image_resize(
|
||||
image_base64, width=width, height=height, crop=crop)
|
||||
image_base64 = tools.image_process(image_base64, (width, height), crop=crop)
|
||||
|
||||
content = base64.b64decode(image_base64)
|
||||
headers.append(('Content-Length', len(content)))
|
||||
|
||||
@@ -524,10 +524,11 @@ class WebsiteSlides(WebsiteProfile):
|
||||
return werkzeug.wrappers.Response(status=304)
|
||||
|
||||
if not image_base64:
|
||||
image_base64 = self._get_default_avatar(field, headers, width, height)
|
||||
image_base64 = self._get_default_avatar()
|
||||
if not (width or height):
|
||||
width, height = tools.image_guess_size_from_field_name(field)
|
||||
|
||||
image_base64 = tools.limited_image_resize(
|
||||
image_base64, width=width, height=height, crop=crop)
|
||||
image_base64 = tools.image_process(image_base64, (width, height), crop=crop)
|
||||
|
||||
content = base64.b64decode(image_base64)
|
||||
headers.append(('Content-Length', len(content)))
|
||||
|
||||
@@ -120,7 +120,7 @@ class Company(models.Model):
|
||||
@api.depends('partner_id', 'partner_id.image')
|
||||
def _compute_logo_web(self):
|
||||
for company in self:
|
||||
company.logo_web = tools.image_resize_image(company.partner_id.image, (180, None))
|
||||
company.logo_web = tools.image_process(company.partner_id.image, (180, None))
|
||||
|
||||
@api.onchange('state_id')
|
||||
def _onchange_state(self):
|
||||
|
||||
@@ -348,9 +348,9 @@ class Partner(models.Model):
|
||||
with open(img_path, 'rb') as f:
|
||||
image_base64 = base64.b64encode(f.read())
|
||||
if image_base64 and colorize:
|
||||
image_base64 = tools.image_colorize(image_base64)
|
||||
image_base64 = tools.image_process(image_base64, colorize=True)
|
||||
|
||||
return tools.image_resize_image_big(image_base64)
|
||||
return tools.image_process(image_base64, size=tools.IMAGE_BIG_SIZE)
|
||||
|
||||
@api.model
|
||||
def _fields_view_get(self, view_id=None, view_type='form', toolbar=False, submenu=False):
|
||||
|
||||
@@ -14,10 +14,6 @@ class TestImage(TransactionCase):
|
||||
|
||||
The following helpers are not tested here because they are wrappers for the
|
||||
other methods or they are tested elsewhere (eg. on TestWebsiteSaleImage):
|
||||
- image_resize_image_big
|
||||
- image_resize_image_large
|
||||
- image_resize_image_medium
|
||||
- image_resize_image_small
|
||||
- image_get_resized_images
|
||||
- image_resize_images
|
||||
- is_image_size_above
|
||||
@@ -56,159 +52,155 @@ class TestImage(TransactionCase):
|
||||
image_base64 = tools.image_to_base64(image, 'PNG')
|
||||
self.assertEqual(image_base64, self.base64_image_1x1)
|
||||
|
||||
def test_10_image_resize_image(self):
|
||||
"""Test that image_resize_image is working as expected."""
|
||||
def test_10_image_process_resize(self):
|
||||
"""Test that image_process is working as expected for resizeing."""
|
||||
|
||||
# CASE: return False if base64_source is falsy
|
||||
res = tools.image_resize_image(False)
|
||||
res = tools.image_process(False)
|
||||
self.assertFalse(res)
|
||||
|
||||
# CASE: return base64_source if format is SVG
|
||||
res = tools.image_resize_image(self.base64_svg)
|
||||
res = tools.image_process(self.base64_svg)
|
||||
self.assertEqual(res, self.base64_svg)
|
||||
|
||||
# CASE: ok with default parameters (keep ratio)
|
||||
image = tools.base64_to_image(tools.image_resize_image(self.base64_image_1920_1080))
|
||||
self.assertEqual(image.size, (1024, 576))
|
||||
|
||||
# CASE: correct resize (keep ratio)
|
||||
image = tools.base64_to_image(tools.image_resize_image(self.base64_image_1920_1080, size=(800, 600)))
|
||||
image = tools.base64_to_image(tools.image_process(self.base64_image_1920_1080, size=(800, 600)))
|
||||
self.assertEqual(image.size, (800, 450))
|
||||
|
||||
# CASE: change filetype to PNG
|
||||
image = tools.base64_to_image(tools.image_resize_image(self.base64_image_1920_1080, filetype='PNG'))
|
||||
# CASE: change format to PNG
|
||||
image = tools.base64_to_image(tools.image_process(self.base64_image_1920_1080, output_format='PNG'))
|
||||
self.assertEqual(image.format, 'PNG')
|
||||
|
||||
# CASE: change filetype to JPEG (case insensitive)
|
||||
image = tools.base64_to_image(tools.image_resize_image(self.base64_image_1x1, filetype='jpeg'))
|
||||
# CASE: change format to JPEG (case insensitive)
|
||||
image = tools.base64_to_image(tools.image_process(self.base64_image_1x1, output_format='jpeg'))
|
||||
self.assertEqual(image.format, 'JPEG')
|
||||
|
||||
# CASE: change filetype to BMP converted to PNG
|
||||
image = tools.base64_to_image(tools.image_resize_image(self.base64_image_1x1, filetype='BMP'))
|
||||
# CASE: change format to BMP converted to PNG
|
||||
image = tools.base64_to_image(tools.image_process(self.base64_image_1x1, output_format='BMP'))
|
||||
self.assertEqual(image.format, 'PNG')
|
||||
|
||||
# CASE: change filetype PNG with RGBA to JPEG
|
||||
# CASE: change format PNG with RGBA to JPEG
|
||||
base64_image_1080_1920_rgba = tools.image_to_base64(PIL.Image.new('RGBA', (1080, 1920)), 'PNG')
|
||||
image = tools.base64_to_image(tools.image_resize_image(base64_image_1080_1920_rgba, filetype='jpeg'))
|
||||
image = tools.base64_to_image(tools.image_process(base64_image_1080_1920_rgba, output_format='jpeg'))
|
||||
self.assertEqual(image.format, 'JPEG')
|
||||
|
||||
# CASE: keep ratio if no height given
|
||||
image = tools.base64_to_image(tools.image_resize_image(self.base64_image_1920_1080, size=(192, None)))
|
||||
image = tools.base64_to_image(tools.image_process(self.base64_image_1920_1080, size=(192, None)))
|
||||
self.assertEqual(image.size, (192, 108))
|
||||
|
||||
# CASE: keep ratio if no width given
|
||||
image = tools.base64_to_image(tools.image_resize_image(self.base64_image_1920_1080, size=(None, 108)))
|
||||
image = tools.base64_to_image(tools.image_process(self.base64_image_1920_1080, size=(None, 108)))
|
||||
self.assertEqual(image.size, (192, 108))
|
||||
|
||||
# CASE: size above, return original
|
||||
image = tools.base64_to_image(tools.image_resize_image(self.base64_image_1920_1080, size=(3000, 2000)))
|
||||
image = tools.base64_to_image(tools.image_process(self.base64_image_1920_1080, size=(3000, 2000)))
|
||||
self.assertEqual(image.size, (1920, 1080))
|
||||
|
||||
# CASE: same size, no change
|
||||
image = tools.base64_to_image(tools.image_resize_image(self.base64_image_1920_1080, size=(1920, 1080)))
|
||||
image = tools.base64_to_image(tools.image_process(self.base64_image_1920_1080, size=(1920, 1080)))
|
||||
self.assertEqual(image.size, (1920, 1080))
|
||||
|
||||
# CASE: no resize if above
|
||||
image = tools.base64_to_image(tools.image_resize_image(self.base64_image_1920_1080, size=(3000, None)))
|
||||
image = tools.base64_to_image(tools.image_process(self.base64_image_1920_1080, size=(3000, None)))
|
||||
self.assertEqual(image.size, (1920, 1080))
|
||||
|
||||
# CASE: no resize if above
|
||||
image = tools.base64_to_image(tools.image_resize_image(self.base64_image_1920_1080, size=(None, 2000)))
|
||||
image = tools.base64_to_image(tools.image_process(self.base64_image_1920_1080, size=(None, 2000)))
|
||||
self.assertEqual(image.size, (1920, 1080))
|
||||
|
||||
# CASE: vertical image, correct resize if below
|
||||
base64_image_1080_1920 = tools.image_to_base64(PIL.Image.new('RGB', (1080, 1920)), 'PNG')
|
||||
image = tools.base64_to_image(tools.image_resize_image(base64_image_1080_1920, size=(3000, 192)))
|
||||
image = tools.base64_to_image(tools.image_process(base64_image_1080_1920, size=(3000, 192)))
|
||||
self.assertEqual(image.size, (108, 192))
|
||||
|
||||
# CASE: adapt to width
|
||||
image = tools.base64_to_image(tools.image_resize_image(self.base64_image_1920_1080, size=(192, 200)))
|
||||
image = tools.base64_to_image(tools.image_process(self.base64_image_1920_1080, size=(192, 200)))
|
||||
self.assertEqual(image.size, (192, 108))
|
||||
|
||||
# CASE: adapt to height
|
||||
image = tools.base64_to_image(tools.image_resize_image(self.base64_image_1920_1080, size=(400, 108)))
|
||||
image = tools.base64_to_image(tools.image_process(self.base64_image_1920_1080, size=(400, 108)))
|
||||
self.assertEqual(image.size, (192, 108))
|
||||
|
||||
def test_11_image_optimize_for_web(self):
|
||||
"""Test that image_optimize_for_web is working as expected."""
|
||||
def test_11_image_process(self):
|
||||
"""Test that image_proces is working as expected."""
|
||||
# CASE: return base64_source if format is SVG
|
||||
res = tools.image_optimize_for_web(self.base64_svg)
|
||||
res = tools.image_process(self.base64_svg)
|
||||
self.assertEqual(res, self.base64_svg)
|
||||
|
||||
# CASE: size excessive
|
||||
base64_image_excessive = tools.image_to_base64(PIL.Image.new('RGB', (45001, 1000)), 'PNG')
|
||||
with self.assertRaises(ValueError):
|
||||
res = tools.image_optimize_for_web(base64_image_excessive)
|
||||
res = tools.image_process(base64_image_excessive, verify_resolution=True)
|
||||
|
||||
# CASE: max_width
|
||||
image = tools.base64_to_image(tools.image_optimize_for_web(self.base64_image_1920_1080, max_width=192))
|
||||
image = tools.base64_to_image(tools.image_process(self.base64_image_1920_1080, (192, 0)))
|
||||
self.assertEqual(image.size, (192, 108))
|
||||
|
||||
# CASE: PNG RGBA doesn't apply quality, just optimize
|
||||
base64_image_1080_1920_rgba = tools.image_to_base64(PIL.Image.new('RGBA', (1080, 1920)), 'PNG')
|
||||
res = tools.image_optimize_for_web(base64_image_1080_1920_rgba)
|
||||
res = tools.image_process(base64_image_1080_1920_rgba)
|
||||
self.assertLessEqual(len(res), len(base64_image_1080_1920_rgba))
|
||||
|
||||
# CASE: PNG RGB doesn't apply quality, just optimize
|
||||
base64_image_1080_1920_rgb = tools.image_to_base64(PIL.Image.new('P', (1080, 1920)), 'PNG')
|
||||
res = tools.image_optimize_for_web(base64_image_1080_1920_rgb)
|
||||
res = tools.image_process(base64_image_1080_1920_rgb)
|
||||
self.assertLessEqual(len(res), len(base64_image_1080_1920_rgb))
|
||||
|
||||
# CASE: JPEG strictly reduced quality
|
||||
res = tools.image_optimize_for_web(self.base64_image_1920_1080)
|
||||
res = tools.image_process(self.base64_image_1920_1080)
|
||||
self.assertLessEqual(len(res), len(self.base64_image_1920_1080))
|
||||
|
||||
# CASE: GIF doesn't apply quality, just optimize
|
||||
base64_image_1080_1920_gif = tools.image_to_base64(PIL.Image.new('RGB', (1080, 1920)), 'GIF')
|
||||
res = tools.image_optimize_for_web(base64_image_1080_1920_gif)
|
||||
res = tools.image_process(base64_image_1080_1920_gif)
|
||||
self.assertLessEqual(len(res), len(base64_image_1080_1920_gif))
|
||||
|
||||
# CASE: unsupported format
|
||||
base64_image_1080_1920_tiff = tools.image_to_base64(PIL.Image.new('RGB', (1080, 1920)), 'TIFF')
|
||||
image = tools.base64_to_image(tools.image_optimize_for_web(base64_image_1080_1920_tiff))
|
||||
image = tools.base64_to_image(tools.image_process(base64_image_1080_1920_tiff))
|
||||
self.assertEqual(image.format, 'JPEG')
|
||||
|
||||
def test_12_crop_image(self):
|
||||
"""Test that crop_image is working as expected."""
|
||||
def test_12_image_process(self):
|
||||
"""Test that image_process is working as expected with crop."""
|
||||
|
||||
# CASE: return False if base64_source is falsy
|
||||
res = tools.crop_image(False, size=(1, 1))
|
||||
res = tools.image_process(False, size=(1, 1), crop='center')
|
||||
self.assertFalse(res)
|
||||
|
||||
# CASE: size, crop biggest possible
|
||||
image = tools.base64_to_image(tools.crop_image(self.base64_image_1920_1080, size=(2000, 2000)))
|
||||
image = tools.base64_to_image(tools.image_process(self.base64_image_1920_1080, size=(2000, 2000), crop='center'))
|
||||
self.assertEqual(image.size, (1080, 1080))
|
||||
|
||||
# CASE: size vertical, limit height
|
||||
image = tools.base64_to_image(tools.crop_image(self.base64_image_1920_1080, size=(2000, 4000)))
|
||||
image = tools.base64_to_image(tools.image_process(self.base64_image_1920_1080, size=(2000, 4000), crop='center'))
|
||||
self.assertEqual(image.size, (540, 1080))
|
||||
|
||||
# CASE: size horizontal, limit width
|
||||
image = tools.base64_to_image(tools.crop_image(self.base64_image_1920_1080, size=(4000, 2000)))
|
||||
image = tools.base64_to_image(tools.image_process(self.base64_image_1920_1080, size=(4000, 2000), crop='center'))
|
||||
self.assertEqual(image.size, (1920, 960))
|
||||
|
||||
# CASE: type center
|
||||
image = tools.base64_to_image(tools.crop_image(self.base64_image_1920_1080, size=(2000, 2000), type='center'))
|
||||
image = tools.base64_to_image(tools.image_process(self.base64_image_1920_1080, size=(2000, 2000), crop='center'))
|
||||
self.assertEqual(image.size, (1080, 1080))
|
||||
|
||||
# CASE: type center
|
||||
image = tools.base64_to_image(tools.crop_image(self.base64_image_1920_1080, size=(2000, 2000), type='top'))
|
||||
image = tools.base64_to_image(tools.image_process(self.base64_image_1920_1080, size=(2000, 2000), crop='top'))
|
||||
self.assertEqual(image.size, (1080, 1080))
|
||||
|
||||
# CASE: type bottom
|
||||
image = tools.base64_to_image(tools.crop_image(self.base64_image_1920_1080, size=(2000, 2000), type='bottom'))
|
||||
image = tools.base64_to_image(tools.image_process(self.base64_image_1920_1080, size=(2000, 2000), crop='bottom'))
|
||||
self.assertEqual(image.size, (1080, 1080))
|
||||
|
||||
# CASE: wrong crop value, use center
|
||||
image = tools.base64_to_image(tools.crop_image(self.base64_image_1920_1080, size=(2000, 2000), type='wrong'))
|
||||
image = tools.base64_to_image(tools.image_process(self.base64_image_1920_1080, size=(2000, 2000), crop='wrong'))
|
||||
self.assertEqual(image.size, (1080, 1080))
|
||||
|
||||
# CASE: size given, resize too
|
||||
image = tools.base64_to_image(tools.crop_image(self.base64_image_1920_1080, size=(512, 512)))
|
||||
image = tools.base64_to_image(tools.image_process(self.base64_image_1920_1080, size=(512, 512), crop='center'))
|
||||
self.assertEqual(image.size, (512, 512))
|
||||
|
||||
def test_13_image_colorize(self):
|
||||
"""Test that image_colorize is working as expected."""
|
||||
def test_13_image_process_colorize(self):
|
||||
"""Test that image_process is working as expected for colorize."""
|
||||
|
||||
# verify initial condition
|
||||
image_rgba = PIL.Image.new('RGBA', (1, 1))
|
||||
@@ -216,33 +208,30 @@ class TestImage(TransactionCase):
|
||||
base64_rgba = tools.image_to_base64(image_rgba, 'PNG')
|
||||
|
||||
# CASE: color random, color has changed
|
||||
image = tools.base64_to_image(tools.image_colorize(base64_rgba))
|
||||
image = tools.base64_to_image(tools.image_process(base64_rgba, colorize=True))
|
||||
self.assertNotEqual(image.getpixel((0, 0)), (0, 0, 0))
|
||||
|
||||
def test_16_limited_image_resize(self):
|
||||
"""Test that limited_image_resize is working as expected."""
|
||||
|
||||
# CASE: return input if base64_source is falsy
|
||||
self.assertEqual(tools.limited_image_resize(False), False)
|
||||
def test_16_image_process_resize(self):
|
||||
"""Test that image_process is working as expected."""
|
||||
|
||||
# CASE: return input if no width or height
|
||||
image = tools.base64_to_image(tools.limited_image_resize(self.base64_image_1920_1080))
|
||||
image = tools.base64_to_image(tools.image_process(self.base64_image_1920_1080))
|
||||
self.assertEqual(image.size, (1920, 1080))
|
||||
|
||||
# CASE: given width, resize and keep ratio
|
||||
image = tools.base64_to_image(tools.limited_image_resize(self.base64_image_1920_1080, width=192))
|
||||
image = tools.base64_to_image(tools.image_process(self.base64_image_1920_1080, (192, 0)))
|
||||
self.assertEqual(image.size, (192, 108))
|
||||
|
||||
# CASE: given height, resize and keep ratio
|
||||
image = tools.base64_to_image(tools.limited_image_resize(self.base64_image_1920_1080, height=108))
|
||||
image = tools.base64_to_image(tools.image_process(self.base64_image_1920_1080, (0, 108)))
|
||||
self.assertEqual(image.size, (192, 108))
|
||||
|
||||
# CASE: crop
|
||||
image = tools.base64_to_image(tools.limited_image_resize(self.base64_image_1920_1080, width=192, height=108, crop=True))
|
||||
image = tools.base64_to_image(tools.image_process(self.base64_image_1920_1080, (192, 108), crop=True))
|
||||
self.assertEqual(image.size, (192, 108))
|
||||
|
||||
# CASE: keep ratio
|
||||
image = tools.base64_to_image(tools.limited_image_resize(self.base64_image_1920_1080, width=1000, height=1000))
|
||||
image = tools.base64_to_image(tools.image_process(self.base64_image_1920_1080, (1000, 1000)))
|
||||
self.assertEqual(image.size, (1000, 562))
|
||||
|
||||
def test_17_image_data_uri(self):
|
||||
|
||||
@@ -64,7 +64,7 @@ class test_ir_http_mimetype(common.TransactionCase):
|
||||
'type': 'binary',
|
||||
})
|
||||
|
||||
resized = odoo.tools.image_resize_image_small(prop.value_binary)
|
||||
resized = odoo.tools.image_process(prop.value_binary, odoo.tools.IMAGE_SMALL_SIZE)
|
||||
# Simul computed field which resize and that is not attachement=True (E.G. on product)
|
||||
prop.write({'value_binary': resized})
|
||||
status, headers, content = self.env['ir.http'].binary_content(
|
||||
|
||||
+5
-49
@@ -168,46 +168,6 @@ def image_process(base64_source, size=(0, 0), verify_resolution=False, quality=8
|
||||
|
||||
return image_to_base64(image, **opt)
|
||||
|
||||
# ----------------------------------------
|
||||
# Image resizing
|
||||
# ----------------------------------------
|
||||
|
||||
|
||||
def image_resize_image(base64_source, size=IMAGE_BIG_SIZE, filetype=None):
|
||||
return image_process(base64_source, size=size, output_format=filetype)
|
||||
|
||||
|
||||
def image_optimize_for_web(base64_source, max_width=0, quality=80):
|
||||
return image_process(base64_source, size=(max_width, 0), verify_resolution=True, quality=quality)
|
||||
|
||||
|
||||
def image_resize_image_big(base64_source, filetype=None):
|
||||
return image_process(base64_source, size=IMAGE_BIG_SIZE, output_format=filetype)
|
||||
|
||||
|
||||
def image_resize_image_large(base64_source, filetype=None):
|
||||
return image_process(base64_source, size=IMAGE_LARGE_SIZE, output_format=filetype)
|
||||
|
||||
|
||||
def image_resize_image_medium(base64_source, filetype=None):
|
||||
return image_process(base64_source, size=IMAGE_MEDIUM_SIZE, output_format=filetype)
|
||||
|
||||
|
||||
def image_resize_image_small(base64_source, filetype=None):
|
||||
return image_process(base64_source, size=IMAGE_SMALL_SIZE, output_format=filetype)
|
||||
|
||||
|
||||
def crop_image(base64_source, size, type='center', image_format=None):
|
||||
return image_process(base64_source, size=size, crop=type, output_format=image_format)
|
||||
|
||||
|
||||
# ----------------------------------------
|
||||
# Colors
|
||||
# ---------------------------------------
|
||||
|
||||
def image_colorize(base64_source):
|
||||
return image_process(base64_source, colorize=True)
|
||||
|
||||
|
||||
# ----------------------------------------
|
||||
# Misc image tools
|
||||
@@ -298,13 +258,13 @@ def image_get_resized_images(base64_source,
|
||||
"""
|
||||
return_dict = dict()
|
||||
if big_name:
|
||||
return_dict[big_name] = image_resize_image_big(base64_source)
|
||||
return_dict[big_name] = image_process(base64_source, size=IMAGE_BIG_SIZE)
|
||||
if large_name:
|
||||
return_dict[large_name] = image_resize_image_large(base64_source)
|
||||
return_dict[large_name] = image_process(base64_source, size=IMAGE_LARGE_SIZE)
|
||||
if medium_name:
|
||||
return_dict[medium_name] = image_resize_image_medium(base64_source)
|
||||
return_dict[medium_name] = image_process(base64_source, size=IMAGE_MEDIUM_SIZE)
|
||||
if small_name:
|
||||
return_dict[small_name] = image_resize_image_small(base64_source)
|
||||
return_dict[small_name] = image_process(base64_source, size=IMAGE_SMALL_SIZE)
|
||||
return return_dict
|
||||
|
||||
|
||||
@@ -333,10 +293,6 @@ def image_resize_images(vals,
|
||||
vals[small_name] = False
|
||||
|
||||
|
||||
def limited_image_resize(base64_source, width=None, height=None, crop=False):
|
||||
return image_process(base64_source, size=(width, height), crop=crop)
|
||||
|
||||
|
||||
def image_data_uri(base64_source):
|
||||
"""This returns data URL scheme according RFC 2397
|
||||
(https://tools.ietf.org/html/rfc2397) for all kind of supported images
|
||||
@@ -354,5 +310,5 @@ if __name__=="__main__":
|
||||
assert len(sys.argv)==3, 'Usage to Test: image.py SRC.png DEST.png'
|
||||
|
||||
img = base64.b64encode(open(sys.argv[1],'rb').read())
|
||||
new = image_resize_image(img, (128,100))
|
||||
new = image_process(img, (128, 100))
|
||||
open(sys.argv[2], 'wb').write(base64.b64decode(new))
|
||||
|
||||
Reference in New Issue
Block a user