Elisp: get image width height πŸ“œ

By Xah Lee. Date: . Last updated: .

Get Image Size

(defun xah-get-image-size-python (zimg-path)
  "Returns image width height as a list.
The elements are strings.

this command calls python. and you must have python PIL installed.
https://pypi.org/project/pillow/
pip install pillow

URL `http://xahlee.info/emacs/emacs/elisp_get_image_width_height.html'
Created: 2026-06-20
Version: 2026-08-03"
  (interactive (list (read-file-name "Image file name:")))
  (let (xcmd xoutstr)
    (setq xcmd
          (format "
import sys
from PIL import Image
ximage = Image.open(\"%s\")
xwidth, xheight = ximage.size
print(xwidth, xheight)"
                  zimg-path))
    (setq xoutstr (with-temp-buffer
                    (call-process-region xcmd nil "python" nil t t "-")
                    (buffer-string)))
    (split-string xoutstr)))
(defun xah-get-image-size-sips (zimg-path)
  "Returns image width height as a list.
The elements are strings.
Calls macOS sips command.

If there's an error, return nil.

URL `http://xahlee.info/emacs/emacs/elisp_get_image_width_height.html'
Created: 2024-11-22
Version: 2025-02-06"
  (interactive (list (read-file-name "Image file name:")))
  (let ((xout (shell-command-to-string (concat "sips -g pixelWidth -g pixelHeight " (shell-quote-argument zimg-path)))))
    ;; (setq xout "  pixelWidth: 1024\n  pixelHeight: 768")
    (if (string-match "pixelWidth: \\([0-9]+\\)[ \n]+pixelHeight: \\([0-9]+\\)" xout)
        (list (match-string 1 xout) (match-string 2 xout))
      nil
      )))
(defun xah-get-image-size-imagemagick (zimg-path)
  "Returns image width height as a list.
Each element is a string type.

Calls the shell command magick.

2024-11-20 xtodo bug. if the image is gif animation or webp animation, the result is not correct.

URL `http://xahlee.info/emacs/emacs/elisp_get_image_width_height.html'
Created: 2024-11-24
Version: 2025-02-06"
  (let ((xsize
         (split-string
          (shell-command-to-string
           (concat "magick identify -format \"%w %h\" " (shell-quote-argument zimg-path))))))
    (if (length= xsize 2)
        xsize
      (progn
        (warn "image may be an animation, result may be wrong.")
        (take 2 xsize)))))
(defun xah-get-image-size-pwsh (zimg-path)
  "Returns image width height as a list.
The elements are strings.
Calls pwsh on Microsoft Windows. (am not sure it works if on linux or mac)

2026-06-20 xtodo. does not support webp.

URL `http://xahlee.info/emacs/emacs/elisp_get_image_width_height.html'
Created: 2024-11-22
Version: 2025-01-01"
  (interactive (list (read-file-name "Image file name:")))
  (when (not (string-match "png$\\|jpg$\\|jpeg$" (downcase zimg-path))) (user-error "image file suffix is not png or jpg"))
  (let (xcmd xoutstr)
    (setq xcmd
          (concat
           "Add-Type -AssemblyName System.Drawing; $ximg=[System.Drawing.Image]::FromFile('"
           zimg-path
           "');  Write-Output $ximg.Width , $ximg.Height "
           ))
    (setq xoutstr (with-temp-buffer
                    (call-process-region xcmd nil "PowerShell" nil t t "-Command" "-")
                    (buffer-string)))
    (split-string xoutstr)))
(defun xah-get-image-size-linux (zimg-path)
  "Returns image width height as a list.
The elements are strings.
Calls unix γ€Œfile」 command.

If the image is not jpeg or png, return nil.

URL `http://xahlee.info/emacs/emacs/elisp_get_image_width_height.html'
Created: 2024-11-22
Version: 2024-11-22"
  (interactive (list (read-file-name "Image file name:")))
  (let ((xout (shell-command-to-string (concat "file " zimg-path)))
        ;; (xout "xx.png: PNG image data, 1558 x 1022, 8-bit/color RGBA, non-interlaced")
        ;; (xout "xx.jpg: JPEG image data, JFIF standard 1.01, aspect ratio, density 1x1, segment length 16, baseline, precision 8, 1024x768, components 3")
        )
    (cond
     ((string-match "PNG image data, \\([0-9]+\\) x \\([0-9]+\\)," xout)
      (list (match-string 1 xout) (match-string 2 xout)))
     ((string-match "JPEG image data, .+, \\([0-9]+\\)x\\([0-9]+\\)" xout)
      (list (match-string 1 xout) (match-string 2 xout)))
     ((string-match "\\([0-9]+\\)x\\([0-9]+\\)" xout)
      (list (match-string 1 xout) (match-string 2 xout)))
     (t nil))))
(defun xah-get-image-size (zimg-path)
  "Returns image width height as a list.
The elements are strings.
If there's an error, return nil.

if one Microsoft Windows, you need python installed. it calls `xah-get-image-size-python'.

URL `http://xahlee.info/emacs/emacs/elisp_get_image_width_height.html'
Created: 2024-11-22
Version: 2026-06-20"
  (interactive (list (read-file-name "Image file name:")))
  (cond
   ((eq system-type 'windows-nt)
    ;; (xah-get-image-size-imagemagick zimg-path)
    (xah-get-image-size-python zimg-path)
    ;; (xah-get-image-size-pwsh zimg-path)
    )
   ((eq system-type 'darwin) (xah-get-image-size-sips zimg-path))
   ((eq system-type 'gnu/linux) (xah-get-image-size-linux zimg-path))
   (t (xah-get-image-size-linux zimg-path))))

obsolete

Get Image Width Height