Skip to Content
Menu
This question has been flagged
4 Replies
14339 Views

Hello everyone,

I am working with Odoo 12.

I have been searching the forum and on web, for information about working with image sizes in Odoo.

I have found lots of information about that but all of them does not fit on my actual requirements.

In fact, most of them only refers to pre-defined image sizes like image big (1024x1024px), image medium and image small. All of them with fixed image sizes. I could not find any information on how to actually define a size for the final image.

What I need to do is:

- Allow user to attach an image to the form;

- Resize this image to a specific image size (let's say: 500x500px), thus reduce it's size;

- Have the image saved on the database;

 I am using the standard Odoo image type field (binary) declared like this:

    fields.Binary("Image", attachment=True, default:lambda, self:self.SOME_DEFINITION_HERE_WHICH_I_DO_NOT_KNOW);

Can anyone help me achieving that?

Thank you all in advance

Best regards

Paulo


Avatar
Discard
Best Answer

Hello Paulo,

You don't need to recreate the image.py file. You can use its methods by importing it from odoo.tools:

`from odoo.tools import image_resize_images`

You should use the method image_resize_images with a specified parameter size , like is in the following example:

image_resize_images(vals, big_name='cover', medium_name='cover_medium', small_name='cover_small',
size={'cover': (1550, None), 'cover_medium': (1000, None), 'cover_small': (420, None)})
The Odoo way of using that technique for storing 3 types of image sizes is by defining 3 fields
 for images in your model, that represents big_image, medium_image and small_image.

Use this method by calling it in create and write methods in the model, like this:
@api.model
def create(self, vals):
image_resize_images(vals)
return super(PaymentAcquirer, self).create(vals)

@api.multi
def write(self, vals):
image_resize_images(vals)
return super(PaymentAcquirer, self).write(vals)

The example is from payment_acquirer.py in the payment module. You can see it for more information.
Regards,
Lyubo.
Avatar
Discard
Author

Dear @Lyubomir,

Thank you very very much for your update.

This is exactly what I need.

Best regards

Paulo

Author Best Answer

Hello,

I have found that the best option, is to recreate the image.py under tools and import this new file to our own new module calling the new defs under this new module instead of the pre-defined Odoo tools module.

Thank you very much

Regards

Paulo



Avatar
Discard
Related Posts Replies Views Activity
2
Feb 19
3485
3
Oct 23
5973
1
Sep 23
1962
1
May 23
1000
2
Apr 23
1374