libvips/nip2

How to modify the basename and extension of a pathname?

larsmaxfield opened this issue · 4 comments

Hi @jcupitt,

Are there functions by which one can modify the basename and extension of a pathname? Or perhaps an approach by which to do this?

I'm continuing my hunt for a GUI-based route on Windows to convert image files with vips (see #109). The specific user need here is to convert a single image file to a pyramidal TIFF and save it with a suffix in its basename. For example, "Fred.jpg" would become "Fred_pyr.tiff".

Here's a screenshot of what I'm envisioning, though eventually using the View / No Edits mode for users as you suggested:

image

Or perhaps this can be done by biting off the end of the pathname and then adding _pyr.tiff to it. For example, chaining together functions which:

  • split the pathname into a list at the .;
    [".../basename" "." "ext"])
  • remove the . and remaining entities from that list;
    [".../basename"])
  • append the suffix _pyr and extension .tiff to that list; and
    [".../basename" "_pyr" ".tiff"])
  • concatenate that list.
    [".../basename_pyr.tiff"]

Any thoughts on this? I'm not familiar with C, so I've been spinning my wheels a bit.

Hiya,

nip2's programming language is something like haskell, if you know that. You could use split (equal '.') to split on period characters, then use last to get the final part (the suffix). init gets all of a list except the final component, so use that and foldr1 (join '.') to reassemble the initial part.

fred = "/a/b/hello.banana"
split (equal '.') fred == ["/a/b/hello", "banana"]
last (split (equal '.') fred) == "banana"
foldr1 (join '.') (init (split (equal '.') fred)) == "/a/b/hello"

Have you found Toolkits / Edit? Select View / Definition browser in the edit window and there's a handy thing for looking though (and editing if you want haha) the standard library:

image

If you click on an identifier in the main edit area, it'll display all matching definitions on the right. Click one of those to see the source.

There are also per-workspace definitions, so you can write private functions as helpers for the current tab that get saved inside the workspace file.

image

So that's adding a function called split_filename which you can use within that tab.

... you can write image processing functions in the same language, edit their definition, and watch all your images update. You can zoom in on individual pixels and see how they are being modified as you type. It's nice for development.

Very slick. Hadn't seen the Definition browser yet.

And those commands for splitting the filename do indeed make sense using the definitions and source as context alongside your explanation. Thank you.

Will experiment (and discover) further ;-)