8

I use eww to open an image from wikipedia:

https://upload.wikimedia.org/wikipedia/commons/6/6a/The_Burning_of_the_Houses_of_Parliament.jpg

Now, I wanted to save it to my computer.

How can I do that?

Drew
  • 75,699
  • 9
  • 109
  • 225
e19293001
  • 207
  • 1
  • 9

1 Answers1

5

As far as I can tell *eww* buffers store image data in the display text property.

So, to save an image we need to get this property and save it to a file.

Here is a sketch of a solution.

(defun my-eww-save-image (filename)
  "Save an image opened in an *eww* buffer to a file."
  (interactive "G")
  (let ((image (get-text-property (point-min) 'display)))
    (with-temp-buffer
      (setq buffer-file-name filename)
      (insert
       (plist-get (if (eq (car image) 'image) (cdr image)) :data))
      (save-buffer))))

This code seems to work in the case I tested it on, but it has at least two flaws:

  • It cannot detect the file type or the file name, so you have to specify the file name with an extension.
  • save-buffer calls hooks in before-save-hooks and after-save-hooks, which does not really make sense here.

If you know how to improve this, please go right ahead.

Constantine
  • 9,072
  • 1
  • 34
  • 49