【问题标题】:Can someone help me with "pdb.gimp_drawable_set_pixel"?有人可以帮我解决“pdb.gimp_drawable_set_pixel”吗?
【发布时间】:2021-07-21 19:10:13
【问题描述】:

我学习 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, 0))   
    pdb.gimp_drawable_fill(layer_one, FILL_BACKGROUND)
  
    # --- Generating small stars
    pdb.gimp_context_set_foreground((255, 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, (lum, (random.random), + 64)
        pixel = 2, (lum, (random.random), + 64)
        pdb.gimp_drawable_set_pixel(layer_one, xs, ys, 3, [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",
    "Pocholo",
    "2021",
    "Create a Starry night",
    "RGB*",
    [    
    (PF_IMAGE, "img", "Input image", 0),
    (PF_DRAWABLE, "drawable", "Input layer", 0),
    (PF_ADJUSTMENT, "smStars", "Small stars", 500, (50, 10000, 10)),
    (PF_ADJUSTMENT, "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   

【问题讨论】:

    标签: python gimp


    【解决方案1】:

    pixel = 2, (lum, (random.random), + 64)
    

    您正在将像素设置为如下所示的元组结构:

    (number, ( number, function, number))
    

    function 是一个函数对象,而不是结果。然后你在它周围加上括号,这样你就传递了一个这样的东西的列表。需要的是 [0-255] 范围内的整数数组。例如将顶角设置为红色:

    pdb.gimp_drawable_set_pixel(layer,0,0,4,[255,0,0,255])
    

    请注意,这是4,[255,0,0,255],因为您添加了一个 Alpha 通道,因此需要提供 4 个值。

    总的来说,你的“python”代码仍然充满了只有在 Scheme 中才有意义的东西(参见 xsyslum 的定义),而你开始使用的脚本不是很好:

    • 由于图层是使用 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 &gt; Noise &gt; RGB noiseFilters &gt; Noise &gt; HSV noise 更改灰色层
    • 设置灰度为Dissolve模式
    • 根据口味设置灰色层不透明度

    当然,为了天文精度,您需要进一步限制颜色:不能有绿色或紫色,甚至蓝色也只是较亮恒星上的色调。

    【讨论】:

    • 感谢您的回复和解释。我知道使用“滤镜>噪波> RGB噪波或滤镜>噪波> HSV噪波”创建星星的方式,然后应用“高斯模糊”,并使用“色阶”控制星星的数量。我只是想尝试将Scheme代码翻译成python并稍微了解pdb.gimp_drawable_set_pixel的概念,但我发现代码中有很多更明显的错误。
    • 我在最初的帖子上发布了我想翻译成python的原始代码“星空.scm”的一部分。
    • 正确执行此操作的唯一可能方法是对原始文件的功能有一个高层次的理解,然后用 Python 从头开始​​重新编码。逐行翻译永远不会产生合理的 Python 代码,特别是因为原始的 Scheme 看起来也不像像样的 Scheme 代码(所以 WTF 循环控制来自哪里......)。
    • 谢谢xenoid!你的回答很好,解决了我想要理解的问题。
    • 当您查看它时,代码只是 1) 绘制随机级别,2) 绘制 3 个随机值并将它们添加到亮度以 gt RGB 值,然后 3) 绘制 2 个随机 X和Y值来设置对应的像素,这个最多做10K次。这是五行 Python。
    猜你喜欢
    • 2021-05-30
    • 2011-12-11
    • 2021-07-10
    • 2021-09-18
    • 1970-01-01
    • 2018-08-13
    • 2015-05-31
    • 2016-07-09
    • 2012-09-21
    相关资源
    最近更新 更多