how to specify diffferent color in a loop in script

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
manit
Posts: 123
Joined: 2009-01-30T22:31:26-07:00

how to specify diffferent color in a loop in script

Post by manit » 2019-04-21T07:18:45-07:00

Hi ,
I am using imagemagick on 64bit ubuntu OS
$ convert -version
Version: ImageMagick 6.8.9-9 Q16 x86_64 2018-09-28 http://www.imagemagick.org
Copyright: Copyright (C) 1999-2014 ImageMagick Studio LLC
Features: DPC Modules OpenMP
Delegates: bzlib cairo djvu fftw fontconfig freetype jbig jng jpeg lcms lqr ltdl lzma openexr pangocairo png rsvg tiff wmf x xml zlib

I am drawing circles in a script using convert.
I want to choose a different color every time (no white , no black)
Is there a way to change different color by incrementing a number so that I can use variable value in loop.
It would be nice to have colors that are very different from each other visually.
I can say that maximum 50 different colors are needed.

Thanks.

snibgo
Posts: 12156
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: how to specify diffferent color in a loop in script

Post by snibgo » 2019-04-21T07:55:03-07:00

manit wrote:I am drawing circles in a script using convert.
I suggest you show the script you have so far.

There are many ways of creating different colours. You might use a simple gradient. Or you could use colours of pixels in a pre-generated 50x1 pixel image.
snibgo's IM pages: im.snibgo.com

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

Re: how to specify diffferent color in a loop in script

Post by fmw42 » 2019-04-21T13:13:30-07:00

Specify colors as HSB and vary the H (in the range 0-360). This gives a 100x100 swatch. The results are rgb colors, but the colors are specified as HSB at full saturation and full brightness. You can also change the brightness and saturation values if you want.

Code: Select all

convert -size 100x100 xc:"hsb(0,255,255)" red.jpg
convert -size 100x100 xc:"hsb(120,255,255)" green.jpg
convert -size 100x100 xc:"hsb(240,255,255)" blue.jpg
see https://imagemagick.org/script/color.php

manit
Posts: 123
Joined: 2009-01-30T22:31:26-07:00

Re: how to specify diffferent color in a loop in script

Post by manit » 2019-05-11T00:48:39-07:00

sorry , wasn't online for long time.
I will try different values of h .
Then I will show the script , it draws circles after getting location from text file.

manit
Posts: 123
Joined: 2009-01-30T22:31:26-07:00

Re: how to specify diffferent color in a loop in script

Post by manit » 2019-06-08T19:37:59-07:00

hi,
here is my python script

Code: Select all

import cv2
import numpy as np
import sys
import os
import subprocess
src_img = cv2.imread(sys.argv[1])
height,width,channel=src_img.shape
file_name_without_ext=sys.argv[1].split('/')[-1].replace('.png','')
xyt_file=sys.argv[2]
cat_xyt_wc = 'cat '+xyt_file+' | wc -l'
lines_in_xyt = subprocess.check_output(cat_xyt_wc, shell=True)
lines_in_xyt=lines_in_xyt.strip('\n')
print('number of points:',lines_in_xyt)
with open(xyt_file) as f:
    lines = f.readlines()
bkup_img='bkup_'+file_name_without_ext+'.png'
os.system('cp '+sys.argv[1]+' '+bkup_img)
hue_increment=240/int(lines_in_xyt)
hue_this_time=0
for i in range(len(lines)):
	x_coord=lines[i].split(' ')[0]
	y_coord=lines[i].split(' ')[1]
	y_coord=str(height-int(y_coord)-1)
	periphery=str(int(y_coord)+5)
	os.system('convert '+bkup_img+' -fill none -strokewidth 2 -stroke "hsb('+str(hue_this_time)+',255,255)" -draw "circle '+x_coord+','+y_coord+' '+x_coord+','+periphery+'"'+' '+bkup_img)
	print('convert '+bkup_img+' -fill none -strokewidth 2 -stroke "hsb('+str(hue_this_time)+',255,255)" -draw "circle '+x_coord+','+y_coord+' '+x_coord+','+periphery+'"'+' '+bkup_img)
	hue_this_time=hue_this_time+hue_increment
print 'minutiae highlighted file is '+bkup_img
os.system('eog '+bkup_img)
It takes png image file as first input and text file as second input.
Text file has x,y coordinate of requires points relative to bottom left corner.
So I am recalculating y coordinate for imagemagick as it considers origin at top left corner . (Is there some way to tell im to consider origin at bottom left ?)
I tried range of hue values from 0 to 240 but colors looked similiar .
Here is an example with text of 20 points and white image of 388x374 dimension.
Text (our script will consider first 2 numbers in each line only)

Code: Select all

147 313 18 8
147 134 39 5
150 185 45 12
151 158 46 8
162 242 97 5
169 140 44 10
173 288 90 7
175 248 98 17
184 126 79 8
189 326 83 11
195 196 97 5
212 330 80 13
216 206 97 9
217 21 13 8
227 155 87 6
237 45 61 7
245 294 85 9
258 203 84 6
282 163 81 9
297 284 37 9
Resultant image - https://i.postimg.cc/sDZKZt2L/bkup-blank-image.png

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

Re: how to specify diffferent color in a loop in script

Post by fmw42 » 2019-06-08T23:21:39-07:00

(Is there some way to tell im to consider origin at bottom left ?)
Not that I am aware. But you can flip the image vertically, then process, then flip vertically again.

manit
Posts: 123
Joined: 2009-01-30T22:31:26-07:00

Re: how to specify diffferent color in a loop in script

Post by manit » 2019-06-09T09:45:38-07:00

fmw42 wrote:
2019-06-08T23:21:39-07:00
(Is there some way to tell im to consider origin at bottom left ?)
Not that I am aware. But you can flip the image vertically, then process, then flip vertically again.
ok .
That can work .
But main question remains that changing hue value results in similiar colors when saturation and brightness are fixed .
see https://i.postimg.cc/sDZKZt2L/bkup-blank-image.png .

snibgo
Posts: 12156
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: how to specify diffferent color in a loop in script

Post by snibgo » 2019-06-13T06:59:59-07:00

manit wrote:But main question remains that changing hue value results in similiar colors when saturation and brightness are fixed .
So, also vary the saturation and brightness. For example:

Code: Select all

magick hald:8 -posterize 4 -unique-colors txt:
This gives 64 colours that are evenly distributed in the RGB colour cube (including black and white).
snibgo's IM pages: im.snibgo.com

manit
Posts: 123
Joined: 2009-01-30T22:31:26-07:00

Re: how to specify diffferent color in a loop in script

Post by manit » 2019-06-14T00:51:44-07:00

I am using imagemagick on 64bit ubuntu OS
$ convert -version
Version: ImageMagick 6.8.9-9 Q16 x86_64 2018-09-28 http://www.imagemagick.org
Copyright: Copyright (C) 1999-2014 ImageMagick Studio LLC
Features: DPC Modules OpenMP
Delegates: bzlib cairo djvu fftw fontconfig freetype jbig jng jpeg lcms lqr ltdl lzma openexr pangocairo png rsvg tiff wmf x xml zlib

There is no magick command here .
Also , does your command give a text output or an image with 64 colors ?

snibgo
Posts: 12156
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: how to specify diffferent color in a loop in script

Post by snibgo » 2019-06-14T03:30:22-07:00

For IM v6, use "convert" instead of "magick".

The command I showed makes a text output. For an image file, change "txt:" to "out.png" or whatever you want.
snibgo's IM pages: im.snibgo.com

manit
Posts: 123
Joined: 2009-01-30T22:31:26-07:00

Re: how to specify diffferent color in a loop in script

Post by manit » 2019-06-23T22:49:14-07:00

yeah.
that works.
I got an image with 1 pixel colors stacked horizontally .
Thanks snibgo .

Post Reply