6

I would love to be able to see images in Markdown that are rescaled to fit into my window. I googled for the answer but could not find how to rescale images shown with iimage-mode in Markdown. I can see a solution for org-mode or an image open with Emacs. Any idea how to do this to an inline-image in Markdown?

Workaround for now: for i in *; do convert $i -resize 800x500\> ../imgs/$i; done I rescale images in the terminal and then use them in my notes.

enter image description here

2 Answers 2

2

Resizing images only works for images types supported by the imagemagick-library. The function imagemagick-types returns a list of these types.

EDIT: Input M-: (setq val (imagemagick-types)) RET and C-h v val RET to check what the function returns. If the value of val is nil imagemagick is not enabled and you are out of luck. If the value is a list look whether your image type is member of that list.

For enabling all types supported by imagemagick set imagemagick-enabled-types to t. You can do that with M-x customize-variable RET imagemagick-enabled-types RET. Just use the option Support all ImageMagick types from the Value menu.

If this prerequisites are fulfilled you can use the following lisp code to adapt the image width to the window width in markdown-buffers.

(setq imagemagick-enabled-types t) 

(defun image-p (obj)
  "Return non-nil if OBJ is an image"
  (eq (car-safe obj) 'image))


(defun iimage-scale-to-fit-width ()
  "Scale over-sized images in the active buffer to the width of the currently selected window.
  (imagemagick must be enabled)"
  (interactive)
  (let ((max-width (window-width (selected-window) t)))
    (alter-text-property (point-min) (point-max)
                         'display
                         (lambda (prop)
                           (when (image-p prop)
                             (plist-put (cdr prop) :type 'imagemagick)
                             (plist-put (cdr prop) :max-width max-width)
                             prop)))))

(defun iimage-scale-on-window-configuration-change ()
  "Hook function for major mode that display inline images:
Adapt image size via `iimage-scale-to-fit-width' when the window size changes."
  (add-hook 'window-configuration-change-hook #'iimage-scale-to-fit-width t t))

(add-hook 'markdown-mode-hook #'iimage-scale-on-window-configuration-change)
2
  • Thanks, Tobias. I tried to use this but I can not get it to run. I see iimage-scale-to-fit-width function when I M-x iimage-<tab>, so it seems that it's loaded properly. However, when I switch on the iimage mode, M-x iimage-mode nothing changes (I don't get any images inserted). I don't now lisp thus I don't know what kind of information would help you to understand the problem. Jul 13, 2017 at 14:33
  • @MarcinMagnus I added instructions how to test the return value of imagemagick-types and how to switch on the support of all types.
    – Tobias
    Jul 14, 2017 at 7:10
1

The newest markdown-mode introduced markdown-max-image-size for resizing large images. It does the job! Emacs has to be compiled with ImageMagick support (--with-imagemagick).

2
  • legend! Works on my windows machine for crying out loud! C-h v markdown-max-image-size --> customize --> Change Maximum width in pixels, NO NEED RESTART OR ANYTHING, JUST TOGGLE THE IMAGES AND IT WORKS. :) I am using GNU Emacs 27.1 (build 1, x86_64-w64-mingw32) of 2020-08-21 with windows 10.
    – Pandian Le
    Oct 12, 2021 at 13:39
  • P.S. I did not do any special compile with ImageMagick support.
    – Pandian Le
    Oct 12, 2021 at 13:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.