Function: image-convert

image-convert is a byte-compiled function defined in image-converter.el.gz.

Signature

(image-convert IMAGE &optional IMAGE-FORMAT)

Documentation

Convert IMAGE file to the PNG format.

IMAGE can either be a file name or image data.

To pass in image data, IMAGE should a string containing the image data, and IMAGE-FORMAT should be a symbol with a MIME format name like "image/webp". For instance:

  (image-convert data-string 'image/bmp)

IMAGE can also be an image object as returned by create-image.

This function converts the image to PNG, and the converted image data is returned as a string.

Source Code

;; Defined in /usr/src/emacs/lisp/image/image-converter.el.gz
(defun image-convert (image &optional image-format)
  "Convert IMAGE file to the PNG format.
IMAGE can either be a file name or image data.

To pass in image data, IMAGE should a string containing the image
data, and IMAGE-FORMAT should be a symbol with a MIME format name
like \"image/webp\".  For instance:

  (image-convert data-string 'image/bmp)

IMAGE can also be an image object as returned by `create-image'.

This function converts the image to PNG, and the converted image
data is returned as a string."
  ;; Find an installed image converter.
  (unless image-converter
    (image-converter--find-converter))
  (unless image-converter
    (error "No external image converters available"))
  (when (and image-format
             (not (= (length (split-string (symbol-name image-format) "/")) 2)))
    (error "IMAGE-FORMAT should be a symbol like `image/png'"))
  (with-temp-buffer
    (set-buffer-multibyte nil)
    (when-let ((err (image-converter--convert
                     image-converter
                     (if (listp image)
                         (plist-get (cdr image) :file)
                       image)
                     (if (listp image)
                         (plist-get (cdr image) :data-p)
                       image-format))))
      (error "%s" err))
    (if (listp image)
        ;; Return an image object that's the same as we were passed,
        ;; but ignore the :type value.
        (apply #'create-image (buffer-string) 'png t
               (cl-loop for (key val) on (cdr image) by #'cddr
                        unless (eq key :type)
                        append (list key val)))
      (buffer-string))))