Skip to content

Loading Image from file raises ValueError #933

@except-pass

Description

@except-pass

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions