How to convert png8 in GraphicsMagick - graphicsmagick

I want to convert an input 8-bit PNG image to 8 bit.
I have tried using GraphicsMagick. But I don't know how to convert png8 in GraphicsMagick.
When I use the command as such:
gm convert source.png png8:output.png
it return the message
gm convert: Invalid background palette index (output.png).
My version is GraphicsMagick 1.3.20 2014-08-16 Q8 .

Try using the "-colors 254" option, e.g.,
gm convert source.png -colors 254 png8:output.png
The "png8:" color reduction is somewhat quick-and-dirty and doesn't do nearly as good a job as the slower -colors option, with either GraphicsMagick or ImageMagick.

Related

R Magick Package Image_Write Saves Tiff Images in Gray Instead of RGB

I have a very large number of images that need to be cropped slightly (by 1 pixel either height-wise or width-wise) in order to do further image-processing on them.
I'm trying to use the Magick package in R to do so, but running into an issue where any images that are gray are saved by Magick in grayscale instead of RGB. I see that people have asked similar questions here and here, and I have tried the solutions offered to no avail. For some reason, when doing Image_Write, setting the defines colorspace to auto-grayscale off and type to truecolor does not work and the images are still saved as grayscale instead of RGB format.
Here is the code I'm trying to use:
crop <- function(a,b) {
image <- image_read(a)
cut <- image_crop(image, b)
image_write(cut, path = a, format = "tiff", defines = c('colorspace:auto-grayscale' = 'false', 'type:truecolor' = 'on'))
}
runcrop <- mapply(crop, mydat[,1], mydat[,2])
Where the input (mydat) is a table with two columns: the path to the image and the pixel size I need the image cropped to. Any image taken in black and white (CH4) is saved as a grayscale output, while all the other channels are correctly saved as RGB images after cropping.
Here is a small excerpt from the table:
> mydat
imtocrop WxH
[1,] "./01 10245 XY01_Fused_CH1.tif" "2288x1218+0+0"
[2,] "./01 10245 XY01_Fused_CH2.tif" "2288x1218+0+0"
[3,] "./01 10245 XY01_Fused_CH3.tif" "2288x1218+0+0"
[4,] "./01 10245 XY01_Fused_CH4.tif" "2288x1218+0+0"
[5,] "./01 10245 XY01_Fused_Overlay.tif" "2288x1218+0+0"
And here is an example image to recapitulate the error: https://www.dropbox.com/s/8m93vflnf6rd6ao/01%2010245%20XY01_Fused_CH4.tif?dl=0
The cropping formula I've written works perfectly, but I can't seem to get Image Magick to save these tiffs as RGB instead of grayscale. I've tried both 'false' and 'off' for auto-grayscale, I've tried 'true' and 'on' for truecolor. I've also tried using only the truecolor option, without the colorspace option (as suggested on ImageMagick's documentation site here). Nothing I do seems to convince Image_Write to output as RBG instead of grayscale. Please let me know if you have any ideas that might fix this error, thanks!
I have no knowledge of R, but am trying to move my knowledge of ImageMagick towards your knowledge of R and maybe we an reach the target of sorting out your problem somewhere in the middle...
Let's make a simple 640x480 solid black image with ImageMagick in the Terminal, and pipe it into exiftool in TIFF format to check its depth and whether it is greyscale or colour:
magick -size 640x480 xc:black tif:- | exiftool - | grep -E '^Bits|^Photometric'
Bits Per Sample : 16
Photometric Interpretation : BlackIsZero <- means GREYSCALE
Ok, not surprisingly, our black 640x480 rectangle came out in greyscale with 16-bits per sample because my ImageMagick was compiled with Q16. Let's rectify the 16-bit-edness:
magick -size 640x480 xc:black -depth 8 tif:- | exiftool - | grep -E '^Bits|^Photometric'
Bits Per Sample : 8 <--- that's better, 8-bit
Photometric Interpretation : BlackIsZero <--- still greyscale
Ok, now let's force the black image to become RGB:
magick -size 640x480 xc:black -type truecolor -depth 8 tif:- | exiftool - | grep -E '^Bits|^Photometric'
Bits Per Sample : 8 8 8
Photometric Interpretation : RGB
My hope is that you can find the corresponding settings/switches/options in R Studio now that you know what they look like.
Another option might be to "shell out" with system() and just use the command-line interface:
magick INPUT.TIF -crop WIDTHxHEIGHT+xoffset+yoffset -type truecolor RESULT.TIF
Note that I am forcing TIFF format and piping to exiftool for simplicity of demonstration. If you are testing in your Terminal, you can write straight to a TIFF file with:
magick -size 640x480 xc:black image.tif
You can also set lossless compression with:
magick -size 640x480 xc:black -compress LZW image.tif

Open only part of an image (JPEG/TIFF etc.) in R

I am analysing very large images in R, in the order of tens of thousands of pixels square. Unfortunately, even with 64 GB RAM, these images sometimes fail to fit into memory, and when they do I can only open one at a time, precluding parallelisation.
My current strategy is to load them using the JPEG or TIFF packages. e.g.:
image <- readJPEG('image.jpg')
However, as I am only performing simple mathematical manipulations (summing, thresholding etc.) that could be performed piece-by-piece, is it possible to only open part of an image at a time by specifying the dimensions to load? If so, I could write a loop to open 1024 x 1024 sized tiles. The JPEG and TIFF packages do not offer an option to do this.
If you are working with very large images, libvips is probably your best bet. You can shell out to it from R using system().
Your question is not very specific, but let's make a 10,000x10,000 pixel TIFF with ImageMagick and it is a black-white gradient:
convert -size 10000x10000 gradient: -depth 8 a.tif
Now threshold that at 50% with vips and check memory required:
vips im_thresh a.tif b.tif 128 --vips-leak
memory: high-water mark 292.21 MB
Pretty frugal, no? By comparison, the equivalent ImageMagick command requires 1.6GB of RAM:
/usr/bin/time -l convert a.tif -threshold 50% b.tif
Sample Output
...
1603895296 maximum resident set size
...
How about adding 64 to every pixel using im_gadd which does:
usage: vips im_gadd a in1 b in2 c out
where:
a is of type "double"
in1 is of type "image"
b is of type "double"
in2 is of type "image"
c is of type "double"
out is of type "image"
calculate a*in1 + b*in2 + c = outfile
So we use:
vips im_gadd 1 a.tif 0 b.tif 64 c.tif --vips-leak
memory: high-water mark 584.41 MB
Need to do some statistics?
vips im_stats c.tif
band minimum maximum sum sum^2 mean deviation
all 64 319 1.915e+10 4.20922e+12 191.5 73.6206
1 64 319 1.915e+10 4.20922e+12 191.5 73.6206
As it turns out, there is an R package - RBioFormats - that allows you to specify part of an image being opened (though it is not available on CRAN). It can be installed from Git as follows:
source("https://bioconductor.org/biocLite.R")
biocLite("aoles/RBioFormats") # You might need to first run `install.packages("devtools")`
library(RBioFormats)
The dimensions of the image can be read from the metadata without having to open the image:
metadata <- read.metadata('image.tiff')
xdim <- metadata#.Data[[1]]$sizeX
ydim <- metadata#.Data[[1]]$sizeY
Suppose that we want to load the top-left 512 x 512 pixels, we use the subset function:
image <- read.image('image.tiff', subset = list(X = 1:512, y = 1:512))
From this it is trivial to write a loop to iteratively process a whole large image. RBioFormats is an R interface into the Java BioFormats library and will open Tiffs, PNGs, JPEGs as well as many proprietary imaging formats.

Image to pencilsketch using graphicsmagick

I am new to imageprocessing. I want to convert an image to pencilsketch. I want to know how can i achieve the same with Graphicsmagick. Thanks in Advance.
It would be somewhat easier with ImageMagick than GraphicsMagick, as Anthony Thyssen has put together some excellent examples - but they rely on ImageMagick capabilities such as "aside-processing" in parentheses which GraphicsMagick lacks. They are here.
However, you can do fairly simplistic stuff with GraphicsMagick. Let's start with this picture of the White House:
gm convert whitehouse.jpg -edge 1 -negate -normalize -colorspace Gray -blur 0x0.5 sketch.png

Output file numbering in graphicsmagick

I'm trying to convert pdf to images using this command:
gm convert ./file.pdf -scene 1 thumbs/thumb%02d.jpg
Although I specify -scene argument, it does nothing, as I get output files starting from thumb00.jpg. And I need them to start from thumb01.jpg.
I'm using GraphicsMagick 1.3.12.
What am I doing wrong here?
In order to ensure numbered output files, add the +adjoin option like:
gm convert ./file.pdf -scene 1 +adjoin thumbs/thumb%02d.jpg
This additional requirement was added by GraphicsMagick 1.3.15. It is ok to use the same option for all earlier releases.
There is still an inability to specify the starting scene number. This is a known bug.

Convert command for EPS to JPG

I want to know how to use convert command to convert EPS img to JPG.
I dont have Linux M/c but I am using Cygwin. I have searched, but nothing is working. I always get invalid argument error.
$ convert "/cygdrive/e/pdf/B313.eps" "/cygdrive/e/macro/B313.JPG"
Invalid Parameter - /e
It will be great if could solve this problem.
I cannot test it for cygwin but the following is working under unix:
convert -density 50 -antialias -colors 128 -background white -normalize -units PixelsPerInch -quality 100 /path/to/eps/test.eps test.jpg
Maybe you have to use backslashes for the file path under windows
$ convert "\cygdrive\e\pdf\B313.eps" "\cygdrive\e\macro\B313.JPG"
And try it with absolute paths like c:\path\ ...
Keep in mind that there's another convert command under windows which converts between file systems. Maybe the wrong one is being invoked.

Resources