|
| 1 | +#!/usr/bin/env python |
| 2 | +# -*- coding: utf-8 -*- |
| 3 | + |
| 4 | +from gimpfu import * |
| 5 | + |
| 6 | + |
| 7 | +def apply_cartoon_filter(img, drawable): |
| 8 | + # see https://developer.gimp.org/api/2.0/libgimp/index.html |
| 9 | + # step 1: duplicate layer two times |
| 10 | + # layer2 |
| 11 | + layer2 = pdb.gimp_layer_copy(drawable, False) |
| 12 | + pdb.gimp_image_add_layer(img, layer2, 0) |
| 13 | + pdb.gimp_item_set_name(layer2, "temp_merge_layer") |
| 14 | + |
| 15 | + # layer3 |
| 16 | + layer3 = pdb.gimp_layer_copy(drawable, False) |
| 17 | + pdb.gimp_image_add_layer(img, layer3, 0) |
| 18 | + pdb.gimp_item_set_name(layer3, "temp_sobel_operator") |
| 19 | + # set layer3 mode to GIMP_GRAIN_EXTRACT_MODE (20) |
| 20 | + pdb.gimp_layer_set_mode(layer3, 20) |
| 21 | + |
| 22 | + # step 2: execute sobel operator |
| 23 | + # alternative pdb.plug_in_sobel(img, layer3, True, True, True) |
| 24 | + pdb.plug_in_edge(img, layer3, 1, 0, 0) |
| 25 | + |
| 26 | + # step 3: adjust curves |
| 27 | + # see https://developer.gimp.org/api/2.0/libgimp/libgimp-gimpcolor.html#gimp-curves-spline |
| 28 | + pdb.gimp_curves_spline(layer3, 0, 6, |
| 29 | + [0, 0, |
| 30 | + 130, 255, |
| 31 | + 255, 255]) |
| 32 | + |
| 33 | + # step 4: adjust levels |
| 34 | + # see https://developer.gimp.org/api/2.0/libgimp/libgimp-gimpcolor.html#gimp-levels |
| 35 | + pdb.gimp_levels(layer3, 0, 0, 130, 1.0, 0, 255) |
| 36 | + |
| 37 | + # step 5: merge layers down |
| 38 | + pdb.gimp_image_merge_down(img, layer3, 0) |
| 39 | + # set layer2 mode to GIMP_OVERLAY_MODE (23) |
| 40 | + # see https://developer.gimp.org/api/2.0/libgimp/libgimp-gimpenums.html#GimpLayerModeEffects |
| 41 | + pdb.gimp_layer_set_mode(img.layers[0], 23) |
| 42 | + pdb.gimp_image_merge_down(img, img.layers[0], 0) |
| 43 | + |
| 44 | + |
| 45 | +register( |
| 46 | + "python-fu-cartoonify", # Plugin name |
| 47 | + "Apply cartoon filter", # Short description |
| 48 | + "Apply cartoon filter", # Long description |
| 49 | + "Pascal Reitermann", # Plugin author |
| 50 | + "Pascal Reitermann", # Copyright or license |
| 51 | + "2022", # Year of publishing |
| 52 | + "Cartoonify...", # Name of the plug-in |
| 53 | + "*", # Supported images |
| 54 | + [ |
| 55 | + (PF_IMAGE, "img", "Input image", None), |
| 56 | + (PF_DRAWABLE, "drawable", "Input drawable", None), |
| 57 | + ], # Input parameter |
| 58 | + [], # Output result |
| 59 | + apply_cartoon_filter, # Name of the function |
| 60 | + menu="<Image>/Filters/Artistic" |
| 61 | + ) |
| 62 | + |
| 63 | + |
| 64 | +main() |
0 commit comments