Apply texture on selected Area (Color) of an Image

Questions and postings pertaining to the development of ImageMagick, feature enhancements, and ImageMagick internals. ImageMagick source code and algorithms are discussed here. Usage questions which are too arcane for the normal user list should also be posted here.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Apply texture on selected Area (Color) of an Image

Post by fmw42 »

Extract the alpha channel as a separate image, then apply texture to whole image after turning alpha off, the apply alpha channel back at the end.

Image
Image

convert aqua_font.png \
\( -clone 0 -alpha extract \) \
\( -clone 0 -alpha off -size 66x58 tile:texture_fabric.gif \
-compose hardlight -composite \) \
-delete 0 +swap -compose copy_opacity -composite aqua_font_texture.png

or this so that you don't have to know how big the image is (but it is more complex)

convert aqua_font.png \
\( -clone 0 -alpha extract \) \
\( -clone 0 -alpha off texture_fabric.gif -channel rgba -virtual-pixel tile -fx "v" \) \
\( -clone 0 -alpha off -clone 2 -compose hardlight -composite \) \
-delete 0,2 +swap -compose copy_opacity -composite aqua_font_texture.png


Image
sanjeevk
Posts: 26
Joined: 2009-08-26T02:17:36-07:00
Authentication code: 8675309
Location: India
Contact:

Re: Apply texture on selected Area (Color) of an Image

Post by sanjeevk »

Thanks ,

It's working .

:D

Thanks.
Sanjeev Kumar
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Apply texture on selected Area (Color) of an Image

Post by anthony »

You do not need to 'extract the alpha'. The original image can be used as the masking image.

Code: Select all

 convert aqua_font.png \
             \( -clone 0 -alpha off texture_fabric.gif -compose HardLight -composite \) \
             -compose SrcIn -composite  result.png
Image

The compose method SrcIN means the Src image (just composed using Hardlight) IN the Destination (the original image). Of course it assumes the texture image is large enough
to cover the input.

If you need to tile the texture image their are many ways to do this.
See IM Examples, Tiling images.
http://www.imagemagick.org/Usage/canvas/canvas/#tile

Few good methods however exist to tile an image to cover some other image, the best is method is currently -draw "color 0,0 reset" (see above link).

It gets even harder if the image you want to tile is in memory! The best method here is to use mpc: with -draw (also see the above).
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
sanjeevk
Posts: 26
Joined: 2009-08-26T02:17:36-07:00
Authentication code: 8675309
Location: India
Contact:

Re: Apply texture on selected Area (Color) of an Image

Post by sanjeevk »

Hi,
fmw42


I am doing one more thing with this image.

Image

I am trying to change the color of this image using following command.

convert aqua_font_texture.png -fuzz 10% -fill \#FF0000 -opaque \#2786E3 abc.png;

but we did not get good result. Can you suggest some thing on the same.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Apply texture on selected Area (Color) of an Image

Post by fmw42 »

Anthony is the real expert here on this, but giving it a try using his method:

convert aqua_font_texture.png -alpha off -colorspace gray +level-colors black,red -alpha on aqua_font_texture_red.png

Image


see +level-colors http://www.imagemagick.org/Usage/color/#level-colors
sanjeevk
Posts: 26
Joined: 2009-08-26T02:17:36-07:00
Authentication code: 8675309
Location: India
Contact:

Re: Apply texture on selected Area (Color) of an Image

Post by sanjeevk »

Thanks,
It' working.

Sanjeev Kumar
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Apply texture on selected Area (Color) of an Image

Post by anthony »

If you have a pure blue, (or red) gradient image, then you can use -modulate to 'rotate' the hue of the image to the color desired.
http://www.imagemagick.org/Usage/color/#modulate

The use of +level-colors replaces the white (or black) of a grayscale image with a specific color. It is a bit like hue rotation of the colors in 'blue' image but starting with a grayscale image
http://www.imagemagick.org/Usage/color/#level-colors

If the image is grey-scale, you can use -tint to color the mid-tones, leaving black and white, black and white!
http://www.imagemagick.org/Usage/color/#tint

Also if the image is grey-scale, you can use composition 'light' methods to color the mid-tones using a solid color overlay, or a color pattern. Which loops us back to the original problem. On other words the same answer.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
sanjeevk
Posts: 26
Joined: 2009-08-26T02:17:36-07:00
Authentication code: 8675309
Location: India
Contact:

Re: Apply texture on selected Area (Color) of an Image

Post by sanjeevk »

Hi ,

I am using this command

convert aqua_font.png \
\( -clone 0 -alpha extract \) \
\( -clone 0 -alpha off texture_fabric.gif -channel rgba -virtual-pixel tile -fx "v" \) \
\( -clone 0 -alpha off -clone 2 -compose hardlight -composite \) \
-delete 0,2 +swap -compose copy_opacity -composite aqua_font_texture.png

for applying texture. It's working with IM 6.5.5-10.
but the same command does not work on the 6.2.8

can you suggest me the alternative for above command that run on the IM 6.2.8

thanks,
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Apply texture on selected Area (Color) of an Image

Post by fmw42 »

untested, but try

convert aqua_font.png \
\( -clone 0 -channel alpha -separate \) \
\( -clone 0 +matte texture_fabric.gif -channel rgba -virtual-pixel tile -fx "v" \) \
\( -clone 0 +matte -clone 2 -compose hardlight -composite \) \
-delete 0,2 +swap -compose copy_opacity -composite aqua_font_texture.png
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Apply texture on selected Area (Color) of an Image

Post by anthony »

Using -fx 'v' is VERY slow.

A faster way to tile an image over an existing image is to use -draw.

Code: Select all

convert aqua_font.png -tile texture_fabric.gif -draw 'color 0,0 reset' tile_filled_image.png
Note that the alpha channel does not need to be cleared. But you are limited to a simple tile method, you do not have access to the various virtual pixel settings to define different types of tiling styles such as 'Mirror' or 'CheckerTile' (which was fixed/added resp. in IM v6.5.0-1)

The only problem with this is that -tile only takes an external 'coder' provided image. If the image to be tiles is in-memory, you will need to set it using an MPR: register.

See Tiling Images
http://www.imagemagick.org/Usage/canvas/#tile
More specifically, Tiling in-memory images,
http://www.imagemagick.org/Usage/canvas/#tile_memory

Another FAST method but one which may be too new, is to use a viewport distort. This also allows you to distort the image tiling in various ways.

Code: Select all

     convert aqua_font.png -set option:distort:viewport '%g'  + delete\
                 texture_fabric.gif -virtual-pixel tile -distort SRT 0   tile_filled_image.png
See http://www.imagemagick.org/Usage/distor ... t_viewport
And distorted tiling in Affine Tiling http://www.imagemagick.org/Usage/distorts/#affine_tile

Note that the viewport is set using one image but then applied to a completely different image!
This is actually a bit of a kludge and relies on knowledge of how IM handles settings when new images are read in.

ASIDE: Normally the setting is calculated and set to each image currently in memory (only the one image in this case), but new images will inherited the previously calculated (and thus possibility wrong) setting from previous images (even if they are all deleted as in the above!). This relies on that setting inheritance, and thus a bit of a 'kludge', as the setting was not directly assigned to the image being distorted.

I'll add an example of this technique to the previous 'in-memory tiling' section, for virtual pixel tiling.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
sanjeevk
Posts: 26
Joined: 2009-08-26T02:17:36-07:00
Authentication code: 8675309
Location: India
Contact:

Re: Apply texture on selected Area (Color) of an Image

Post by sanjeevk »

Hi All,

Your commands does not work as per my requirement. Now I ma using following command for this.


convert aqua_font.png \( -clone 0 +matte\) \
\( -clone 0 +matte texture_fabric.gif -channel rgba -virtual-pixel tile -fx "v" \) \
\( -clone 0 +matte -clone 2 -compose hardlight -composite \) \
-delete 0,2 +swap -compose copy_opacity -composite aqua_font_texture.png

this command is working for me on IM-6.2.8.

If you have any suggestion on the same. Please let me know.
And I also want to know the working process of this command please let me know.

thanks.
sanjeevk
Posts: 26
Joined: 2009-08-26T02:17:36-07:00
Authentication code: 8675309
Location: India
Contact:

Re: Apply texture on selected Area (Color) of an Image

Post by sanjeevk »

Test
Last edited by sanjeevk on 2010-01-26T21:51:16-07:00, edited 1 time in total.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Apply texture on selected Area (Color) of an Image

Post by fmw42 »

sorry I don't think I can be of much help. First your first web site does not work completely. When I try to change the color I get error messages or no change. Second I have no Idea how to compare the second site with the first. Nor if I did, I would not know how one processed the images vs how the other processed the image. Or if either was even using IM and what functions were used.
sanjeevk
Posts: 26
Joined: 2009-08-26T02:17:36-07:00
Authentication code: 8675309
Location: India
Contact:

Re: Apply texture on selected Area (Color) of an Image

Post by sanjeevk »

sorry ! There was some problem on my server. Now we have fixed this. You can review this.
In this application I also want to implement associate color functionality. I am working on this feature.
Post Reply