Text box layer centering in python-fu

Post a reply

In an effort to prevent automatic submissions, we require that you complete the following challenge.
:gimp: :golden_wilber: :gimplearn: :NewGL: :GL2: :favicon4: :award_trophy: :polygon_award_trophy: :Ella_trophy_award: :gimp_learn_olympic_medal: :thumbsup: :2_Thumbs: :download: :learned_something: :gimp_clap: :hammer_head: :happy_purple: :yay_yay_purple: :giggle_purple: :evil_laugh_purple: :lmao_purple: :rofl: :aw_shucks_purple: :off_to_bed_purple: :blowing_hearts: :cool: :ninja: :scared_smilie: :red_heart: :white_heart: :thumbs_up_love_recieved: :stars_1: :stars_2: :stars_3: :stars_4: :stars_5: :public_domain: :fb_like: :fb_love: :fb_haha: :fb_wow: :fb_sad: :fb_angry: :bull_riding: :paint :computer_smiley: :confetti: :terrific: :congrats: :crysmiley: :hi5: :goodjob: :popcorn: :hearts_smiley: :pompom: :drinking_smilies: :wine_smiley: :smiley_toss: :gaah: :i_care: :kungfu: :photosmiley: :snap: :trampoline: :tantrum: :yes: :thank_you: :tick_tock: :thats_awesome: :what she said: :what he said: :2_10_rectangle_select_tool: :2_10_ellipse_select_tool: :2_10_free_select_tool: :2_10_fuzzy_select_tool: :2_10_select_by_color_tool: :2_10_scissors_select_tool: :2_10_foreground_select_tool: :2_10_paths_tool: :2_10_color_picker_tool: :2_10_zoom_tool: :2_10_measure_tool: :2_10_move_tool: :2_10_alignment_tool: :2_10_crop_tool: :2_10_unified_transform_tool: :2_10_rotate_tool: :2_10_scale_tool :2_10_shear_tool: :2_10_handle_transform_tool: :2_10_perspective_tool: :2_10_flip_tool: :2_10_cage_transform_tool: :2_10_warp_transform_tool: :2_10_text_tool: :2_10_bucket_fill_tool: :2_10_gradient_tool: :2_10_pencil_tool: :2_10_paintbrush_tool: :2_10_eraser_tool: :2_10_airbrush_tool: :2_10_ink_tool: :2_10_mypaint_brush_tool: :2_10_clone_tool: :2_10_healing_tool: :2_10_perspective_clone_tool: :2_10_blur_sharpen_tool: :2_10_smudge_tool: :2_10_dodge_burn_tool: :2_10_foreground_background: :rectangle_select: :ellipse_select: :free_select: :fuzzy_select: :select_by_color: :scissors_select: :foreground_select: :paths: :color_picker: :zoom: :measure: :move: :alignment: :crop: :rotate: :scale: :shear: :perspective: :flip: :cage_transform: :text: :bucket_fill: :blend: :pencil: :paintbrush: :eraser: :airbrush: :ink: :clone: :healing: :perspective_clone: :blur_sharpen: :smudge: :dodge_burn: :foreground_background: :D :) ;) :( :o :shock: :? 8-) :lol: :x :P :oops: :cry: :evil: :twisted: :roll: :!: :?: :idea: :arrow: :| :mrgreen: :geek: :ugeek: :tree_01: :tree_02: :tree_03: :tree_04: :tree_05: :tree_06: :tree_07: :tree_08: :tree_09: :tree_10: :tree_11: :tree_12:

BBCode is ON
[img] is ON
[flash] is OFF
[url] is ON
Smilies are ON

Topic review

Expand view Topic review: Text box layer centering in python-fu

Re: Text box layer centering in python-fu

by tim » 25 Jun 2019, 13:54

I don't think so as I type "align" into procedure browser, only one method showed up and I don't think it allows you to use it like the front end alignment tool.

Re: Text box layer centering in python-fu

by Mapsking » 25 Jun 2019, 08:12

Out of curiosity, couldn't you program it to use the built-in alignment tool?

Re: Text box layer centering in python-fu

by vndep » 24 Jun 2019, 19:40

That totally works! Thanks!

Re: Text box layer centering in python-fu

by tim » 24 Jun 2019, 18:58

if you use fontname function to create that layer of text the layer will be sized based on your text so it's already a good measure of your text.width and height.
That text layer will have layer.width and layer.height.
Which you could use to move around using offsets (just search for "offsets" in procedure browser in GIMP).
two functions should show up. One is to get the offsets.
The other is to set the offsets of the layer.
Use the one to set the offsets which is of the top left corner of layer.
That should be sufficient. If you want even more accuracy you can use gimp_text_get_extents_fontname but I usually get away with using created text layers width and height.

Text box layer centering in python-fu

by vndep » 24 Jun 2019, 16:29

Any thoughts on an efficient way to center a textbox in a python script?

For background, the script starts with an image and adds a layer of text. I want to make sure that the center of the textbox aligns with a specific X,Y coordinate in the image. To clarify, I am not thinking about left/center/right justification within the textbox.

I do not know in advance what text will be in the textbox. It could be of any length and include narrow characters (such as "i") or wide characters (such as "W")

My original thought was to somehow grab the X,Y coordinates of the four corners of the textbox (or maybe height and length) and then do some algebra to figure out to where I should move the textbox.

The only function that seems relevant is pdb.gimp_selection_bounds; however the parameter for that function is an image rather than a layer.

Anticipating a follow up question, I am not having any luck finding the python function that would allow me to move the textbox, either.

Thanks in advance!