如何解决有人可以帮我解决“pdb.gimp_drawable_set_pixel”吗?
我学习 GIMP python 大约一年左右,我一直在研究如何从 GIMP 方案转换为 GIMP python。我有一个来自创作者“T. Demand & GnuTux”的旧 GIMP scm,它创建了一个星空,我正试图将其转换为 python。到目前为止,代码给了我一个错误“pdb.gimp_drawable_set_pixel(layer_one,xs,ys,3,[pixel]) TypeError: 错误的参数类型"
谁能解决这个问题,让我知道我做错了什么?提前致谢!
这是完整的代码
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from gimpfu import *
import os,sys,time
import random
import math
import gettext
gettext.install("gimp20",gimp.locale_directory,unicode=True)
def pm_create_starry_night(img,drawable,smStars,bglum):
#--- Initiates the temporary state
pdb.gimp_context_set_defaults()
pdb.gimp_context_set_default_colors()
# --- Star group
img.undo_group_start()
ii = 0
ns = 0
xs = 0
ys = 0
lum = 0
pixel = "Bytesarray"
width = pdb.gimp_drawable_width(drawable) # Get Width
height = pdb.gimp_drawable_height(drawable) # Get Height
random.random = "realtime"
pixel = [0,255]
pixel = [1,255]
pixel = [2,255]
# --- Create the sky
layer_one = pdb.gimp_layer_new(img,img.width,img.height,RGBA_IMAGE,"smStars",100,LAYER_MODE_norMAL)
pdb.gimp_image_insert_layer(img,layer_one,None,-1)
pdb.gimp_layer_add_alpha(layer_one)
pdb.gimp_context_set_foreground((255,255,255))
pdb.gimp_context_set_background((0,0))
pdb.gimp_drawable_fill(layer_one,FILL_BACKGROUND)
# --- Generating small stars
pdb.gimp_context_set_foreground((255,255))
while ns < smStars:
ns = ns + 1
xs = (random.random,(width -1))
ys = (random.random,(height -1))
lum = (random.random,(200 + 128))
pixel = 0,(lum,(random.random),+ 64)
pixel = 1,+ 64)
pixel = 2,+ 64)
pdb.gimp_drawable_set_pixel(layer_one,[pixel])
ii = ii + 1
if ii > 10000:
ns = smStars
pdb.gimp_displays_flush()
# --- Set gimp to default
pdb.gimp_context_set_defaults()
pdb.gimp_context_set_default_colors()
# --- End group
img.undo_group_end()
register(
"pm_create_starry_night","Creates stars at night","Creates stars at night effect","Pocholo","2021","Create a Starry night","RGB*",[
(PF_IMAGE,"img","Input image",0),(PF_DRAWABLE,"drawable","Input layer",(PF_ADJUSTMENT,"Small stars",500,(50,10000,10)),"bglum","Background luminosity",5,(0,64,1)),],[],pm_create_starry_night,menu="<Image>/Pocholo-scripts/Create a Starry night",domain=("gimp20",gimp.locale_directory))
main()
Starry sky.scm
```(define (script-fu-starry-night-sample image drawable smalls bglum)
(let* (
(layer-one -1)
(ii 0)
(ns 0)
(xs 0)
(ys 0)
(lum 0)
(pixw (cons-array 3 'byte))
(height (car (gimp-drawable-height drawable))) ; Get Height
(width (car (gimp-drawable-width drawable))) ; Get Width
)
(srand (realtime))
(aset pixw 0 255)
(aset pixw 1 255)
(aset pixw 2 255)
; Set the fg to white,bg to black
(gimp-palette-set-foreground '(255 255 255))
(gimp-context-set-background '(0 0 0))
(set! layer-one (car (gimp-layer-new image width height RGB-IMAGE "Bottom" 100 LAYER-MODE-norMAL-LEGACY)))
(gimp-image-add-layer image layer-one -1)
(gimp-image-lower-layer-to-bottom image layer-one)
(gimp-drawable-fill layer-one 1) ;0 FG,1 BG,2 white
; generating small stars
(while (< ns smalls)
(set! ns (+ ns 1))
(set! xs (rand (- width 1)))
(set! ys (rand (- height 1)))
(set! lum (+ (rand 200) 128))
(aset pixw 0 (+ lum (rand 64)))
(aset pixw 1 (+ lum (rand 64)))
(aset pixw 2 (+ lum (rand 64)))
(gimp-drawable-set-pixel layer-one xs ys 3 pixw)
; preparing the exit of the loop
(set! ii (+ ii 1))
(if (> ii 10000) (set! ns smalls))
) ; end of loop
解决方法
与
pixel = 2,(lum,(random.random),+ 64)
您正在将像素设置为如下所示的元组结构:
(number,( number,function,number))
其中 function
是函数对象,而不是结果。然后你在它周围加上括号,所以你传递了一个这样的列表。需要的是 [0-255] 范围内的整数数组。例如将顶角设置为红色:
pdb.gimp_drawable_set_pixel(layer,4,[255,255])
请注意,这是 4,255]
,因为由于您添加了 Alpha 通道,因此需要提供 4 个值。
总的来说,您的“python”代码仍然充满了只有在 Scheme 中才有意义的东西(参见 xs
、ys
和 lum
的定义),以及脚本你开始的不是很好:
- 由于图层是使用 RGBA_IMAGE 类型创建的,所以它从一开始就有一个 Alpha 通道,因此无需添加。
- 离开时将所有内容重新设置为默认值是一种大罪,在脚本入口使用
gimp.context_push()
并在退出时使用 gimp.context_pop()。 - 由于
pdb.gimp_context_set_defaults()
也会重置颜色,因此不需要以下pdb.gimp_context_set_default_colors()
。 - 使用
ii
变量来控制循环的最大迭代次数是我一段时间以来见过的最多的 WTF 代码。在 Python 中,您会执行for _ in range(min(smStars,10000)) (the
is a varaible fir the loop index,using
` 是一种约定,表明您不会在任何地方使用它)。
此外,set_pixel()
操作很慢,在 python-fu 中你可以使用“像素区域”。这些是直接映射到像素数据的 Python 数组,非常高效。
最后,整个脚本可以通过几个操作完成:
- 创建一个填充黑色的图层
- 在其上创建一个填充中间灰色 (127,127,127) 的图层
-
Filters > Noise > RGB noise
或Filters > Noise > HSV noise
改变灰色层 - 将灰度图层设置为
Dissolve
模式 - 根据口味设置灰色图层的不透明度
当然,为了天文精确度,您需要进一步限制颜色:您不能有绿色或紫色,甚至蓝色也只是更亮恒星的色调。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。