The Wayback Machine - https://web.archive.org/web/20220102134137/https://github.com/jina-ai/jina/issues/4008
Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot dump imageblob to bytesio #4008

Open
Stubatiger opened this issue Nov 29, 2021 · 3 comments · May be fixed by #4023
Open

Cannot dump imageblob to bytesio #4008

Stubatiger opened this issue Nov 29, 2021 · 3 comments · May be fixed by #4023

Comments

@Stubatiger
Copy link

@Stubatiger Stubatiger commented Nov 29, 2021

ENV

Python 3.9
jina 2.5.0

Describe the bug

If i try to dump an image blob to a io.bytesio object an error is thrown

from jina import Document
import io
d =  Document(uri='steam_data/image_store/8c/5b/8c5b265b9c533636.png')
output = io.BytesIO() 

(
    d
    .load_uri_to_image_blob()
    .dump_image_blob_to_file(output)
)
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
/tmp/ipykernel_12013/123594708.py in <module>
      5 
      6 (
----> 7     d
      8     .load_uri_to_image_blob()
      9     .dump_image_blob_to_file(output)

~/miniconda3/envs/steam3.9/lib/python3.9/site-packages/jina/types/document/mixins/image.py in dump_image_blob_to_file(self, file, channel_axis)
    100             blob = _move_channel_axis(self.blob, channel_axis, -1)
    101             buffer = _to_png_buffer(blob)
--> 102             fp.write(buffer)
    103         return self
    104 

AttributeError: 'nullcontext' object has no attribute 'write'

Describe how you solve it

Here

fp = _get_file_context(file)

the returned object of the context is not used in this line

 with fp:

if we change the code to

with fp as fp_cxt:
    blob = _move_channel_axis(self.blob, channel_axis, -1)
    buffer = _to_png_buffer(blob)
    fp_cxt.write(buffer)

it seems to work for my case.
Im not sure of the broader consequences though, as i dont know what the nullcontext is used for here

@JoanFM
Copy link
Member

@JoanFM JoanFM commented Nov 29, 2021

Hey @Stubatiger ,

Thanks for finding the issue. Would u please raise a PR to fix the issue along with a test to show the fix?

@Stubatiger
Copy link
Author

@Stubatiger Stubatiger commented Dec 1, 2021

Sure, ill give it a try on the weekend :o)

@CatStark
Copy link
Member

@CatStark CatStark commented Dec 15, 2021

Hi @Stubatiger , thanks a lot for your contribution! Did you have time to check Joan's feedback? or is there anything else you need from our side to continue with the PR?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment