Imago是通用LISP的图像操纵库。它支持PNG,PCX,便携式位图(.pnm),TrueVision TGA(.TGA)和JPEG格式中的图像。您可以使用imago:read-image ,并使用imago:write-format编写图像,其中format是png , pcx , pnm , tga或jpg之一。
您可以使用更高级的Libjpeg-Turbo库来通过加载imago/jpeg-turbo系统来处理JPEG文件。确保系统上安装了libjpeg-turbo 。使用imago-jpeg-turbo:read-jpg-turbo和imago-jpeg-turbo:write-jpg-turbo函数(或只是imago:read-image和imago:write-image )来使用此功能。
您可以使用更高级和更快的pngload库来通过加载imago/pngio系统来读取PNG文件。使用imago-pngio:read-png (或只是imago:read-image )来使用此功能。 NB: pngload会自动将索引的颜色图像转换为RGB(或ARGB)图像。如果要使用索引图像,请改用旧的PNG加载程序。 ZPNG(通过imago-pngio:write-png )库也将用于保存PNG图像。
要创建图像,请查看图像类别的文档(例如imago:rgb-image或imago:grayscale-image )。您需要通过传递:w , :h和可选的:initial-color关键字参数以make-instance 。另外,您可以使用make-XXX-image和make-XXX-image-from-pixels功能:
;; Create 100x100 px black grayscale image
(imago:make-grayscale-image 100 100)
;; Create 400x100 px red RGB image
(imago:make-rgb-image 400 100 (imago:make-color 255 0 0))
;; Create 400x100 px half-transparent red RGB image
(imago:make-rgb-image 400 100 (imago:make-color 255 0 0 127))
;; Create an image from an array of pixels
(imago:make-rgb-image-from-pixels
(make-array '(100 100)
:element-type 'imago:rgb-pixel
:initial-element (imago:make-color 0 255 255)))
大多数示例都是从这里开始的。
(resize *image* 400 150)
| 原来的 | 处理 |
|---|---|
![]() | ![]() |
(rotate *image* 45)
| 原来的 | 处理 |
|---|---|
![]() | ![]() |
(emboss *image* :angle (* pi 0.75) :depth 1.0)
| 原来的 | 处理 |
|---|---|
![]() | ![]() |
(let ((kernel #2A((0 0 0 0 0)
(0 0 1 0 0)
(0 1 -4 1 0)
(0 0 1 0 0)
(0 0 0 0 0))))
(convolve *image* kernel 1 0))
| 原来的 | 处理 |
|---|---|
![]() | ![]() |
(do-region-pixels (*image* color x y 70 65 140 125)
(setf color (invert-color color)))
| 原来的 | 处理 |
|---|---|
![]() | ![]() |
(enhance-contrast *grayscale-image*)
| 原来的 | 处理 |
|---|---|
![]() | ![]() |
(do-image-pixels (*image* color x y)
(multiple-value-bind (r g b) (color-rgb color)
(setf color (make-color b
(floor (* g 0.8))
r))))
| 原来的 | 处理 |
|---|---|
![]() | ![]() |
(let ((operator (default-compose-operator *image1*)))
(compose nil *image1* *image2* 20 20 operator))
| 原始1 | 原始2 | 处理 |
|---|---|---|
![]() | ![]() | ![]() |
(let ((points '(83 45 73 150 73 150 198 106 198 106 83 45)))
(draw-polygon *image* points +white+ :closed t))
(draw-circle *image* 83 45 15 +white+)
(draw-circle *image* 73 150 15 +white+)
(draw-circle *image* 198 106 15 +white+)
(draw-bezier-curve *image* 10 80 150 60 100 170 200 170 +red+)
(draw-line *image* 0 5 254 5 +yellow+)
(draw-line *image* 0 10 254 10 +yellow+ :dash-length 1 :dash-interval 1)
(draw-line *image* 0 15 254 15 +yellow+ :dash-length 4 :dash-interval 2)
| 原来的 | 处理 |
|---|---|
![]() | ![]() |
(defun sea-view (image)
(let ((image2 (flip nil image :horizontal)))
(do-image-pixels (image2 color x y)
(multiple-value-bind (r g b)
(color-rgb color)
(setf color (make-color (floor r 3) (floor g 3) (floor b 2)))))
(let* ((width (image-width image))
(height (image-height image))
(result (make-instance (class-of image)
:width width :height (* height 2))))
(copy result image)
(copy result image2 :dest-y height)
result)))
| 原来的 | 处理 |
|---|---|
![]() | ![]() |
此示例需要蛇和数组操作系统(在QuickLisp中可用)。
(defpackage components-example
(:use #:cl
#:snakes
#:imago)
(:export #:convert-to-image))
(in-package :components-example)
(defgenerator generate-colors ()
(loop while t do
(yield (make-color (random 256)
(random 256)
(random 256)))))
(defgenerator black ()
(yield (make-color 0 0 0)))
(defun convert-to-image (components)
(declare (type (simple-array fixnum (* *)) components))
(let ((colors (take (1+ (reduce #'max (aops:flatten components)))
(chain (black)
(generate-colors))))
(image (make-array (array-dimensions components)
:element-type 'rgb-pixel)))
(array-operations/utilities:nested-loop (i j)
(array-dimensions components)
(setf (aref image i j)
(nth (aref components i j) colors)))
(make-instance 'rgb-image :pixels image)))
(in-package :cl-user)
(let* ((image (imago:read-image "~/.quicklisp/local-projects/imago/tests/spheres.png"))
(components (imago:label-components (imago:convert-to-binary image 1))))
(components-example:convert-to-image components))
| 原来的 | 处理 |
|---|---|
![]() | ![]() |
计算平方的欧几里得距离变换(也可以使用曼哈顿距离变换)。此示例需要array-operations 。
(defun edt-image (image)
(declare (type imago:binary-image image))
(let* ((dt (imago:distance-transform image :type :edt))
(max (reduce #'max (aops:flatten dt)))
(pixels (make-array (array-dimensions dt) :element-type 'imago:grayscale-pixel)))
(map-into (aops:flatten pixels)
(lambda (x) (imago:make-gray (floor (* 255 x) max)))
(aops:flatten dt))
(make-instance 'imago:grayscale-image :pixels pixels)))
(edt-image original-image)
| 原来的 | 处理 |
|---|---|
![]() | ![]() |
您可以通过安装imago/jupyter系统在Jupyter中查看Imago图像。然后调用imago-jupyter:show-image函数以显示图像。
