Create LGTM images with GIMP! (Python-fu)

Introduction

The number of remote work is increasing, and code reviews end with exchanging comments. .. .. Lonely: sob: LGTM images add color to such code reviews! This time, I will use python-fu, a free image editing software GIMP, to create LGTM images!

environment

Deliverables

The completed form looks like this

Before editing After editing
before after
source
#!/usr/bin/env python
# -*- coding: utf-8 -*-

from gimpfu import *
import os

def plugin_main(image, layer, dir_path):
  if (not type(dir_path) == type("") or not len(dir_path)):
    gimp.message("Please select a folder")
    return

  add_lgtm(image, layer, dir_path)

def draw_text(image, size, text):
  layer = pdb.gimp_text_layer_new(image, text, "Hiragino Sans", size, 0)
  image.add_layer(layer)
  #Make it white
  pdb.gimp_text_layer_set_color(layer, (255, 255, 255, 1.0))
  return layer

def draw_shadow(image, text_layer):
  #Range selection
  pdb.gimp_selection_none(image)
  pdb.gimp_image_select_item(image, 0, text_layer)

  #Drawing color setting
  maskcolor = (0, 0, 0, 1.0)
  pdb.gimp_context_set_foreground(maskcolor)

  opacity = 15 #make it thin
  shadow_layer = gimp.Layer(image, "shadow", image.width, image.height, RGB_IMAGE, opacity, NORMAL_MODE)
  image.add_layer(shadow_layer, 1) #Add to the back of the text

  #Add alpha channel
  pdb.gimp_layer_add_alpha(shadow_layer)
  pdb.gimp_edit_clear(shadow_layer)
  shadow_layer.fill(TRANSPARENT_FILL)

  #Expand selection
  pdb.gimp_selection_grow(image, 2)
  #fill
  pdb.gimp_edit_fill(shadow_layer, FOREGROUND_FILL)
  
def cal_font_size(image):
  width = image.width / 4
  height = image.height
  max_font_size = 160
  return min([width, height, max_font_size])

def save_file(image, layer, dir_path, file_name): 
  file_path = dir_path + '/' + file_name
  #Save image
  pdb.file_png_save(image, layer, file_path, file_path, 1.0, 0, 0, 0, 0, 0, 0)

def add_lgtm(image, layer, dir_path): 
  font_size = cal_font_size(image)
  text_layer = draw_text(image, font_size, 'LGTM')

  #Top and bottom center alignment
  x = image.width/2 - text_layer.width/2
  y = image.height/2 - text_layer.height/2
  text_layer.set_offsets(x, y)

  draw_shadow(image, text_layer)

  #Layer integration
  flatten_layer = pdb.gimp_image_flatten(image)

  #Save
  file_name = 'lgtm.png'
  save_file(image, flatten_layer, dir_path, file_name)

register(
  "python_fu_add_lgtm",
  "Add LGTM",
  "Add LGTM text",
  "am",
  "am",
  "2020/5/11",
  "<Image>/Filters/Languages/Python-Fu/AddLGTM", 
  "RGB*, GRAY*", 
  [
  (PF_DIRNAME, "directory_path", "Save directoryPath", ""),
  ],
  [], 
  plugin_main) 

main()

Copy the above source, grant permissions, store it in the folder below, and start GIMP.

Library/Application Support/GIMP/2.10/plug-ins
python-fu

AddLGTM is added to Filter (R) → Python-Fu as shown in the image.

How to use

The image with "LGTM" added is saved in the folder selected by the following procedure.

  1. Open the image with GIMP
  2. Filter (R) → Python-Fu → Add LGTM selection
  3. Select save destination
  4. OK

How to make

Please refer to this article I wrote earlier for how to create python-fu. Write about how to make a GIMP script (python-fu)? : punch :: punch:

Since I had to use various procedures such as adding characters and adding character borders for the one I made this time, I will describe how to search for the procedure.

This article was very helpful. Batch level correction of images with GIMP (Python-Fu)

How to find a procedure

  1. Search for the corresponding function in GIMP (GNU Image Editing Program)
  2. Use GIMP Help (H) → Procedure Browser (B) to search for the end of the URL of the relevant page.

For example, if you want to select an LGTM string and search for a procedure to extend this selection.

There is a page called 4.12. Expand Selection ... The URL is as follows. https://docs.gimp.org/ja/gimp-selection-grow.html If you search for gimp-selection-grow in GIMP Help (H) → Procedure Browser (B), the following will be hit.

procedure

You can extend the selection by 2px by doing pdb.gimp_selection_grow (image, 2): clap:

at the end

I wanted to use GIMP for a long time, but I'm tired of opening it with GIMP to add LGTM: confused: I think there are many other services for creating LGTM images, so you can search for them: rolling_eyes: The script has comments, so if you don't like it, please rewrite it and use it! Let's mass-produce LGTM images together and add color to code reviews: fire:

Recommended Posts

Create LGTM images with GIMP! (Python-fu)
Create a batch of images and inflate with ImageDataGenerator
Create games with Pygame
Create filter with scipy
Center images with python-pptx
LGTM --Compose LGTM images with videos and photos and output GIF animation
Create a web API that can deliver images with Django
Automatically download images with scraping
Bordering images with python Part 1
Create an environment with virtualenv
Create an API with Django
Create / search / create table with PynamoDB
Create 3d gif with python3
Save images with web scraping
Create graph with plotly button
Create a homepage with django
Create Image Viewer with Tkinter
Create custom rules with ElastAlert
Create a heatmap with pyqtgraph
Combine two images with Django
Create a directory with python
Create xlsx file with XlsxWriter
Start in 5 minutes GIMP Python-Fu
Create a program that can generate your favorite images with Selenium