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 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函數以顯示圖像。
