Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to make a mask from a selection?
#9
[quote='ersc57' pid='46597' dateline='1770654820']

Please respect the forum and post in English and learn not to quote over-and-over.

Not python as OP wants but might be useful although, does not look like for Gimp 3

Your main difficulty (Step 2) is solved quite easily by creating a temporary dummy image.
How to create a mask from a selection (Step 2)
To create a mask that does not change the overall image, you need to temporarily create a new image of the same size in GIMP's memory, fill it with black, turn the selection white, and save it.
Here is the logic in Script-Fu (SCM) that does just that:

Code:
(let* (
     (width  (car (gimp-image-width image)))
     (height (car (gimp-image-height image)))
     ; 1. Создаем временное изображение в памяти (не отображаем его)
     (mask-img (car (gimp-image-new width height GRAY)))
     (mask-layer (car (gimp-layer-new mask-img width height GRAY-IMAGE "Mask" 100 NORMAL-MODE)))
     )
 
 (gimp-image-insert-layer mask-img mask-layer 0 -1)

 ; 2. Заливаем всё черным (фон маски)
 (gimp-context-set-background '(0 0 0))
 (gimp-drawable-fill mask-layer BACKGROUND-FILL)

 ; 3. Переносим выделение из основного изображения в маску
 ; Копируем выделение через канал
 (let ((selection (car (gimp-selection-save image))))
   (gimp-image-select-item mask-img CHANNEL-OP-REPLACE selection)
   
   ; 4. Заливаем выделенное белым
   (gimp-context-set-foreground '(255 255 255))
   (gimp-drawable-edit-fill mask-layer FOREGROUND-FILL)
   
   ; Удаляем временный канал
   (gimp-image-remove-channel image selection)
 )

 ; 5. Экспортируем маску
 (file-png-save-defaults RUN-NONINTERACTIVE mask-img mask-layer "C:/temp/mask.png" "mask.png")

 ; 6. Удаляем временное изображение из памяти, чтобы не занимать RAM
 (gimp-image-delete mask-img)
)


P.S. «...тому не нужно далеко ходить, у кого черт за плечами», — ответил Пацюк
Reply


Messages In This Thread
How to make a mask from a selection? - by ersc57 - 02-07-2026, 08:54 AM
RE: How to make a mask from a selection? - by И.И. Геминиев - 02-10-2026, 12:25 AM

Forum Jump: