This page shows you a elisp code that lets you convert/scale marked image files in dired by a single key-press.
I want a command, so that i can convert all jpg files to png or any other format, for all marked files in dired. Also, same for scaling images.
For working on my website, i often need to convert images from any of gif/jpg/png formats, or resize them.
I have setup abbreviations so that when i type 8cvt and it automatically becomes convert -quality 85% .
Here's the steps when i need to convert a image file:
shell-command 【Meta+!】.I have another abbrev that expands to: find . -name "*png" | xargs -l -i basename "{}" ".png" | xargs -l -i convert -quality 85% "{}.png" "{}.jpg", which converts all png files to jpg in a dir. 〔☛ Using Emacs Abbrev Mode for Abbreviation〕
I've been using these methods for the past ≈4 years. Today, while working on a lot images, i got tired of the keystrokes. My elisp skill has quite improved since. So i wrote 2 short emacs commands to do these.
(defun scale-image (fileList scalePercentage) "Create a scaled jpg version of images of marked files in dired. The new names have “-s” appended before the file name extension. Requires ImageMagick shell tool." (interactive (list (dired-get-marked-files) (read-from-minibuffer "scale percentage:"))) (require 'dired) (mapc (lambda (ξf) (let ( newName cmdStr ) (setq newName (concat (file-name-sans-extension ξf) "-s" ".jpg") ) (while (file-exists-p newName) (setq newName (concat (file-name-sans-extension newName) "-s" (file-name-extension newName t))) ) ;; relative paths used to get around Windows/Cygwin path remapping problem (setq cmdStr (concat "convert -scale " scalePercentage "% -quality 85% " (file-relative-name ξf) " " (file-relative-name newName)) ) (shell-command cmdStr) )) fileList ))
(defun 2jpg (fileList) "Create a jpg version of images of marked files in dired. Requires ImageMagick shell tool. " (interactive (list (dired-get-marked-files) )) (require 'dired) (mapc (lambda (ξf) (let ( newName cmdStr ) (setq newName (concat (file-name-sans-extension ξf) ".jpg") ) (while (file-exists-p newName) (setq newName (concat (file-name-sans-extension newName) "-2" (file-name-extension newName t))) ) ;; relative paths used to get around Windows/Cygwin path remapping problem (setq cmdStr (concat "convert " (file-relative-name ξf) " " (file-relative-name newName)) ) ;; (async-shell-command cmdStr) (shell-command cmdStr) )) fileList ))
I give these commands a hotkey, for example F5. 〔☛ Emacs: How to Define Keys〕 So, i just press one key, and the file under cursor will be converted. If i want to convert multiple images, i mark them first. Emacs Power!
The weird ξ you see in my elisp code is Greek x. I use Unicode char in symbol name for easy distinction from builtin symbols. You can just ignore it. 〔☛ Programing Style: Variable Naming: English Words Considered Harmful〕
(defun 2zip () "Zip the current file/dir in `dired'. If multiple files are marked, only zip the first one. Require unix zip commandline tool." (interactive) (require 'dired) (let ( (fileName (elt (dired-get-marked-files) 0)) ) (shell-command (format "zip -r '%s.zip' '%s'" (file-relative-name fileName) (file-relative-name fileName))) ))