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

Loading Image from file raises ValueError #933

Open
3 tasks done
except-pass opened this issue May 30, 2024 · 0 comments
Open
3 tasks done

Loading Image from file raises ValueError #933

except-pass opened this issue May 30, 2024 · 0 comments
Labels
bug Something isn't working

Comments

@except-pass
Copy link

First check

  • I added a descriptive title to this issue.
  • I used the GitHub search to try to find a similar issue and didn't find one.
  • I searched the Marvin documentation for this issue.

Bug summary

Loading a marvin.Image from a local file raises a value error -- please see below. The error comes because the split method is being called on a PosixPath object, which does not have this method. The fix is to change line 297 of marvin/types.py to format = path.suffix.lstrip('.').

I'm happy to give a pull request with this small change if its helpful.

<the rest of the image class...>
    @classmethod
    def from_path(cls, path: Union[str, Path]) -> "Image":
        with open(path, "rb") as f:
            data = f.read()
        #format = path.split(".")[-1]
        format = path.suffix.lstrip('.')
        if format not in ["jpg", "jpeg", "png", "webp"]:
            raise ValueError(f"Invalid image format: {format}")
        return cls(data=data, format=format)

Reproduction

import marvin
from pydantic import BaseModel

class MyModel(BaseModel):
    account_number: str

from pathlib import Path
img = marvin.Image(
    Path("example_image.jpg"),
)
result = marvin.cast(img, target=MyModel)

Error

Traceback (most recent call last):
  File "tryimage.py", line 9, in <module>
    img = marvin.Image(
  File "<...>/python3.10/site-packages/marvin/types.py", line 275, in __init__
    obj = type(self).infer(data_or_url, **kwargs)
  File "<...>/python3.10/site-packages/marvin/types.py", line 287, in infer
    return cls.from_path(path, **kwargs)
  File "<...>/python3.10/site-packages/marvin/types.py", line 297, in from_path
    format = path.split(".")[-1]
AttributeError: 'PosixPath' object has no attribute 'split'

Versions

Version:                2.3.4
Python version:         3.10.12
OS/Arch:                linux/x86_64

Additional context

No response

@except-pass except-pass added the bug Something isn't working label May 30, 2024
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
Development

No branches or pull requests

1 participant