marvin icon indicating copy to clipboard operation
marvin copied to clipboard

Loading Image from file raises ValueError

Open except-pass opened this issue 8 months ago • 0 comments

First check

  • [X] I added a descriptive title to this issue.
  • [X] I used the GitHub search to try to find a similar issue and didn't find one.
  • [X] 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 avatar May 30 '24 02:05 except-pass