Elisp: Get Image Size πŸš€

By Xah Lee. Date: . Last updated: .
(defun xah-get-image-size-sips (Filepath)
  "Returns a (list width height) of a image's size.
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: 2024-11-22"
  (interactive (list (read-file-name "Image file name:")))
  (let ((xout (shell-command-to-string (concat "sips -g pixelWidth -g pixelHeight " Filepath))))
    ;; (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-pwsh (Filepath)
  "Returns a (list width height) of a image's size.
The elements are strings.
Calls pwsh on Microsoft Windows. (am not sure it works if on linux or mac)

URL `http://xahlee.info/emacs/emacs/elisp_get_image_width_height.html'
Created: 2024-11-22
Version: 2024-11-23"
  (interactive (list (read-file-name "Image file name:")))
  (let (xcmd xoutstr)
    (setq xcmd
          (concat
           "Add-Type -AssemblyName System.Drawing; $ximg=[System.Drawing.Image]::FromFile('"
           Filepath
           "');  Write-Output $ximg.Width , $ximg.Height "
           ))
    (setq xoutstr (with-temp-buffer
                    (call-process-region xcmd nil "pwsh" nil t t "-Command" "-")
                    (buffer-string)))
    (split-string xoutstr)))

(defun xah-get-image-size-linux (Filepath)
  "Returns a (list width height) of a image's size.
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 " Filepath)))
        ;; (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))))

(defvar xah-imagemagick-path nil "Full file path or program name of ImageMagick program. This is usually just magick, on Windows mac linux.")
(setq xah-imagemagick-path "magick")

(defun xah-get-image-size-imagemagick (Filepath)
  "Returns a image file's width and height as a vector.
Each element is a string type.

Requires the shell command imagemagick at variable `xah-imagemagick-path'.

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: 2024-11-24"
  (let ((xsize
         (split-string
          (shell-command-to-string
           (concat xah-imagemagick-path " identify -format \"%w %h\" " Filepath)))))
    (if (eq 2 (length xsize))
        xsize
      (progn
        (warn "image may be an animation, result may be wrong.")
        (take 2 xsize)))))

(defun xah-get-image-size-elisp (ImgFilepath)
  "Returns (list width height) of a image.
The elements are integer type.
Support png jpg svg gif and any image type emacs supports.
If the image is not supported, return nil.

;; 2024-11-20 todo. found a major bug. the size generated is not correct. it varies with the current emacs frame.
;; GNU Emacs 29.4 (build 2, x86_64-w64-mingw32) of 2024-07-05

Created: 2024-11-21
Version: 2024-11-24"
  (let ((xiext (downcase (file-name-extension ImgFilepath))) xitype)
    (setq xitype
          (cond
           ((string-equal xiext "jpg") 'jpeg)
           ((string-equal xiext "jpeg") 'jpeg)
           ((string-equal xiext "jfif") 'jpeg)
           ((string-equal xiext "png") 'png)
           (t (intern xiext))))
    (if (image-type-available-p xitype)
        (progn
          (clear-image-cache ImgFilepath)
          (let ((xsize (image-size (create-image (file-truename ImgFilepath)) t)))
            (list (car xsize) (cdr xsize))))
      nil)))

(defun xah-get-image-size (Filepath)
  "Returns a (list width height) of a image's size.
The elements are strings.
If there's an error, return nil.

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

(defvar xah-imagemagick-path nil "Full file path or program name of ImageMagick program. This is usually just magick, on Windows mac linux.")
(setq xah-imagemagick-path "magick")

(defun xah-get-image-dimensions-imk (Filepath)
  "Returns a image file's width and height as a vector.
Each element is a integer.
Requires the shell command imagemagick at variable `xah-imagemagick-path'.
See also: `xah-get-image-dimensions'.

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: 2015-05-12
Version: 2024-11-21"
  (let ((xwidthHeight
         (split-string
          (shell-command-to-string
           (concat xah-imagemagick-path " identify -format \"%w %h\" " Filepath)))))
    (when (or (eq nil xwidthHeight) (not (eq 2 (length xwidthHeight))))
      (warn "image may be an animation, this command xah-get-image-dimensions-imk may return incorrect width height."))
    (vector
     (string-to-number (elt xwidthHeight 0))
     (string-to-number (elt xwidthHeight 1)))))

(defun xah-get-image-dimensions (Filepath)
  "Returns a vector [width height] of a image's dimension.
The elements are integer datatype.
Support png jpg svg gif and any image type emacs supports.
if not supported image type, calls `xah-get-image-dimensions-imk'.

;; 2024-11-20 todo. found a major bug. the size generated is not correct. it varies with the current emacs frame. for now, call `xah-get-image-dimensions-imk' instead.
see
URL `http://xahlee.info/emacs/misc/elisp_bug_get_image_width_height.html'

URL `http://xahlee.info/emacs/emacs/elisp_get_image_width_height.html'
Created: 2017-01-11
Version: 2024-11-21"
  (xah-get-image-dimensions-imk Filepath))

Get Image Width Height