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!
The completed form looks like this
Before editing | After editing |
---|---|
#!/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
AddLGTM is added to Filter (R) → Python-Fu as shown in the image.
The image with "LGTM" added is saved in the folder selected by the following procedure.
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)
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.
You can extend the selection by 2px by doing pdb.gimp_selection_grow (image, 2)
: clap:
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