A new, faster Emacs package to do image triage

One thing that’s annoyed me for yonks is how slow Emacs is when displaying images. Emacs has never been fast on this point, exactly, and has gotten better over the years — but at the same time, image sizes have exploded. In the bad old days, images were just a couple of megabytes, but with modern cameras, images can be 25MB, easily.

So now it’s really painful to page through directories of images:

It takes half a second for each image, and if you don’t find that annoying, you’re way more patient than I am.

Those are from my camera, and I’ve adjusted the image size down to “just” 10MB — because it’s just too painful to deal with 25MB images all around. For instance, when posting a 25MB file to WordPress, it uses about half a minute to scale it down to various sizes. Not fun.

This has, in the past, led me to use other non-Emacs ways of looking through images. So horrible! When you find images you want to copy an image somewhere else, for instance, you have to go through all these non-ergonomic contortions… It’s a complete nightmare! Have you people seen non-Emacs interfaces!? They’re awful!

Now, Emacs has things like image-dired that will go through a directory and create thumbnails, which makes things more liveable. But it does that by creating thumbnails up-front, which I just don’t like. And it doesn’t really help with the display of the images themselves when you want to look at them.

Now, what’s the baseline here?

larsi@corrigan:/home/sony/ftp$ time djpeg DSC09468.JPG >/dev/null

real	0m0.128s

Well, just decoding one of these 10MB images takes more than a tenth of a second. Emacs also resizes images before display, so this is perhaps a more fair comparison:

larsi@corrigan:/home/sony/ftp$ time convert -resize 1500x DSC09468.JPG jpg:- >/dev/null

real	0m0.264s

So — large images just take a lot of time anyway, and there’s not much we can do about that.

But what if we cheated?

Whoa. No, those li’l images haven’t been resized and cached (like image-dired does) — they’re just extracted from the exif metadata: All modern cameras store a thumbnail inside the images. So I’m just extracting them and displaying them on-the-fly. As you can see from the screencast there, it’s so fast that even if I jump randomly around in the buffer, it keeps up.

(It inserts thumbnails for the lines displayed in the window, and then the next and a half window’s worth, thinking that you’re presumably going to scroll down. And then whenever you move around, it insert the thumbnails for the page you’re currently watching. So we’re not wasting preciouses electricity on things that are never going to be shown — my test directory here has 8K images.)

If the images don’t contain thumbnails, the new mode does things the hard way — by calling convert to scale the images down. This is, of course, a lot slower, but things run asynchronously, anyway, so the UX is UXing, as they say.

(What’s that? They don’t? They don’t say that? They should!)

But what about looking at the pictures in full, then? That’s as slow as before, surely?

Let’s have a look at what’s in the JPEG:

larsi@corrigan:/home/sony/ftp$ exiftool -json -a -G1 -s -preview:all DSC09471.JPG
[{
  "SourceFile": "DSC09471.JPG",
  "IFD1:ThumbnailImage": "(Binary data 7453 bytes, use -b option to extract)",
  "MPImage2:PreviewImage": "(Binary data 287215 bytes, use -b option to extract)"
}]

There’s a 1616×1080 “preview” image embedded in there, too!

(Side note — amusing JSON format here, with the size of the image (in bytes) embedded in a string. Which also includes helpful command line options. Such JSON.)

So:

Emacs displays these previews so fast without doing any magic that it refreshes faster than I can hit n.

If there are no thumbnails, the mode precomputes the next dozen images, so you only see the slowness if you never take any pauses. Lay off that n finger, man! Not cool!

(Or rather, it will do that tomorrow — my fingers got too tired from typing all day long…)

In addition, the new mode also allows looking at entire hierarchies with M-x image-triage-tree, which is useful when doing image archaeology.

The useful Dired commands are present in this mode, too — you can mark images (both from the listing view and the per-image buffer), and then copy them over somewhere. (This was the main reason I wrote this — I had a bunch of old images I wanted to go over and copy over the useful ones (therefore the name image-triage).)

(defvar-keymap image-triage-list-mode-map
  "RET" #'image-triage-display-image
  "SPC" #'image-triage-list-toggle-mark
  "m" #'image-triage-list-mark
  "U" #'image-triage-list-unmark-all
  "u" #'image-triage-list-unmark
  "g" #'image-triage-list-reload
  "v" #'image-triage-list-view-in-dired
  "C" #'image-triage-list-copy
  "D" #'image-triage-list-delete
  "s" #'image-triage-list-toggle-sort)

It’s not feature complete yet, I guess… but seems to work fine for my use case, at least.

And I wrote the mode by hand! So there! Except that I got Claude to write a helper command in C to dump thumbnails, because exiftool is just too slow:

So the new C programme uses half a millisecond to extract the thumbnail. exiftool uses… er… let’s see… carry the one… 0.072s to do it. Is that right? Yeah, I guess — exiftool is a Perl script, after all. So the C program is… 144x faster. 🫠

Now you’re asking yourself — hasn’t somebody already written a thumbnail extractor that you can just apt install instead? Yes, exiv2 exists and is fast… but can only extract thumbnails, and not previews. And! It can’t extract to stdout — it insists on writing to the same directory as the image is in, so it’s a pain to deal with. So whatchagonnado.

(Typical Linux situation — one solution that’s general but slow, and one solution that’s fast but outdated and awkward to use.)

But I understand many people are opposed to using LLMs, so you don’t need to compile the extra binary. In that case, things look like this:

Not as impressive, but I guess it’s usable — it’s asynchronous, after all.

So there you go.

I’ve put the code on Microsoft Github, so you can give it a whirl if you want.

“”Wow””

There’s a good article today in kode24 called “I can’t be bothered to hear about that vibe coded hobby project of yours”:

But as with all AI-generated content, the problem is that people won’t bother spending time on something you didn’t bother spending time on yourself.

[…]

Then, of course, there are a few instances where you simply cannot find what you need. That is why some argue that the true value of “vibbe-coding” lies in creating your own tools for your own personal use. And they might well be right.

Because the beauty of it is that you don’t have to tell anyone else about them.

So true, so true — I agree 100%.

Anyway, a couple decades ago, probably while falling asleep, I imagined a silly FFT display of the music playing. You know — one of those things that move along with the beat and the frequencies like all the Winamp plugins had.

I’m not sure quite why I had that idea, because I’m against having displays with lots of action in general — I hate distractions.

But I had an idea for how it would look, and I thought it might look, for want of a better word, “cool”.

So I started working on it, and then I realised that 1) I know nothing about the ALSA sound system, and 2) I know little about Fourier and maths in general, and 5) I know very little about X programming.

So I was the person least suited to doing this in the world, and the project stalled pretty quickly.

This is my excellent drawing to illustrate what I wanted it to look like — I was thinking about a circle with red lines, but then I remembered that I wanted the lines to be more radial.

After the previous success with Just Have Claude Do It yesterday, I thought of just asking it to do this one, too, and half an hour later…

And with some tweaking:

I guess with stupid hobby projects like this you used to be able to say “well, this may be stupid, but at least I made it! And now I know how to do X programming!”

Well, this project may be stupid, but I didn’t make it, and I’m even stupider than when I started it, I guess. So that’s a major difference between doing silly programming yourself and outsourcing it to an LLM.

Still… the results are amusing, I think? And I would not actually have done this myself.

You can find the results on Microsoft Github, but I guess you can just ask Claude to make you one yourself — that’s probably easier than using this code…

Let’s Split (Vinyl)

Ever since the late 90s, I’ve sampled vinyl records (and ripped CDs) before listening to them — because I’m bone lazy, and I don’t like running back and forth to the stereo every twenty minutes to flip the record over.

There’s a lot of software and APIs in this area to allow you to automate a lot of this. There was the CD CDDB database of CD data that you could query to rename tracks after ripping them, for instance.

But one thing that didn’t exist back then was good software to split sampled vinyl albums into tracks. So I wrote a programme myself around 1997, and I’ve been using it ever since, and after splitting I use the Discogs API to rename tracks automatically.

(I did some research about ten years ago into whether something better had shown up, and the answer seemed to be “yes” — but deep inside software that wanted to handle all this stuff, not just “split it already and leave me alone”.)

The vsplit programme I wrote is pretty simple: It takes as input a “noise floor” level, and then splits when you have a noise floor that’s been low enough for long enough. (The noise floor is constant across records, I find, but varies between record players.)

This handles, say — 80% of albums, I’m guesstimating? And the rest I’ve used an Emacs mode for graphically (but manually) splitting the albums.

But for a decade and a half I’ve had this idea: I know how many tracks there are on an album. So surely one could write some kind of “adaptive algorithm” to use this extra information to inform the splitting? That is, if we’ve found 15 split points using the first algo, and we know that there are only 5 tracks, then there have to be some tracks that have pretty silent bits embedded, and we should append those bits to each other.

Or something.

A decade ago I started writing something along those lines, but I just had to give up: It was too much back and forth with testing and retesting — once I had gotten something to work on one album, it broke on a different album, and then I had to tediously listen to what’s up with that, and and and I just threw my hands up and left well enough alone.

But now we have LLMs:

And after uploading several albums [REMEMBER TO EDIT THIS OUT BEFORE POSTING BECAUSE IT SOUNDS REALLY ILLEGAL] to it — some that have straightforward silent bits between tracks and some that are more free-flowing:

And finally:

I’ve now got something that seems to work, like, 99% of the time! This final album I gave it was a real brain teaser — it has one track that doesn’t really have a pause between two tracks, so you just have to make the algo pick a random point.

Like, I’m impressed — I wasn’t sure whether it was going to get there or not, but:

And everything seems like it’s working A-OK.

vsplit.c went from 300 lines to 750 lines, which was less than I expected. 🫠

I’m really not one of those RAH RAH AI AI people (really not! I swear!), but this is just such a perfect example of LLMs doing programming work that (demonstrably!) I’m either not able to do or just can’t be bothered to do. Probably the latter, but there’s not that much difference in practice, now is there?

I’ve put the resulting thing on Microsoft Github. You’re welcome.

How much userspace does one need for a Linux machine anyway?

A joking comment on my post about Emacs being pretty small these days said something like “130MB just to show a PNG”… which made me wonder what the userland on this Raspberry Pi Zero was like, anyway.

Above you see a list sorted by the resident size of the processes.

The biggest thing, at 29MB, is systemd-journal — which isn’t really necessary on a device like this, but nice to have.

Next up is exim4 at 17MB, which is totally unnecessary — this system is not going to be receiving mail (or even sending it).

NetworkManager (at 10MB) sounds necessary, but isn’t — one can just plop some stuff down in /etc/network/interfaces instead.

Then we have three instances of sshd (at 10MB and down), which is because I’m ssh‘d in — there’s normally just two otherwise? But necessary.

Then there’s stuff that sounds necessary, but ModemManager (!) at 4MB certainly isn’t, and I don’t think polkitd is either (4MB).

bluetoothd (3MB) is not necessary, and neither is avahid (two processes, 4MB altogether).

And then the rest (like d are the processes that update the screen and stuff) look necessary.

But that’s (+ 29 17 10 4 4 3 4) => 71MB of stuff that does not need to be running, I guess.

Which is more than the size of the Emacs process, anyway.

(There are obviously super duper optimised Linux images that run on actually constrained devices, but I was just curious as to how much a normal Debian image could be shaved down.)

Not that I’m going to be pruning processes — there’s plenty of space to go around, but it just shows how far off my initial supposition that the device was too limited to run Emacs locally was.