Configuring emacs for showing fixed width inline images
Asked Answered
L

2

2

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.

Limpid answered 14/7, 2013 at 8:17 Comment(0)
R
2

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))))))))))
Replica answered 15/7, 2013 at 7:20 Comment(1)
Above solution looks OK, but I am followed the alternative of generating a thumb for all images with: mogrify -resize 80x80 -background white -gravity center -extent 80x80 -format jpg -quality 75 -path ../thumbs . and then linking to the original file. I would import just the thumb then, and open the original with the standard image viewer outside emacs.Limpid
H
2

org-image-actual-width will work so long as your Emacs is built with Imagemagick support. To check, evaluate (image-type-available-p 'imagemagick). It will evaluate to t if your Emacs has Imagemagick support.

Harte answered 16/7, 2019 at 12:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.