emacsorg-modeemacs24

Configuring emacs for showing fixed width inline images


Follow up to: Emacs org-display-inline-images

I managed to show inline images following abo-abo's advice. Now I want to set a fixed width size for all of them.

Setting (setq org-image-actual-width 50) didn't work. Emacs picked the variable, but it isn't doing anything to the images. The idea is to show them as thumbnails.

Note: I am using emacs 24.2 on Linux. I can show thumbs using M x image-dired, however, I want to show fixed size images under org mode. Normal images can already be shown.


Solution

  • I've looked at org-display-inline-images source: it's just calling create-image. It seems there's no scaling options at this moment.

    I've written a small work-around. It's a bit of a hack, but maybe you'd like to try it. What does: when org wants to display an image "~/cat.jpg", this functions makes it look if "~/catt.png" is present and show that instead. If "~/catt.png" isn't found, ImageMagick's convert is called to create it like so:

    convert ~/cat.jpg -thumbnail 300x300 ~/catt.png
    

    You can customize the thumb size and type and name if you want. And don't forget to have ImageMagick installed.

    (defun org-display-inline-images (&optional include-linked refresh beg end)
      "Display inline images.
    Normally only links without a description part are inlined, because this
    is how it will work for export.  When INCLUDE-LINKED is set, also links
    with a description part will be inlined.  This can be nice for a quick
    look at those images, but it does not reflect what exported files will look
    like.
    When REFRESH is set, refresh existing images between BEG and END.
    This will create new image displays only if necessary.
    BEG and END default to the buffer boundaries."
      (interactive "P")
      (unless refresh
        (org-remove-inline-images)
        (if (fboundp 'clear-image-cache) (clear-image-cache)))
      (save-excursion
        (save-restriction
          (widen)
          (setq beg (or beg (point-min)) end (or end (point-max)))
          (goto-char beg)
          (let ((re (concat "\\[\\[\\(\\(file:\\)\\|\\([./~]\\)\\)\\([^]\n]+?"
                (substring (org-image-file-name-regexp) 0 -2)
                "\\)\\]" (if include-linked "" "\\]")))
            old file ov img)
        (while (re-search-forward re end t)
          (setq old (get-char-property-and-overlay (match-beginning 1)
                               'org-image-overlay))
          (setq file (expand-file-name
                  (concat (or (match-string 3) "") (match-string 4))))
          (when (file-exists-p file)
                (let ((file-thumb (format "%s%st.png" (file-name-directory file) (file-name-base file) "t.png")))
                  (unless (file-exists-p file-thumb)
                    (shell-command (format "convert %s -thumbnail 300x300 %s"
                                           file file-thumb)))
            (if (and (car-safe old) refresh)
                (image-refresh (overlay-get (cdr old) 'display))
              (setq img (save-match-data (create-image file-thumb)))
              (when img
            (setq ov (make-overlay (match-beginning 0) (match-end 0)))
            (overlay-put ov 'display img)
            (overlay-put ov 'face 'default)
            (overlay-put ov 'org-image-overlay t)
            (overlay-put ov 'modification-hooks
                     (list 'org-display-inline-remove-overlay))
            (push ov org-inline-image-overlays))))))))))