Skip to content

Instantly share code, notes, and snippets.

@LisekLucek
Last active September 10, 2022 20:24
Show Gist options
  • Save LisekLucek/4b10167826e6fb57c284994bec7154e3 to your computer and use it in GitHub Desktop.
Save LisekLucek/4b10167826e6fb57c284994bec7154e3 to your computer and use it in GitHub Desktop.
GIMP script for automated Telegram sticker generation
;;
;; GIMP script for automated Telegram sticker generation
;;
;; 1. Open an image with transparent background
;; 2. Run the script from Filters/Make Sticker
;; 3. Export generated sticker as PNG
;; 4. Done! You can upload the sticker to Telegram via https://t.me/Stickers
;;
;; Installation guide:
;; https://docs.gimp.org/en/install-script-fu.html
;;
(define (script-fu-text-box inImage inStickerSize inBorderSize inBorderColour)
(let*
(
(imgWidth 0)
(imgHeight 0)
(bgLayer (car (gimp-layer-new inImage 512 512 RGBA-IMAGE "Sticker Background" 100 LAYER-MODE-NORMAL-LEGACY)))
(initBgColour (car (gimp-context-get-background)))
)
; Preparations
(gimp-context-push)
(gimp-image-undo-group-start inImage)
; Actual work here
(plug-in-autocrop RUN-NONINTERACTIVE inImage (car (gimp-image-get-active-layer inImage)))
(set! imgWidth (car (gimp-image-width inImage)))
(set! imgHeight (car (gimp-image-height inImage)))
(if
(< imgWidth imgHeight)
(begin
(gimp-image-scale inImage (/ (* (- inStickerSize (* 2 inBorderSize)) imgWidth) imgHeight) (- inStickerSize (* 2 inBorderSize)))
)
(begin
(gimp-image-scale inImage (- inStickerSize (* 2 inBorderSize)) (/ (* (- inStickerSize (* 2 inBorderSize)) imgHeight) imgWidth))
)
)
(set! imgWidth (car (gimp-image-width inImage)))
(set! imgHeight (car (gimp-image-height inImage)))
(gimp-image-resize inImage (+ imgWidth (* 2 inBorderSize)) (+ imgHeight (* 2 inBorderSize)) inBorderSize inBorderSize)
(gimp-image-select-item inImage CHANNEL-OP-REPLACE (car (gimp-image-get-active-layer inImage)))
(gimp-selection-grow inImage inBorderSize)
(gimp-image-insert-layer inImage bgLayer 0 (+ 1 (car (gimp-image-get-item-position inImage (car (gimp-image-get-active-layer inImage))))))
(gimp-layer-resize-to-image-size bgLayer)
(gimp-context-set-background inBorderColour)
(gimp-edit-fill bgLayer FILL-BACKGROUND)
(gimp-context-set-background initBgColour)
(gimp-selection-none inImage)
; Finishing Off
(gimp-image-undo-group-end inImage)
(gimp-context-pop)
(gimp-displays-flush)
)
)
(script-fu-register
"script-fu-text-box" ; func name
"Make Sticker" ; menu label
"Generates telegram stickers from given image" ; description
"Lisek Lucek" ; author
"Copyright 2022, Lisek Lucek" ; copyright notice
"10th of September 2022" ; date created
"" ; image type that the script works on
SF-IMAGE "Image" 0 ; current image
SF-ADJUSTMENT "Sticker Size" '(512 1 10000 1 10 0 1)
SF-ADJUSTMENT "Border Size" '(4 1 1000 1 10 0 1)
SF-COLOR "Background Colour" '(255 255 255)
)
(script-fu-menu-register "script-fu-text-box" "<Image>/Filters")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment