Imago é uma biblioteca de manipulação de imagem para o Lisp comum. Ele suporta imagens em formatos PNG, PCX, Bitmap portátil (.pnm), TrueVision TGA (.tga) e JPEG. Você pode ler uma imagem com imago:read-image e escrever uma imagem com imago:write-format onde format é um de png , pcx , pnm , tga ou jpg .
Você pode usar a biblioteca Libjpeg-Turbo mais avançada para lidar com arquivos JPEG carregando o sistema imago/jpeg-turbo . Verifique se libjpeg-turbo está instalado no seu sistema. Use imago-jpeg-turbo:read-jpg-turbo e imago-jpeg-turbo:write-jpg-turbo Funções (ou apenas imago:read-image e imago:write-image ) para usar essa funcionalidade.
Você pode usar a biblioteca PNGLOAD mais avançada e rápida para ler arquivos PNG carregando o sistema imago/pngio . Use imago-pngio:read-png (ou apenas imago:read-image ) para usar essa funcionalidade. NB: pngload converte automaticamente imagens coloridas indexadas em imagens RGB (ou argb). Se você deseja trabalhar com imagens indexadas, use o carregador PNG antigo. Também será usada a biblioteca ZPNG (via imago-pngio:write-png ) para salvar imagens PNG.
Para criar uma imagem, observe a documentação para as classes de imagem (como imago:rgb-image ou imago:grayscale-image ). Você precisa fazer uma instância de uma dessas classes passando :w , :h e opcionalmente :initial-color para make-instance . Como alternativa, você pode usar funções make-XXX-image e 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)))
A maioria dos exemplos é tirada daqui.
(resize *image* 400 150)
| Original | Processado |
|---|---|
![]() | ![]() |
(rotate *image* 45)
| Original | Processado |
|---|---|
![]() | ![]() |
(emboss *image* :angle (* pi 0.75) :depth 1.0)
| Original | Processado |
|---|---|
![]() | ![]() |
(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))
| Original | Processado |
|---|---|
![]() | ![]() |
(do-region-pixels (*image* color x y 70 65 140 125)
(setf color (invert-color color)))
| Original | Processado |
|---|---|
![]() | ![]() |
(enhance-contrast *grayscale-image*)
| Original | Processado |
|---|---|
![]() | ![]() |
(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))))
| Original | Processado |
|---|---|
![]() | ![]() |
(let ((operator (default-compose-operator *image1*)))
(compose nil *image1* *image2* 20 20 operator))
| Original 1 | Original 2 | Processado |
|---|---|---|
![]() | ![]() | ![]() |
(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)
| Original | Processado |
|---|---|
![]() | ![]() |
(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)))
| Original | Processado |
|---|---|
![]() | ![]() |
Este exemplo requer sistemas de cobras e operações de matriz (disponíveis no 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))
| Original | Processado |
|---|---|
![]() | ![]() |
Cálculo da transformação da distância euclidiana quadrada (também está disponível a transformação de distância de Manhattan). Este exemplo requer 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)
| Original | Processado |
|---|---|
![]() | ![]() |
Você pode visualizar imagens IMAGO no Jupyter instalando o sistema imago/jupyter . Em seguida, ligue para a função imago-jupyter:show-image para mostrar uma imagem.
