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

Passing jpegs constructed from byte streams crashes with FileNotFoundError: [Errno 2] No such file or directory: '' #415

Open
davidabrahams1 opened this issue Sep 5, 2023 · 3 comments · May be fixed by #878
Labels
bug Something isn't working

Comments

@davidabrahams1
Copy link

davidabrahams1 commented Sep 5, 2023

Environment

I don't believe this bug is environment dependent

To reproduce

This line of code: https://github.com/mosaicml/streaming/blob/v0.5.2/streaming/base/format/mds/encodings.py#L417

assumes that Images created from byte-streams will have hasattr(obj, 'filename') == False, however I believe they will actually have obj.filename == ''. This will then throw an exception on with open(obj.filename, 'rb') as f, since the filename is empty-string.

Here's a minimal script you can use to prove this:

(pillowenv) devbox-david-84f94b7bb8-4hrlq% cat image_foo.py 
from PIL import Image
from io import BytesIO

# Open the JPEG image using Pillow
with Image.open('/some/path.jpg') as img:
    img_bytes_io = BytesIO()
    
    # Save the image to the BytesIO object in JPEG format
    img.save(img_bytes_io, format='JPEG')

image_data = img_bytes_io.getvalue()
image_bytes_io = BytesIO(image_data)
image_opened = Image.open(image_bytes_io)
print(image_bytes_io)
print(image_opened)
print(hasattr(image_opened, 'filename'))
print(image_opened.filename)
(pillowenv) devbox-david-84f94b7bb8-4hrlq% python image_foo.py 
<_io.BytesIO object at 0x7fbe49122450>
<PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=640x480 at 0x7FBE49115850>
True

You can see it has the filename attribute, and printing it is empty string. I imagine the fix is just to check if the filename is empty string?

Expected behavior

Additional context

@davidabrahams1 davidabrahams1 added the bug Something isn't working label Sep 5, 2023
@sagnak
Copy link

sagnak commented Sep 5, 2023

this was my temporary workaround with monkey-patching till the bug-fix:

def jpeg_encode_without_filename(self: JPEG, obj: Image.Image) -> bytes:
    self._validate(obj, Image.Image)  # pylint: disable=protected-access
    out = BytesIO()
    obj.save(out, format="JPEG")
    return out.getvalue()


JPEG.encode = jpeg_encode_without_filename.__get__(JPEG(), JPEG)  # pylint: disable=no-value-for-parameter

@snarayan21
Copy link
Collaborator

Hey @davidabrahams1 and @sagnak, is this still an issue you're seeing?

@Padge91
Copy link

Padge91 commented Jan 23, 2025

@snarayan21 I encountered this issue today with 0.11.0. The suggested monkey patch fixed it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
4 participants