Hi all.,
can anyone tell me how can I compress (reducing image size)binary data before saving it into the postgres. Help me out plzz.
Odoo is the world's easiest all-in-one management software.
It includes hundreds of business apps:
Hi all.,
can anyone tell me how can I compress (reducing image size)binary data before saving it into the postgres. Help me out plzz.
Hi,
Here's a simple example of how you can integrate image compression with a button click action in an Odoo model:
from odoo import models, fields, api
from PIL import Image
import io
import base64
class MyModel(models.Model):
_name = 'my.model'
name = fields.Char(string="Name")
image = fields.Binary(string="Image")
@api.multi
def compress_image(self):
for record in self:
try:
img = Image.open(io.BytesIO(base64.b64decode(record.image)))
output_stream = io.BytesIO()
# Compress the image with the specified quality (adjust as needed)
img.save(output_stream, format='JPEG', quality=85)
# Update the image field with the compressed image
record.image = base64.b64encode(output_stream.getvalue())
output_stream.close()
print("Image compression successful!")
except Exception as e:
print(f"An error occurred: {e}")
Make sure to adjust the code according to your specific requirements and the structure of your Odoo module. Additionally, handle error cases and edge scenarios appropriately.
Hope it helps
สร้างบัญชีวันนี้เพื่อเพลิดเพลินไปกับฟีเจอร์พิเศษและมีส่วนร่วมกับคอมมูนิตี้ที่ยอดเยี่ยมของเรา!
ลงชื่อRelated Posts | ตอบกลับ | มุมมอง | กิจกรรม | |
---|---|---|---|---|
|
2
ก.ย. 23
|
7571 | ||
|
2
มี.ค. 23
|
46619 | ||
|
2
ธ.ค. 23
|
57583 | ||
|
0
มี.ค. 22
|
2290 | ||
|
0
ม.ค. 22
|
2821 |
Hope you are finding this: http://learnopenerp.blogspot.com/2020/07/how-to-resize-image-on-saving-records-in-odoo.html