Emacs Lisp: Optimize JPG images within Dired with help of ImageMagick

Emacs Lisp: Optimize JPG images within Dired with help of ImageMagick

This Emacs Lisp function is optimizing marked JPG images within Dired and GNU Emacs editor. It is using ImageMagick and web recommendation on how images shall be optimized. The quality of images will be 70% of the original and setttings for optimization are made for Internet. Do not use this function on images which quality shall remain intact for printing or publishing purposes others but web.

You may need this function to quote the shell file names which already contain the double quote.

´´´lisp (defun shell-double-quote (s) “Double quotes for the string that shall be fed to shell command” (replace-regexp-in-string “"” “\\"” s)) ´´´

Optimizing images quickly within Emacs and Dired (directory editing) is handy and useful especially with purpose to save the space and to publish images for web.

(defun optimize-image-jpg (file)
  "Optimizes the JPG image with quality 70%"
  (let ((extension (file-name-extension file)))
    (when (equalp extension "jpg")
      (let* ((file (shell-double-quote file))
         (command (format "mogrify -sampling-factor 4:2:0 -strip -interlace JPEG -colorspace RGB -quality 70 \"%s\"" file)))
    (message command)
    (shell-command command)))))

(defun optimize-jpg-images-dired ()
  "Optimizes JPG images inside of Dired"
  (interactive)
  (let ((files (dired-get-marked-files)))
    (dolist (file files)
      (optimize-image-jpg file))
    (revert-buffer)))

For any comments or suggestions feel free to contact GNU Support.

Leave Your Comment or Contact GNU.Support

Contact GNU.Support now. There is a simple rule at GNU.Support: if we can help you, we do, whenever and wherever necessary, and it's the way we've been doing business since 2002, and the only way we know


Full name:


E-mail:


Message: