How to show a product image with binary field?

Hi, i have a custom field for product module. It adds a photo to the template.

The code for that is:

photo = fields.Binary(‘Photo’)

@classmethod
def convert_photo(cls, data):
    if data and Image:
        image = Image.open(BytesIO(data))
        image.thumbnail((200, 200), Image.ANTIALIAS)
        data = BytesIO()
        image.save(data, image.format)
        data = fields.Binary.cast(data.getvalue())
    return data

@classmethod
def write(cls, *args):
    actions = iter(args)
    args = []
    for products, vals in zip(actions, actions):
        vals = vals.copy()

        if 'photo' in vals:
            vals['photo'] = cls.convert_photo(vals['photo'])

        args.append(products)
        args.append(vals)
    return super(Template, cls).write(*args)

And I used to display it in the form with:

It works fine with sao but not with tryton, maybe I need to add something more to display it correctly?

I would like to ask to about how to print in a report a binary field for an image correctly? If I use <product.photo> it display the binary data for the field, I read an old post [0] but I can not make it work.

[0] https://groups.google.com/forum/?fromgroups#!searchin/tryton/image$20report|sort:date/tryton/plDDCzfmTgw/pekpys1pf5sJ

You can try this in his form
<field name=“photo” widget=“image”/>

As explained on the relatorio documentation you should use a frame with the following expression:

image: product.photo, 'image/png'

Note that the first element is the field expression containing the image and the second the mimetype of the image.

Hope it helps

Thanks, I already use it on my view form, works on sao but not in tryton.

Thanks Sergi, I already use it but I can’t display the image on the report. When I add the frame with image: product.photo and print the report the follow message is received:

Traceback (most recent call last):
File “/opt/aisa/lib/python34/site-packages/trytond/wsgipy”, line 73, in dispatch_request
return endpoint(request, **requestview_args)
File “/opt/aisa/lib/python34/site-packages/trytond/protocols/dispatcherpy”, line 46, in rpc
request, database_name, *requestrpc_params)
File “/opt/aisa/lib/python34/site-packages/trytond/wsgipy”, line 44, in auth_required
return wrapped(*args, **kwargs)
File “/opt/aisa/lib/python34/site-packages/trytond/protocols/wrapperspy”, line 122, in wrapper
return func(request, pool, *args, **kwargs)
File “/opt/aisa/lib/python34/site-packages/trytond/protocols/dispatcherpy”, line 176, in _dispatch
result = rpcresult(meth(*c_args, **c_kwargs))
File “/opt/aisa/lib/python34/site-packages/trytond/modules/sale/salepy”, line 1553, in execute
return super(SaleReport, cls)execute(ids, data)
File “/opt/aisa/lib/python34/site-packages/trytond/modules/account_aisa/reportpy”, line 299, in execute
return super(SaleReport, cls)execute(ids, data)
File “/opt/aisa/lib/python34/site-packages/trytond/report/reportpy”, line 182, in execute
oext, content = cls_execute(records, data, action_report)
File “/opt/aisa/lib/python34/site-packages/trytond/report/reportpy”, line 190, in _execute
return clsconvert(action, clsrender(action, report_context))
File “/opt/aisa/lib/python34/site-packages/trytond/report/reportpy”, line 290, in render
data = rel_report(**report_context)render()
File “/opt/aisa/lib/python34/site-packages/relatorio/templates/basepy”, line 34, in render
return selfserializer(selfevents)
File “/opt/aisa/lib/python34/site-packages/relatorio/templates/opendocumentpy”, line 1056, in call
for kind, data, pos in stream:
File “/opt/aisa/lib/python34/site-packages/genshi/corepy”, line 292, in _ensure
for event in stream:
File “/opt/aisa/lib/python34/site-packages/genshi/filters/i18npy”, line 691, in call
for kind, data, pos in stream:
File “/opt/aisa/lib/python34/site-packages/genshi/template/basepy”, line 638, in _include
for event in stream:
File “/opt/aisa/lib/python34/site-packages/genshi/template/markuppy”, line 327, in _match
for event in stream:
File “/opt/aisa/lib/python34/site-packages/genshi/template/basepy”, line 578, in _flatten
for kind, data, pos in stream:
File “/opt/aisa/lib/python34/site-packages/genshi/template/directivespy”, line 168, in _generate
attrs = _eval_expr(selfexpr, ctxt, vars)
File “/opt/aisa/lib/python34/site-packages/genshi/template/basepy”, line 289, in _eval_expr
retval = exprevaluate(ctxt)
File “/opt/aisa/lib/python34/site-packages/genshi/template/evalpy”, line 178, in evaluate
return eval(selfcode, _globals, {‘data’: data})
File “”, line 330, in <Expression ‘__relatorio_make_href(__relatorio_get_cache(140189814980168))’>
File “/opt/aisa/lib/python34/site-packages/relatorio/templates/opendocumentpy”, line 129, in call
bitstream = BytesIO(bitstream)
TypeError: ‘int’ does not support the buffer interface

From the traceback it seems you are using a Integer fields instead of a Binary one.

No, I’m using [0] and [1] to create the field and using it on the report, but some reason detect it like int. I read about it on [2] but don’t know how to test a solution for it.

[0] http://hg.savannah.gnu.org/hgweb/health/file/3e291c75405d/tryton/health/health.py#l433
[1] http://hg.savannah.gnu.org/hgweb/health/file/3e291c75405d/tryton/health/health.py#l577
[2] https://stackoverflow.com/questions/28335048/struct-unpack-causing-typeerrorint-does-not-support-the-buffer-interface

This is probably because the context has the key to request size: Fields — trytond 5.3 documentation

I think that the trouble come from context, it display the size instead of the value, how I could display the value? I couldn’t find an example of that in the docs.

You should remove the key to request the size from the context in order to return the value. See:

Thanks for your help, but I still can not make to work a report with image, PNG or JPG. Maybe I can find and example on tryton or the code to remove the key to request the size from the context.

Maybe this thread will help you: https://groups.google.com/forum/#!msg/tryton-dev/4N0l2-im2jc/P4nGCqA0BQAJ . Read carefully the selected answer.

1 Like

Thanks @edbo, I read the post and the solution works fine.

Only added the field in model
photo = fields.fields.Binary(“Photo”)

And in report use the follow code inside the name of a frame:
image: (line.product.photo, “image/png”)

Thanks a lot!

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.