Gimp-Forum.net
gimp-drawable-set-pixel giving unspecified error - Printable Version

+- Gimp-Forum.net (https://www.gimp-forum.net)
+-- Forum: GIMP (https://www.gimp-forum.net/Forum-GIMP)
+--- Forum: Extending the GIMP (https://www.gimp-forum.net/Forum-Extending-the-GIMP)
+---- Forum: Scripting questions (https://www.gimp-forum.net/Forum-Scripting-questions)
+---- Thread: gimp-drawable-set-pixel giving unspecified error (/Thread-gimp-drawable-set-pixel-giving-unspecified-error)



gimp-drawable-set-pixel giving unspecified error - rgbellotti - 11-24-2024

I generally use some version of GIMP 2.10 on many different distros of Linux, most often Debian-based such Ubuntu, Parrot, Neon, etc

For the most part, I've done my scripting in Scheme because it was the only scripting language I could get to register properly, the python scripts would never load no matter what troubleshooting steps I tried.  This is fine though as Scheme is fun and still powerful enough to do just about whatever I need with the proper ideas.

Anyhow, the point being that I came across a hurdle with (gimp-drawable-set-pixel), which is the only function I know where you can explicitly define pixels values to place into an image.  And with the following script :

Code:
(define (test-set-pixel)
 (let* (
         ;; Create a new image and drawable
         (frame (car (gimp-image-new 10 10 RGB)))
         (drawframe (car (gimp-layer-new frame 10 10 RGB "Test Layer" 100 NORMAL-MODE)))
       )
   ;; Add the new layer to the image
   (gimp-image-insert-layer frame drawframe 0 -1)
   (gimp-drawable-fill drawframe TRANSPARENT-FILL)

   ;; Test setting a single pixel
   (let* (
          (x 0)
          (y 0)
          (new-pixel (vector 255 0 0 255)) ;; Red with full opacity
        )
     (gimp-message (string-append "Setting pixel at (" (number->string x) ", " (number->string y) ")"))
     (gimp-drawable-set-pixel drawframe x y 4 new-pixel))

   ;; Save the image for inspection
   (file-png-save 0 frame drawframe "test.png" "test.png" 1 9 1 1 1 1 1)
   (gimp-message "Test image saved.")
   (gimp-image-delete frame)
 )
)


I get the cryptic error message --  "Error: Procedure execution of gimp-drawable-set-pixel failed".  As a side note, I don't use python because it's been difficult for me to get Gimp to register python scripts in Linux, so seeing as how I have quite a few Scheme scripts started I'd like to try and resolve this fundamental problem of being able to place a pixel into an image, are there other functions that could give a similar functionality?  I've considered sampling pixels with (gimp-drawable-get-pixel) and then creating a new layer with 1x1 pixels, and then applying a paint to the new layer to match the color of sampled picture, and then mixed down a layer group of 320 x 240 layers (for a 320x240 pixel image for example) and then calling that a 'set pixel" alternative, but that would be an extreme work around.  Is there anything obvious that I'm overlooking?


RE: gimp-drawable-set-pixel giving unspecified error - rgbellotti - 11-24-2024

I found this work-around to work alright so far, and figured I'd post here in case anyone else ever came across that scenario.  This test function will essentially place one pixel on a new image of the color you specify:
Code:
(define (test-set-pixel)
 (let* (
         ;; Create a new image and drawable
         (frame (car (gimp-image-new 10 10 RGB)))
         (drawframe (car (gimp-layer-new frame 10 10 1 "Test Layer" 100 28)))
       )
   ;; Add the new layer to the image
   (gimp-image-insert-layer frame drawframe 0 -1)

   ;; Set the foreground color to red (the brush color that you will draw)
   (gimp-context-set-foreground '(255 0 0))

   ;; Set the brush size and settings to pixel-perfect accuracy
   (gimp-context-set-brush-size 1)
   (gimp-context-set-brush-angle 0)
   (gimp-context-set-brush-aspect-ratio 0)
   (gimp-context-set-brush-hardness 1)
   (gimp-context-set-brush-force 1)

   ;; Use the pencil tool to draw a single pixel
   ;; NOTE ;; "PRESS" and "RELEASE" are both considered a "stroke" of the pencil
   ;; so use '2' for the stroke count, for PRESS = (5,5) RELEASE = (5,5)

   (gimp-pencil drawframe 2 #(5 5 5 5))

   ;; Save the image
   (file-png-save 1 frame drawframe "test_pixel.png" "test_pixel.png" 1 9 1 1 1 1 1)
   (gimp-message "Test image saved.")
   (gimp-image-delete frame)
 )
)



RE: gimp-drawable-set-pixel giving unspecified error - teapot - 11-24-2024

Hi rgbellotti,

You've found an alternative method but just to follow up your first method in your first post:

The number of channels you specified didn't match the layer you created.

You specified 4 channels:
(gimp-drawable-set-pixel drawframe x y 4 new-pixel)
which is rgba.

For 2.10 gimp-layer-new the layer type is
{ RGB-IMAGE (0), RGBA-IMAGE (1), GRAY-IMAGE (2), GRAYA-IMAGE (3), INDEXED-IMAGE (4), INDEXEDA-IMAGE (5) }

Whereas you put RGB which is 0, so no alpha:
(drawframe (car (gimp-layer-new frame 10 10 RGB "Test Layer" 100 NORMAL-MODE)))

RGB is typically used for image type:
{ RGB (0), GRAY (1), INDEXED (2) }

In your second post it was fixed when you hardcoded 1 for type:
(drawframe (car (gimp-layer-new frame 10 10 1 "Test Layer" 100 28)))

Perhaps intead of hardcoding the number of channels you could have used:
(gimp-drawable-bpp drawable)
to get the bytes per pixel.


RE: gimp-drawable-set-pixel giving unspecified error - rgbellotti - 11-24-2024

(3 hours ago)teapot Wrote: Hi rgbellotti,

You've found an alternative method but just to follow up your first method in your first post:

The number of channels you specified didn't match the layer you created.

You specified 4 channels:
(gimp-drawable-set-pixel drawframe x y 4 new-pixel)
which is rgba.

For 2.10 gimp-layer-new the layer type is
{ RGB-IMAGE (0), RGBA-IMAGE (1), GRAY-IMAGE (2), GRAYA-IMAGE (3), INDEXED-IMAGE (4), INDEXEDA-IMAGE (5) }

Whereas you put RGB which is 0, so no alpha:
(drawframe (car (gimp-layer-new frame 10 10 RGB "Test Layer" 100 NORMAL-MODE)))

RGB is typically used for image type:
{ RGB (0), GRAY (1), INDEXED (2) }

In your second post it was fixed when you hardcoded 1 for type:
(drawframe (car (gimp-layer-new frame 10 10 1 "Test Layer" 100 28)))

Perhaps intead of hardcoding the number of channels you could have used:
(gimp-drawable-bpp drawable)
to get the bytes per pixel.

Thank you!  This was the problem.  I haven't tried (gimp-drawable-bpp drawable) yet but that does sound useful, I'll look into it Smile