Specify a pixel in image magick - imagemagick

Where can I find documentation for this?
I am trying this option:
-format "%[pixel: u.p{1,1}]"
And it gets the pixel 1,1.
How could I get pixel at 100% of the image, I tried:
-format "%[pixel: u.p{100%,100%}]"
-format "%[pixel: u.p{-1,-1}]"
I was reading this, but this is difficult to understand as pixel entry says:
p = pixel to use (absolute or relative to current pixel)
Context
The complete command I am using is:
color=`convert ~/Desktop/my_image.png -format "%[pixel: u.p{-1,-1}]" info:`
And I am trying to get the color in the last pixel of the image.

By looking at the documentation i suggest you try u.p{w-1,h-1}

Related

Get specific channel depth with ImageMagick

Is it possible to get the bit depth of a specific channel with an ImageMagick command?
I can see the individual channel depths with convert :rose -verbose info:, but I was wondering if there was a trick to only getting to print the alpha depth.
The reason I'm doing this is because I'm trying to write a script to figure out if an image either:
Does not support transparency (like JPEG)
Only supports ON/OFF transparency (like GIF)
Supports >1 bit transparency (like PNG)
I can check if the image is fully opaque with identify %[opaque] :rose, and was thinking I could check the type of transparency by checking if the alpha bit depth is greater than 1
In Imagemagick to get the bit depth of the alpha channel, try
convert transparent_image -alpha extract -format "%z\n" info:
or
convert transparent_image -alpha extract -format "%[depth]\n" info:
See http://www.imagemagick.org/script/escape.php
For example:
convert logo: -transparent white -alpha extract -format "%z" info:
8
However, this may actually be binary, but listed as 8-bits.
So you might want to look at the histogram or unique-colors to see how many actual colors exist.
convert logo: -transparent white -alpha extract -format "%[colors]\n" info:
2
Through some agonizing searching and trial and error, I may have figured it out. If I run
convert rose: -channel A -separate -format %[fx:z] info:-
I get the number of bits in the alpha channel. For PNGs it seems to print 8, for JPEGs and GIFs it prints 1.

Convert rgb to png with imagemagik

I am trying to convert some image (raw) that has no header, just the pixel values to a png, so I can view it. I found some information here (using imageMagik) and it works if the image is one channel. I used the command
convert -depth 8 -size 5312x2988+0 gray:image.raw pic.png
I searched more and I found that using the rpg is a way to treat more channels image, so I changed the syntax to
convert -depth 8 -size 5312x2988+0 rgb:image.raw pic.png
... but the output seems to be more like a 9x9 matrix containing the small images, like
R R R
G G G
B B B
The size is not wrong, but it may be the way the pixels are stored (interlaced/not-interlaced).
Can anyone help me to convert the 3-channel image, the correct way?
You could try specifying -interlace plane before the input file. Or maybe -interlace line, like this:
convert -interlace plane -depth 8 -size 5312x2988+0 rgb:image.raw pic.png
Like many ImageMagick parameters, you can enumerate the options at the command line with identify -list OPTION, so, in this case:
identify -list interlace
Output
Line
None
Plane
Partition
GIF
JPEG
PNG

get the most representative colour from the uppermost pixel of a picture (the sky colour)

Using any language readily available under a UNIX system, I'd like to get the the representative RGB colour values for the uppermost pixels of a JPG file.
In other words, given an image file, I'd like to -crop3072x1, resize to 1x1 (obviously without preserving the aspect ratio), then get the RGB value of the resulting pixel.
Using GraphicsMagick, there's apparently a TXT image format that's available:
gm convert IMG_X.JPG -crop x1 -resize 1x1\! txt:-
For example:
% gm convert IMG_7753.JPG -crop 2800x1 -resize 1x1\! txt:-
0,0: ( 63,107,158) #3F6B9E
References:
http://www.graphicsmagick.org/convert.html
http://www.graphicsmagick.org/GraphicsMagick.html#details-crop
http://www.graphicsmagick.org/GraphicsMagick.html#details-resize
http://www.graphicsmagick.org/GraphicsMagick.html#details-geometry

How to find average color of an image with ImageMagick?

How do I get the RGB values of the average color of an image, where each value is 0-255? Such as "255,255,255"
I run this command to shrink the image down and it returns the 'rgba' value with the alpha channel and sometimes it gives text color names:
convert cat.png -resize 1x1\! -format "%[pixel:u]\n" info:
Output:
rgba(155,51,127,0.266087)
There are two aspects to my answer:
Resize the original image to a 1-pixel-image. This pixel then will have the "average" color as ImageMagick's convert sees it.
Output the result as the special .txt format supported by convert. This text format enumerates all pixels of an image, giving first its coordinates ($row,$column:), then its RGB or CMYK values in different formats.
Here is a command which covers both aspects in one:
convert cat.png -resize 1x1 out.txt
cat out.txt
To get the output directly in the terminal window, you could use:
convert cat.png -resize 1x1 txt:-
Example output:
convert p4.png -resize 1x1 txt:-
# ImageMagick pixel enumeration: 1,1,255,srgb
0,0: (189,185,184) #BDB9B8 srgb(189,185,184)
You can do the following to parse out just the comma-separated RGB values. It also will not return text color names.
convert cat.png -resize 1x1\! \
-format "%[fx:int(255*r+.5)],%[fx:int(255*g+.5)],%[fx:int(255*b+.5)]" info:-
Output format should look like:
155,51,127
This should work in ImageMagick 6.3.9.1+
The C# way, using the NuGet Magick.Net package(s) and borrowing from the command line examples.
ImageMagick.IMagickColor<ushort> color;
using (ImageMagick.MagickImage image = new ImageMagick.MagickImage(file))
{
// Get average color
image.Resize(1, 1);
color = image.GetPixels().First().ToColor();
}

Autolevels over an entire image while only sampling from a smaller area

I have some poorly scanned images (around 0.25M) that I need to adjust automatically. The outside edges (about 20% of the area) are throwing off the auto levels commands.
Is there a way to calculate the image histogram from a subset of the image data while executing autolevels over the entire area?
eg:
convert **-samplearea [box]** -autolevels infile.png
Imagemagick's -autolevels command will calculate the min/max of the image, and passes it to the -level command. Calculating what the autolevel values would be from an image's region would be just a -crop, -format, and info:.
convert source.png -crop $geometry -format "%[min],%[max]" info:
Where $geometry is defined.
So if I wanted apply level correction in respect to the area in the top-left corner of the image, my commands would read:
# Grab the area to analyze (top-left, but not on edge)
geometry="50x50+5+5"
# Grab values
levels=$(convert source.jpeg -crop $geometry -format "%[min],%[max]" info:)
# Apply to whole image
convert source.jpeg -level $levels out.jpeg

Resources