You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用Common Lisp创建简单图形?Imago库使用问题求助

解决Imago库报错并实现数字列表生成图形的方案

一、修复"MAKE-GRAYSCALE-IMAGE"未找到的问题

这个错误源于Imago版本迭代后的函数名变更——旧文档中的make-grayscale-image已被新版本的imago:make-image替代,而RGB图像创建函数imago:make-rgb-image的使用逻辑未变,但需确保正确加载库。

首先通过Quicklisp加载最新版Imago:

(ql:quickload :imago)

二、可运行的示例代码

1. 创建纯色RGB图像

;; 创建400x100的红色RGB图像并保存为PNG
(imago:write-image 
  (imago:make-rgb-image 400 100 (imago:make-color 255 0 0))
  "red_background.png" :png)

2. 用数字列表绘制彩色点

假设你有格式为((x1 y1 r1 g1 b1) (x2 y2 r2 g2 b2) ...)的坐标+颜色数字列表,可遍历列表绘制点:

(let* ((width 500)
       (height 500)
       ;; 白色背景的RGB图像
       (image (imago:make-rgb-image width height (imago:make-color 255 255 255)))
       ;; 自定义的点数据列表
       (point-list '((100 100 255 0 0) (200 200 0 255 0) (300 150 0 0 255))))
  ;; 批量绘制点
  (loop for (x y r g b) in point-list do
    (setf (imago:image-pixel image x y) (imago:make-color r g b)))
  ;; 保存结果
  (imago:write-image image "colored_points.png" :png))

3. 用数字列表绘制彩色线段

使用格式为((x1 y1 x2 y2 r g b) ...)的线段数据列表,结合Imago原生画线函数实现:

(let* ((width 500)
       (height 500)
       (image (imago:make-rgb-image width height (imago:make-color 255 255 255)))
       ;; 自定义的线段数据列表
       (line-list '((50 50 450 50 255 0 0) 
                    (50 100 450 100 0 255 0) 
                    (50 150 450 150 0 0 255))))
  ;; 批量绘制线段
  (loop for (x1 y1 x2 y2 r g b) in line-list do
    (imago:draw-line image x1 y1 x2 y2 (imago:make-color r g b)))
  (imago:write-image image "colored_lines.png" :png))

三、最简高效实现思路

  • 优先复用Imago原生函数(make-rgb-image、image-pixel、draw-line),避免手动实现基础图形算法。
  • 优化数字列表格式:可提前将颜色预转为imago:color对象,减少循环内的对象创建开销,例如:
    (let* ((red (imago:make-color 255 0 0))
           (point-list '((100 100 red) (200 200 (imago:make-color 0 255 0)))))
      ;; 绘制逻辑...
    
  • 批量处理大量数据时,用loop替代dolist,性能更优。

内容的提问来源于stack exchange,提问作者adrian

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.04 01:50:17