(defunxah-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 (xcmdxoutstr)
(setqxcmd
(format"
import sys
from PIL import Image
ximage = Image.open(\"%s\")
xwidth, xheight = ximage.size
print(xwidth, xheight)"zimg-path))
(setqxoutstr (with-temp-buffer
(call-process-regionxcmdnil"python"niltt"-")
(buffer-string)))
(split-stringxoutstr)))
(defunxah-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-argumentzimg-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
)))
(defunxah-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-argumentzimg-path))))))
(if (length=xsize 2)
xsize
(progn
(warn"image may be an animation, result may be wrong.")
(take 2 xsize)))))
(defunxah-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$" (downcasezimg-path))) (user-error"image file suffix is not png or jpg"))
(let (xcmdxoutstr)
(setqxcmd
(concat"Add-Type -AssemblyName System.Drawing; $ximg=[System.Drawing.Image]::FromFile('"zimg-path"'); Write-Output $ximg.Width , $ximg.Height "
))
(setqxoutstr (with-temp-buffer
(call-process-regionxcmdnil"PowerShell"niltt"-Command""-")
(buffer-string)))
(split-stringxoutstr)))
(defunxah-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)))
(tnil))))
(defunxah-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
((eqsystem-type 'windows-nt)
;; (xah-get-image-size-imagemagick zimg-path)
(xah-get-image-size-pythonzimg-path)
;; (xah-get-image-size-pwsh zimg-path)
)
((eqsystem-type 'darwin) (xah-get-image-size-sipszimg-path))
((eqsystem-type 'gnu/linux) (xah-get-image-size-linuxzimg-path))
(t (xah-get-image-size-linuxzimg-path))))