(defunxah-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
)))
(defunxah-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 (xcmdxoutstr)
(setqxcmd
(concat"Add-Type -AssemblyName System.Drawing; $ximg=[System.Drawing.Image]::FromFile('"Filepath"'); Write-Output $ximg.Width , $ximg.Height "
))
(setqxoutstr (with-temp-buffer
(call-process-regionxcmdnil"pwsh"niltt"-Command""-")
(buffer-string)))
(split-stringxoutstr)))
(defunxah-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)))
(tnil))))
(defvarxah-imagemagick-pathnil"Full file path or program name of ImageMagick program. This is usually just magick, on Windows mac linux.")
(setqxah-imagemagick-path"magick")
(defunxah-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
(concatxah-imagemagick-path" identify -format \"%w %h\" "Filepath)))))
(if (eq 2 (lengthxsize))
xsize
(progn
(warn"image may be an animation, result may be wrong.")
(take 2 xsize)))))
(defunxah-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-extensionImgFilepath))) xitype)
(setqxitype
(cond
((string-equalxiext"jpg") 'jpeg)
((string-equalxiext"jpeg") 'jpeg)
((string-equalxiext"jfif") 'jpeg)
((string-equalxiext"png") 'png)
(t (internxiext))))
(if (image-type-available-pxitype)
(progn
(clear-image-cacheImgFilepath)
(let ((xsize (image-size (create-image (file-truenameImgFilepath)) t)))
(list (carxsize) (cdrxsize))))
nil)))
(defunxah-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
((eqsystem-type 'windows-nt) (xah-get-image-size-pwshFilepath))
((eqsystem-type 'darwin) (xah-get-image-size-sipsFilepath))
((eqsystem-type 'gnu/linux) (xah-get-image-size-linuxFilepath))
(t (xah-get-image-size-linuxFilepath))))
;; 2024-11-24
;; obsolete
(defvarxah-imagemagick-pathnil"Full file path or program name of ImageMagick program. This is usually just magick, on Windows mac linux.")
(setqxah-imagemagick-path"magick")
(defunxah-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
(concatxah-imagemagick-path" identify -format \"%w %h\" "Filepath)))))
(when (or (eqnilxwidthHeight) (not (eq 2 (lengthxwidthHeight))))
(warn"image may be an animation, this command xah-get-image-dimensions-imk may return incorrect width height."))
(vector
(string-to-number (eltxwidthHeight 0))
(string-to-number (eltxwidthHeight 1)))))
(defunxah-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-imkFilepath))