Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Auto fit brush to image?
#9
Many thanks for your help. I see where I'd gone the wrong way with some of my values. I decided that I still want it to create a new layer automatically, so I modified your code to have it draw on a new layer which is based on the current selection and offset (changes occur after the code to set the brush size):

Code:
#!/usr/bin/env python
# -*- coding: utf-8 -*-

# GIMP plugin to ifll the current layer with a stroke of a brush

# (c) Ofnuts 2023
#
#   History:
#
#   v0.0: 2024-01-14: Initial version
#
#   This program is free software; you can redistribute it and/or modify
#   it under the terms of the GNU General Public License as published by
#   the Free Software Foundation; either version 2 of the License, or
#   (at your option) any later version.
#
#   This program is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU General Public License for more details.
#
#   You should have received a copy of the GNU General Public License
#   along with this program; if not, write to the Free Software
#   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.


import sys,os,os.path,traceback,random
from gimpfu import *
import gimpcolor

debug='OFN_DEBUG' in os.environ
 
def trace(format,*args):
   if debug:
       print format % args


def strokeBrushOnLayer(image,drawable):
   #pdb.gimp_plugin_enable_precision()
   image.undo_group_start()
   gimp.context_push()
   try:        
       # Get brush data
       _, _, _, _, bw, bh, _, _ = pdb.gimp_brushes_get_brush_data("") # Data for current brush
       lw,lh=drawable.width,drawable.height
       ox,oy=drawable.offsets
       scaleX=float(lw)/bw # required for horizontal fill
       scaleY=float(lh)/bh # required for vertical fill
       scale=max(scaleX,scaleY)
       size=int(round(max(bw,bh)*scale)) # Brush size is size of biggest dimension
       trace("Brush is %dx%d, sized to %d (%5.2f)",bw,bh,size,scale)
       pdb.gimp_context_set_brush_size(size)
           
       newLayer = pdb.gimp_layer_new(image, lw, lh, RGBA_IMAGE, "Scaled Brush", 100.0, LAYER_MODE_NORMAL)
       pdb.gimp_layer_set_offsets(newLayer, ox, oy)
       pdb.gimp_image_insert_layer(image, newLayer, None, 0)
       
       pdb.gimp_paintbrush_default(newLayer, 2, [lw/2,lh/2])
       
   except Exception as e:
       print e.args[0]
       gimp.message(e.args[0])
       print traceback.format_exc()
       
   gimp.context_pop()
   image.undo_group_end()
   gimp.displays_flush()    

### Registration
author='Ofnuts'
copyrightYear='2024'
desc='Fill layer with scaled current brush'
register(
   'ofn-brush-to-layer',
   desc,desc,author,author,copyrightYear,desc+'...',
   'RGB*',
   [
       (PF_IMAGE, 'image', 'Input image', None),
       (PF_DRAWABLE, 'layer', 'Input layer', None),
   ],
   [],
   strokeBrushOnLayer,
   menu='<Image>/Test' if debug else '<Image>/Filters/Render/'
)

main()
Reply


Messages In This Thread
Auto fit brush to image? - by R Soul - 01-14-2024, 01:21 PM
RE: Auto fit brush to image? - by Ofnuts - 01-14-2024, 02:10 PM
RE: Auto fit brush to image? - by R Soul - 01-14-2024, 05:22 PM
RE: Auto fit brush to image? - by Ofnuts - 01-14-2024, 05:51 PM
RE: Auto fit brush to image? - by R Soul - 01-14-2024, 07:24 PM
RE: Auto fit brush to image? - by Ofnuts - 01-14-2024, 09:41 PM
RE: Auto fit brush to image? - by R Soul - 01-14-2024, 10:02 PM
RE: Auto fit brush to image? - by Ofnuts - 01-14-2024, 10:22 PM
RE: Auto fit brush to image? - by R Soul - 01-15-2024, 09:16 PM

Forum Jump: