From 96c31983f56a28171a11b42da6d28c1afbe79572 Mon Sep 17 00:00:00 2001 From: kamilburda <8431034+kamilburda@users.noreply.github.com> Date: Sun, 6 Aug 2023 16:56:56 +0200 Subject: [PATCH] Initial release --- .gitignore | 231 +++++++++++++++++++++++++++++++++++++ CHANGELOG.md | 4 + LICENSE | 29 +++++ README.md | 42 +++++++ align_layer/align_layer.py | 101 ++++++++++++++++ 5 files changed, 407 insertions(+) create mode 100644 .gitignore create mode 100644 CHANGELOG.md create mode 100644 LICENSE create mode 100644 README.md create mode 100644 align_layer/align_layer.py diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..c456b9c --- /dev/null +++ b/.gitignore @@ -0,0 +1,231 @@ +################# +## Custom +################# + +resources/ +ide/ + +################# +## Eclipse +################# + +*.pydevproject +.project +.metadata +bin/ +tmp/ +*.tmp +*.bak +*.swp +*~.nib +local.properties +.classpath +.settings/ +.loadpath + +# External tool builders +.externalToolBuilders/ + +# Locally stored "Eclipse launch configurations" +*.launch + +# CDT-specific +.cproject + +# PDT-specific +.buildpath + + +################# +## Visual Studio +################# + +## Ignore Visual Studio temporary files, build results, and +## files generated by popular Visual Studio add-ons. + +# User-specific files +*.suo +*.user +*.sln.docstates + +# Build results + +[Dd]ebug/ +[Rr]elease/ +x64/ +build/ +[Bb]in/ +[Oo]bj/ + +# MSTest test Results +[Tt]est[Rr]esult*/ +[Bb]uild[Ll]og.* + +*_i.c +*_p.c +*.ilk +*.meta +*.obj +*.pch +*.pdb +*.pgc +*.pgd +*.rsp +*.sbr +*.tlb +*.tli +*.tlh +*.tmp +*.tmp_proj +*.log +*.vspscc +*.vssscc +.builds +*.pidb +*.log +*.scc + +# Visual C++ cache files +ipch/ +*.aps +*.ncb +*.opensdf +*.sdf +*.cachefile + +# Visual Studio profiler +*.psess +*.vsp +*.vspx + +# Guidance Automation Toolkit +*.gpState + +# ReSharper is a .NET coding add-in +_ReSharper*/ +*.[Rr]e[Ss]harper + +# TeamCity is a build add-in +_TeamCity* + +# DotCover is a Code Coverage Tool +*.dotCover + +# NCrunch +*.ncrunch* +.*crunch*.local.xml + +# Installshield output folder +[Ee]xpress/ + +# DocProject is a documentation generator add-in +DocProject/buildhelp/ +DocProject/Help/*.HxT +DocProject/Help/*.HxC +DocProject/Help/*.hhc +DocProject/Help/*.hhk +DocProject/Help/*.hhp +DocProject/Help/Html2 +DocProject/Help/html + +# Click-Once directory +publish/ + +# Publish Web Output +*.Publish.xml +*.pubxml + +# NuGet Packages Directory +## TODO: If you have NuGet Package Restore enabled, uncomment the next line +#packages/ + +# Windows Azure Build Output +csx +*.build.csdef + +# Windows Store app package directory +AppPackages/ + +# Others +sql/ +*.Cache +ClientBin/ +[Ss]tyle[Cc]op.* +~$* +*~ +*.dbmdl +*.[Pp]ublish.xml +*.pfx +*.publishsettings + +# RIA/Silverlight projects +Generated_Code/ + +# Backup & report files from converting an old project file to a newer +# Visual Studio version. Backup files are not needed, because we have git ;-) +_UpgradeReport_Files/ +Backup*/ +UpgradeLog*.XML +UpgradeLog*.htm + +# SQL Server files +App_Data/*.mdf +App_Data/*.ldf + +############# +## Windows detritus +############# + +# Windows image file caches +Thumbs.db +ehthumbs.db + +# Folder config file +Desktop.ini + +# Recycle Bin used on file shares +$RECYCLE.BIN/ + +# Mac crap +.DS_Store + + +############# +## Python +############# + +*.py[co] + +# Packages +*.egg +*.egg-info +dist/ +build/ +eggs/ +parts/ +var/ +sdist/ +develop-eggs/ +.installed.cfg + +# Installer logs +pip-log.txt + +# Unit test / coverage reports +.coverage +.tox + +#Translations +*.mo + +#Mr Developer +.mr.developer.cfg + + +############# +## Jekyll +############# + +_site +.sass-cache +.jekyll-metadata \ No newline at end of file diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000..0b819fb --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,4 @@ +1.0 +=== + +Initial release. diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..68ec7d1 --- /dev/null +++ b/LICENSE @@ -0,0 +1,29 @@ +BSD 3-Clause License + +Copyright (c) 2023, Kamil Burda +All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are met: + +1. Redistributions of source code must retain the above copyright notice, this + list of conditions and the following disclaimer. + +2. Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimer in the documentation + and/or other materials provided with the distribution. + +3. Neither the name of the copyright holder nor the names of its + contributors may be used to endorse or promote products derived from + this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE +FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR +SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER +CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, +OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..62baf95 --- /dev/null +++ b/README.md @@ -0,0 +1,42 @@ +Align Layer +=========== + +This is a simple [GIMP](https://www.gimp.org/) script that aligns a layer against another layer. + +Alignment is performed both horizontally (none, left, center, right) and vertically (none, top, center, bottom). + +X- and Y-offsets can be specified as pixels, as percentages of image width/height or as percentages of the second layer width/height. + +This script introduces a procedure named `python-fu-align-layer`. +While aligning layers is already possible in GIMP, this script is intended to be used for automation purposes. Hence, there is no GUI associated with this procedure. + + +Usage +----- + +Example of centering the first layer against the second layer, both horizontally and vertically: + +``` +image = gimp.image_list()[0] +layer_to_align = image.layers[0] +reference_layer = image.layers[1] + +pdb.python_fu_align_layer(image, layer_to_align, reference_layer, 2, 2, 0, 0, 0, 0) +``` + +The parameters are explained in the GIMP procedural database browser. The procedure name is `python-fu-align-layer`. + + +Installation +------------ + +GIMP 2.10 is required. + +1. In GIMP, locate the folder containing GIMP plug-ins - open GIMP and go to Edit → Preferences → Folders → Plug-Ins. +2. Copy the `align_layer` folder inside one of the folders identified in step 1. + +For Windows, make sure you have GIMP installed with support for Python scripting. + +For Linux, make sure you use a GIMP installation bundled as Flatpak (which can be downloaded from the [official GIMP page](https://www.gimp.org/downloads/)) or AppImage. + +For macOS, make sure you have Python 2.7 installed. diff --git a/align_layer/align_layer.py b/align_layer/align_layer.py new file mode 100644 index 0000000..06ba8d1 --- /dev/null +++ b/align_layer/align_layer.py @@ -0,0 +1,101 @@ +#! /usr/bin/env python +# -*- coding: utf-8 -*- + +from __future__ import absolute_import, division, print_function, unicode_literals + +from gimp import pdb +import gimpfu + + +def align_layer( + image, + layer_to_align, + reference_layer, + horizontal_alignment, + vertical_alignment, + x_offset, + x_offset_unit, + y_offset, + y_offset_unit, +): + new_x, new_y = layer_to_align.offsets + + if horizontal_alignment == 1: + new_x = reference_layer.offsets[0] + elif horizontal_alignment == 2: + new_x = reference_layer.offsets[0] + (reference_layer.width - layer_to_align.width) // 2 + elif horizontal_alignment == 3: + new_x = reference_layer.offsets[0] + reference_layer.width - layer_to_align.width + + if vertical_alignment == 1: + new_y = reference_layer.offsets[1] + elif vertical_alignment == 2: + new_y = reference_layer.offsets[1] + (reference_layer.height - layer_to_align.height) // 2 + elif vertical_alignment == 3: + new_y = reference_layer.offsets[1] + reference_layer.height - layer_to_align.height + + if x_offset: + if x_offset_unit == 0: + new_x += x_offset + elif x_offset_unit == 1: + new_x += (image.width * x_offset) / 100 + elif x_offset_unit == 2: + new_x += (reference_layer.width * x_offset) / 100 + + if y_offset: + if y_offset_unit == 0: + new_y += y_offset + elif y_offset_unit == 1: + new_y += (image.height * y_offset) / 100 + elif y_offset_unit == 2: + new_y += (reference_layer.height * y_offset) / 100 + + pdb.gimp_layer_set_offsets(layer_to_align, new_x, new_y) + + +gimpfu.register( + proc_name='align_layer', + blurb='Aligns a layer against another layer.', + help=( + 'While aligning layers is already possible in GIMP,' + ' this script can be used for automation purposes.'), + author='Kamil Burda', + copyright='Kamil Burda', + date='2023', + label='', + imagetypes='*', + params=[ + (gimpfu.PF_IMAGE, 'image', 'Image', None), + (gimpfu.PF_LAYER, 'layer_to_align', 'Layer to align', None), + (gimpfu.PF_LAYER, 'reference_layer', 'Reference layer', None), + (gimpfu.PF_SLIDER, + 'horizontal_alignment', + 'Horizontal alignment (0 = none, 1 = left, 2 = center, 3 = right)', + 0, + (0, 3, 1)), + (gimpfu.PF_SLIDER, + 'vertical_alignment', + 'vertical_alignment (0 = none, 1 = top, 2 = center, 3 = bottom)', + 0, + (0, 3, 1)), + (gimpfu.PF_FLOAT, 'x_offset', 'X-offset', 0.0), + (gimpfu.PF_SLIDER, + 'x_offset_unit', + ('Unit of X-offset (0 = pixel, 1 = percentage of image width,' + ' 2 = percentage of reference layer width)'), + 0, + (0, 1, 1)), + (gimpfu.PF_FLOAT, 'y_offset', 'Y-offset', 0.0), + (gimpfu.PF_SLIDER, + 'y_offset_unit', + ('Unit of Y-offset (0 = pixel, 1 = percentage of image height,' + ' 2 = percentage of reference layer height)'), + 0, + (0, 1, 1)), + ], + results=[], + function=align_layer) + + +if __name__ == '__main__': + gimpfu.main()