It is currently Tue Aug 18, 2026 3:24 am


All times are UTC - 5 hours [ DST ]



Post new topic Reply to topic  [ 35 posts ]  Go to page 1, 2  Next
Author Message
 Post subject: "Knitted Look" GIMP 2.10 Plug-in
PostPosted: Sat Nov 09, 2024 11:31 am  (#1) 
Offline
Script Coder
User avatar

Joined: May 07, 2014
Posts: 4527
Location: Canada
Spawned from this topic: viewtopic.php?f=11&t=15936
The plug-in colors it to prepare for knitted look effect (slow but much faster than real knitting :rofl )
run plug in on image,
then use the this image Image
to fill a layer ontop and set multiply mode (adjust opacity to taste).
then duplicate the pattern layer and set to pin light mode (adjust to taste like lower percentage opacity to 1 or 2 percent).


To make it even more realistic you can blur the original layer that got changed/colored by plug-in with blur radius of 2. So that it's like knitted colors light reflect off of each other so colors aren't super sharp.

#!/usr/bin/env python

from gimpfu import *
import math
def knit(image,layer):
    pdb.gimp_image_undo_group_start(image)
    #104 x 65 is the pattern dimension
    p_width = 104/4
    p_height = 65/4
    for y in range(0,int(math.ceil(layer.height/p_height+1))):
        pdb.gimp_progress_update(1.0*y*p_height/layer.height)
        for x in range(0,int(math.ceil(layer.width/p_width+1))):
            x1 = x * p_width
            y1 = y * p_height
            points = [x1+10/4,y1+0,x1+48/4,y1+46/4,x1+78/4,y1+0,x1+104/4,y1-39/4,x1+104/4,y1+26/4,x1+48/4,y1+111/4,x1+0,y1+53/4,x1+0,y1-12/4]
            pdb.gimp_image_select_polygon(image,CHANNEL_OP_REPLACE,16,points)
            R,std_dev,median,pixels,count,percent = pdb.gimp_drawable_histogram(layer,HISTOGRAM_RED,0,1)
            G,std_dev,median,pixels,count,percent = pdb.gimp_drawable_histogram(layer,HISTOGRAM_GREEN,0,1)
            B,std_dev,median,pixels,count,percent = pdb.gimp_drawable_histogram(layer,HISTOGRAM_BLUE,0,1)
            pdb.gimp_context_set_foreground((int(R),int(G),int(B)))
            pdb.gimp_edit_fill(layer,FILL_FOREGROUND)
    pdb.gimp_image_undo_group_end(image)
    pdb.gimp_selection_none(image)
register(
    "python_fu_tt_knit_colors",
    "Colors the image like as if it's knitted",
    "Colors the image like as if it's knitted",
    "author name",
    "copyright name",
    "2024.11.09",
    "A Knit Colors",
    "RGB*",      # Alternately use RGB, RGB*, GRAY*, INDEXED etc.
    [
    #INPUT BEGINS
    (PF_IMAGE, "image", "IMAGE:", None),
    (PF_DRAWABLE, "layer", "DRAWABLE:", None),   
    #INPUT ENDS
    ],
    [],
    knit,
    menu="<Image>/Python-Fu")
main()

# Below is all the example input types for INPUTS for the plug-in which can be cut and pasted into #INPUT BEGINS section and edited to taste (found online years back that I didn't want to constantly look up)
# Since GIMP is free anyways, I thought it would be handy here for me to CUT and PASTE, CHANGE, USE.
#           (PF_INT, "p0", "_INT:", 0), # PF_INT8, PF_INT16, PF_INT32  similar but no difference in Python.
#           (PF_FLOAT, "p02", "_FLOAT:", 3.141),
#           (PF_STRING, "p03", "_STRING:", "foo"),  # alias PF_VALUE
#           (PF_TEXT, "p04", "TEXT:", "bar"),
#           # PF_VALUE
#           # Pick one from set of choices
#           (PF_OPTION,"p1",   "OPTION:", 0, ["0th","1st","2nd"]), # initially 0th is choice
#           (PF_RADIO, "p16", "RADIO:", 0, (("0th", 1),("1st",0))), # note bool indicates initial setting of buttons
#           # PF_RADIO is usually called a radio button group.
#           # SLIDER, ADJUSTMENT types require the extra parameter of the form (min, max, step).
#           (PF_TOGGLE, "p2",   "TOGGLE:", 1), # initially True, checked.  Alias PF_BOOL
#           # PF_TOGGLE is usually called a checkbox.
#           (PF_SLIDER, "p3", "SLIDER:", 0, (0, 100, 10)),
#           (PF_SPINNER, "p4", "SPINNER:", 21, (1, 1000, 50)),  # alias PF_ADJUSTMENT
#           # Pickers ie combo boxes ie choosers from lists of existing Gimp objects
#           (PF_COLOR, "p14", "_COLOR:", (100, 21, 40) ), # extra param is RGB triple
#           # PF_COLOUR is an alias by aussie PyGimp author lol
#           (PF_IMAGE, "p15", "IMAGE:", None), # should be type gimp.image, but None works
#           (PF_FONT, "p17", "FONT:", 0),
#           (PF_FILE, "p18", "FILE:", 0),
#           (PF_BRUSH, "p19", "BRUSH:", 0),
#           (PF_PATTERN, "p20", "PATTERN:", 0),
#           (PF_GRADIENT, "p21", "GRADIENT:", 0),
#           (PF_PALETTE, "p22", "PALETTE:", 0),
#           (PF_LAYER, "p23", "LAYER:", None),
#           (PF_CHANNEL, "p24", "CHANNEL:", None),  # ??? Usually empty, I don't know why.
#           (PF_DRAWABLE, "p25", "DRAWABLE:", None),
#           # Mostly undocumented, but work
#           (PF_VECTORS, "p26", "VECTORS:", None),
#           (PF_FILENAME, "p27", "FILENAME:", 0),
#           (PF_DIRNAME, "p28", "DIRNAME:", 0)
#           # PF_REGION might work but probably of little use.  See gimpfu.py.
Image

If you plan to scale down pattern, use code in post #5 as it allows you specify what scaling from half, third...tenth of pattern size so that when you scale pattern the coloring still align with scaled texture pattern.

Full-service version in Post #20 (where you don't have play with pattern.png as it is included in plug-in, you'll just have to specify Knit-V shape's width [in pixels])


Attachments:
File comment: knit-colors.py to color image to prepare for knit effect
knit-colors.zip [1.91 KiB]
Downloaded 1948 times

_________________
TinT


Last edited by trandoductin on Mon Nov 11, 2024 5:05 am, edited 4 times in total.
Share on Facebook Share on Twitter Share on Orkut Share on Digg Share on MySpace Share on Delicious Share on Technorati
Top
 Post subject: Re: "Knitted Look" GIMP 2.10 Plug-in
PostPosted: Sat Nov 09, 2024 11:42 am  (#2) 
Offline
Script Coder
User avatar

Joined: May 07, 2014
Posts: 4527
Location: Canada
Image

_________________
TinT


Top
 Post subject: Re: "Knitted Look" GIMP 2.10 Plug-in
PostPosted: Sat Nov 09, 2024 11:47 am  (#3) 
Offline
Script Coder
User avatar

Joined: May 07, 2014
Posts: 4527
Location: Canada
Where is dinasset?
Image

_________________
TinT


Top
 Post subject: Re: "Knitted Look" GIMP 2.10 Plug-in
PostPosted: Sat Nov 09, 2024 1:23 pm  (#4) 
Offline
GimpChat Member
User avatar

Joined: Mar 01, 2014
Posts: 14133
Location: Spain, Aragón
So cool! Thanks a lot tran. :hi5 :tyspin
I scaled the pattern to be more adapted to the image size. :)


Attachments:
Pink-Flower-Knit_Colors.jpg
Pink-Flower-Knit_Colors.jpg [ 333.65 KiB | Viewed 6396 times ]

_________________
Image

Gimp 2.10.30(samj) portable _ OS Windows 10 Home_ 64bits
Don’t be afraid to start over. It’s a new chance to rebuild what you want.
Top
 Post subject: Re: "Knitted Look" GIMP 2.10 Plug-in
PostPosted: Sat Nov 09, 2024 4:00 pm  (#5) 
Offline
Script Coder
User avatar

Joined: May 07, 2014
Posts: 4527
Location: Canada
You're welcome,
if you scale the pattern, then we gotta change the plug-in too so that the coloring aligned with the texture mapping.
What did you scale the pattern down by, what fraction or percentage? 1/2?
Here ya go Bella,
this will allow you to tell plug-in what fraction/scale you scaled down pattern by, you can choose between half,third,forth,...all the way to a tenth of pattern size.
#!/usr/bin/env python

from gimpfu import *
import math
def knit(image,layer,scale):
    pdb.gimp_image_undo_group_start(image)
    scale = int(scale)
    scale = 1.0/(scale+2) #if it's 0 scale becomes half, if it's option 8 it becomes 1/10th.
    #104 x 65 is the pattern dimension
    p_width = 104/4*scale
    p_height = 65/4*scale
    for y in range(0,int(math.ceil(layer.height/p_height+1))):
        pdb.gimp_progress_update(1.0*y*p_height/layer.height)
        for x in range(0,int(math.ceil(layer.width/p_width+1))):
            x1 = x * p_width
            y1 = y * p_height
            points = [x1+10/4*scale,y1,x1+48/4*scale,y1+46/4*scale,x1+78/4*scale,y1,x1+104/4*scale,y1-39/4*scale,x1+104/4*scale,y1+26/4*scale,x1+48/4*scale,y1+111/4*scale,x1,y1+53/4*scale,x1+0,y1-12/4*scale]
            pdb.gimp_image_select_polygon(image,CHANNEL_OP_REPLACE,16,points)
            if (pdb.gimp_selection_is_empty(image)==TRUE):
                pass
            else:
                R,std_dev,median,pixels,count,percent = pdb.gimp_drawable_histogram(layer,HISTOGRAM_RED,0,1)
                G,std_dev,median,pixels,count,percent = pdb.gimp_drawable_histogram(layer,HISTOGRAM_GREEN,0,1)
                B,std_dev,median,pixels,count,percent = pdb.gimp_drawable_histogram(layer,HISTOGRAM_BLUE,0,1)
                pdb.gimp_context_set_foreground((int(R),int(G),int(B)))
                pdb.gimp_edit_fill(layer,FILL_FOREGROUND)
    pdb.gimp_image_undo_group_end(image)
    pdb.gimp_selection_none(image)

register(
    "python_fu_tt_knit_colors",
    "Colors the image like as if it's knitted",
    "Colors the image like as if it's knitted",
    "author name",
    "copyright name",
    "2024.11.09",
    "A Knit Colors",
    "RGB*",      # Alternately use RGB, RGB*, GRAY*, INDEXED etc.
    [
    #INPUT BEGINS
    (PF_IMAGE, "image", "IMAGE:", None),
    (PF_DRAWABLE, "layer", "DRAWABLE:", None),
    (PF_OPTION,"scale",   "Scaling Pattern:", 0, ["1 half","1 third","1 quarter","1 fifth","1 sixth","1 seventh","1 eighth","1 nineth","1 tenth"]), # initially 0th is choice
    #INPUT ENDS
    ],
    [],
    knit,
    menu="<Image>/Python-Fu")
main()

# Below is all the example input types for INPUTS for the plug-in which can be cut and pasted into #INPUT BEGINS section and edited to taste (found online years back that I didn't want to constantly look up)
# Since GIMP is free anyways, I thought it would be handy here for me to CUT and PASTE, CHANGE, USE.
#           (PF_INT, "p0", "_INT:", 0), # PF_INT8, PF_INT16, PF_INT32  similar but no difference in Python.
#           (PF_FLOAT, "p02", "_FLOAT:", 3.141),
#           (PF_STRING, "p03", "_STRING:", "foo"),  # alias PF_VALUE
#           (PF_TEXT, "p04", "TEXT:", "bar"),
#           # PF_VALUE
#           # Pick one from set of choices
#           (PF_OPTION,"p1",   "OPTION:", 0, ["0th","1st","2nd"]), # initially 0th is choice
#           (PF_RADIO, "p16", "RADIO:", 0, (("0th", 1),("1st",0))), # note bool indicates initial setting of buttons
#           # PF_RADIO is usually called a radio button group.
#           # SLIDER, ADJUSTMENT types require the extra parameter of the form (min, max, step).
#           (PF_TOGGLE, "p2",   "TOGGLE:", 1), # initially True, checked.  Alias PF_BOOL
#           # PF_TOGGLE is usually called a checkbox.
#           (PF_SLIDER, "p3", "SLIDER:", 0, (0, 100, 10)),
#           (PF_SPINNER, "p4", "SPINNER:", 21, (1, 1000, 50)),  # alias PF_ADJUSTMENT
#           # Pickers ie combo boxes ie choosers from lists of existing Gimp objects
#           (PF_COLOR, "p14", "_COLOR:", (100, 21, 40) ), # extra param is RGB triple
#           # PF_COLOUR is an alias by aussie PyGimp author lol
#           (PF_IMAGE, "p15", "IMAGE:", None), # should be type gimp.image, but None works
#           (PF_FONT, "p17", "FONT:", 0),
#           (PF_FILE, "p18", "FILE:", 0),
#           (PF_BRUSH, "p19", "BRUSH:", 0),
#           (PF_PATTERN, "p20", "PATTERN:", 0),
#           (PF_GRADIENT, "p21", "GRADIENT:", 0),
#           (PF_PALETTE, "p22", "PALETTE:", 0),
#           (PF_LAYER, "p23", "LAYER:", None),
#           (PF_CHANNEL, "p24", "CHANNEL:", None),  # ??? Usually empty, I don't know why.
#           (PF_DRAWABLE, "p25", "DRAWABLE:", None),
#           # Mostly undocumented, but work
#           (PF_VECTORS, "p26", "VECTORS:", None),
#           (PF_FILENAME, "p27", "FILENAME:", 0),
#           (PF_DIRNAME, "p28", "DIRNAME:", 0)
#           # PF_REGION might work but probably of little use.  See gimpfu.py.

_________________
TinT


Top
 Post subject: Re: "Knitted Look" GIMP 2.10 Plug-in
PostPosted: Sun Nov 10, 2024 1:34 am  (#6) 
Offline
GimpChat Member
User avatar

Joined: Dec 09, 2018
Posts: 736
Nice work, thank you Tim :clap :tyspin

I gave it a try using the first version of the plug-in but to make the pattern match I had to scale it by 1/4 so from 104x65 to 26x16. Didn't try the version in post #5 as I was too confused by the scaling by then.

Is it too early for a Christmas jumper?

Attachment:
knit-santa.jpg
knit-santa.jpg [ 257.01 KiB | Viewed 6349 times ]


Top
 Post subject: Re: "Knitted Look" GIMP 2.10 Plug-in
PostPosted: Sun Nov 10, 2024 3:53 am  (#7) 
Offline
GimpChat Member
User avatar

Joined: Mar 01, 2014
Posts: 14133
Location: Spain, Aragón
Thanks again Tran. I tested your Knit Colors v2 of the plug-in.
I think that the result is better about quality of the final image.
However I must say that the plug-in had much time, more than before.
Here my result.


Attachments:
fishing-Knit_Colors.jpg
fishing-Knit_Colors.jpg [ 697.98 KiB | Viewed 6335 times ]

_________________
Image

Gimp 2.10.30(samj) portable _ OS Windows 10 Home_ 64bits
Don’t be afraid to start over. It’s a new chance to rebuild what you want.
Top
 Post subject: Re: "Knitted Look" GIMP 2.10 Plug-in
PostPosted: Sun Nov 10, 2024 6:34 am  (#8) 
Offline
Script Coder
User avatar

Joined: May 07, 2014
Posts: 4527
Location: Canada
Issabella: for some reason your image looks like it's an image that is has not been ran by plugin since the image is still there and not shaped like the knitted coloring.
I don't know.
teapot: nice one

_________________
TinT


Top
 Post subject: Re: "Knitted Look" GIMP 2.10 Plug-in
PostPosted: Sun Nov 10, 2024 6:38 am  (#9) 
Offline
GimpChat Member
User avatar

Joined: Mar 01, 2014
Posts: 14133
Location: Spain, Aragón
I can'r explain it. It took almos 1 hour as I applied 1 fifth (perhaps too small)

_________________
Image

Gimp 2.10.30(samj) portable _ OS Windows 10 Home_ 64bits
Don’t be afraid to start over. It’s a new chance to rebuild what you want.


Top
 Post subject: Re: "Knitted Look" GIMP 2.10 Plug-in
PostPosted: Sun Nov 10, 2024 10:12 am  (#10) 
Offline
Script Coder
User avatar

Joined: May 07, 2014
Posts: 4527
Location: Canada
Issabella wrote:
I can'r explain it. It took almos 1 hour as I applied 1 fifth (perhaps too small)

The scale is so you can choose the scaling factor of whatever you decide to scale the pattern.png down to.

_________________
TinT


Top
 Post subject: Re: "Knitted Look" GIMP 2.10 Plug-in
PostPosted: Sun Nov 10, 2024 11:08 am  (#11) 
Offline
GimpChat Member
User avatar

Joined: Mar 01, 2014
Posts: 14133
Location: Spain, Aragón
Thanks Tin. Please, is it alright now?
I applied a third to reduce the pattern.


Attachments:
fishing-700x545_third.jpg
fishing-700x545_third.jpg [ 131.43 KiB | Viewed 6299 times ]
fishing-700x545_third-Outcome.jpg
fishing-700x545_third-Outcome.jpg [ 200.36 KiB | Viewed 6299 times ]

_________________
Image

Gimp 2.10.30(samj) portable _ OS Windows 10 Home_ 64bits
Don’t be afraid to start over. It’s a new chance to rebuild what you want.
Top
 Post subject: Re: "Knitted Look" GIMP 2.10 Plug-in
PostPosted: Sun Nov 10, 2024 1:13 pm  (#12) 
Offline
Script Coder
User avatar

Joined: May 07, 2014
Posts: 4527
Location: Canada
it's better but it still doesn't match exactly as you can see some V's with more than one color.

_________________
TinT


Top
 Post subject: Re: "Knitted Look" GIMP 2.10 Plug-in
PostPosted: Sun Nov 10, 2024 2:05 pm  (#13) 
Offline
GimpChat Member
User avatar

Joined: Mar 01, 2014
Posts: 14133
Location: Spain, Aragón
Sorry Tin, I don't know how to do it better. :gaah
I'll try it with a simplier image, perhaps. Anyway thanks for your patience and your plug-in. :tyspin

_________________
Image

Gimp 2.10.30(samj) portable _ OS Windows 10 Home_ 64bits
Don’t be afraid to start over. It’s a new chance to rebuild what you want.


Top
 Post subject: Re: "Knitted Look" GIMP 2.10 Plug-in
PostPosted: Sun Nov 10, 2024 4:58 pm  (#14) 
Offline
Script Coder
User avatar

Joined: May 07, 2014
Posts: 4527
Location: Canada
It's like this for example if you chose to scale the pattern.png that I attached by 50% then choose plug-in to run a 1 half.
If you scale pattern.png by 25% then choose plug-in to run at 1 quarter. and it'll match perfectly :D

_________________
TinT


Top
 Post subject: Re: "Knitted Look" GIMP 2.10 Plug-in
PostPosted: Sun Nov 10, 2024 6:04 pm  (#15) 
Offline
GimpChat Member
User avatar

Joined: Dec 09, 2018
Posts: 736
trandoductin wrote:
It's like this for example if you chose to scale the pattern.png that I attached by 50% then choose plug-in to run a 1 half.
If you scale pattern.png by 25% then choose plug-in to run at 1 quarter. and it'll match perfectly :D

This would be the case if you took out the additional scale by 1/4 that's thoughout the code.

Version 1 was scale by 1/4.

Version 2 scales by both 1/4 and the variable 'scale' but surely it should be just the variable 'scale'. Choosing a quarter in in the option menu would then match version 1.

That said I like results this plug-in makes a lot, thank you again :tyspin


Top
 Post subject: Re: "Knitted Look" GIMP 2.10 Plug-in
PostPosted: Sun Nov 10, 2024 8:23 pm  (#16) 
Offline
Script Coder
User avatar

Joined: May 07, 2014
Posts: 4527
Location: Canada
Both versions /4 before scale is applied because the pattern.png I shared is already scaled down by me. The original was huge.
Choosing 1 quarter would not match version 1 because it would be like version /4 then multiplied by (1/4) which would be 1/16 of original pattern which I did not share.
If you like it a lot you should blame it on dinasset which spawned this. :hehe

_________________
TinT


Top
 Post subject: Re: "Knitted Look" GIMP 2.10 Plug-in
PostPosted: Mon Nov 11, 2024 3:53 am  (#17) 
Offline
GimpChat Member
User avatar

Joined: Mar 01, 2014
Posts: 14133
Location: Spain, Aragón
:tyspin Thanks Tin.


Attachments:
robin-knit-colors-Issa.jpg
robin-knit-colors-Issa.jpg [ 723.07 KiB | Viewed 6225 times ]

_________________
Image

Gimp 2.10.30(samj) portable _ OS Windows 10 Home_ 64bits
Don’t be afraid to start over. It’s a new chance to rebuild what you want.
Top
 Post subject: Re: "Knitted Look" GIMP 2.10 Plug-in
PostPosted: Mon Nov 11, 2024 3:56 am  (#18) 
Offline
Script Coder
User avatar

Joined: May 07, 2014
Posts: 4527
Location: Canada
This one still looks like it's half color and not colored by plugin?
What's did you scale pattern.png down to, please attach original bird image. I'll do it.

_________________
TinT


Top
 Post subject: Re: "Knitted Look" GIMP 2.10 Plug-in
PostPosted: Mon Nov 11, 2024 4:30 am  (#19) 
Offline
GimpChat Member
User avatar

Joined: Mar 01, 2014
Posts: 14133
Location: Spain, Aragón
Thanks Tin


Attachments:
robin-9094847_1280.jpg
robin-9094847_1280.jpg [ 152.4 KiB | Viewed 6219 times ]

_________________
Image

Gimp 2.10.30(samj) portable _ OS Windows 10 Home_ 64bits
Don’t be afraid to start over. It’s a new chance to rebuild what you want.
Top
 Post subject: Re: "Knitted Look" GIMP 2.10 Plug-in
PostPosted: Mon Nov 11, 2024 4:47 am  (#20) 
Offline
Script Coder
User avatar

Joined: May 07, 2014
Posts: 4527
Location: Canada
I decided to make it less confusing and have the plug-in do all the dirty work.
This version of plug-in has the pattern.png encoded/included inside it so you don't have to deal with pattern.png at all.
Just specify what width (in pixels) you want the individual knit-V shape to be.
It'll create multiply layer and pin light layer, you'll just have to play opacity of these two layers and possibly blur the colored layer (2px blur radius).
Issabella I think you'll like this version of plug-in


Attachments:
File comment: release 3. includes pattern.png inside the plug-in so you don't have to deal with manual making multiply mode and pin light mode just adjust opacity of those two layers afterwards to taste if you like
knit-colors.zip [16.48 KiB]
Downloaded 1847 times

_________________
TinT
Top
Post new topic Reply to topic  [ 35 posts ]  Go to page 1, 2  Next

All times are UTC - 5 hours [ DST ]



* Login  



Powered by phpBB3 © phpBB Group