how can i add an alpha channel to this image using ImageMagick - png

I tried adding an alpha channel to this image using this command
convert az.png -alpha set temp.png
The resulting temp.png still has no alpha channel. What am I doing wrong?
The image was obtained from http://flagpedia.net/data/flags/mini/az.png

By default ImageMagick will "optimize" the output, removing the alpha channel if it's all-opaque, and perhaps converting it to indexed format if fewer than 256 colors are present. You can force ImageMagick to retain the alpha channel by using "png32:" prefix on the output filename:
convert az.png png32:temp.png
or with the current ImageMagick release version 7
magick az.png png32:temp.png
resulting in
pngcheck *.png
OK: az.png (40x20, 24-bit RGB, non-interlaced, 92.7%).
OK: temp.png (40x20, 32-bit RGB+alpha, non-interlaced, 87.0%).

Related

Proper syntax for STDIN/STDOUT in imagemagick

I downloaded ImageMagick so I could batch process folders of images at work, cropping them to size and then slicing them into 12 equal pieces. I can do each task individually, but I'd rather execute it all in one line using STDIN and STDOUT. However, even after looking through answers here and the documentation on the website, I'm not any closer to making it work.
I'm working in Windows Powershell. Here's what I've tried, working with a single image:
convert -crop '5072x3552+87+0' image.jpg jpg:- | convert -crop '1268x1184' - jpg: - | convert -crop '1268x1030+0+0' - C:\folder1\folder2\folder3\square.jpg
This gives me a series of errors:
convert.exe: no decode delegate for this image format `' #
error/constitute.c/ReadImage/508.
convert.exe: no images defined `jpg:-' #
error/convert.c/ConvertImageCommand/3253.
convert.exe: no decode delegate for this image format `' #
error/constitute.c/ReadImage/508.
convert.exe: no images defined
`C:\users\lmcane\desktop\imagem\test\leaf.jpg' #
error/convert.c/ConvertImageCommand/3253
I'm using ImageMagick 7.0.2 on Windows 7. Windows PowerShell is opened as administrator.
Additional guidance on formatting STDIN and STDOUT in imagemagick would be welcome.
Any help appreciated.
image for reference
process for single original image
I would try doing the first trim of the edges using -fuzz (to allow for small differences in the border colour) and -trim. That gets you this:
convert grid.png -fuzz 30% -trim trimmed.png
Then, I would tile to 4x3 and repage the images so they forget their positions in the original image:
convert trimmed.png -crop 4x3# +repage step2.png
Now crop off the tops, bottoms and sides and save the individual 12 frames:
convert step2.png -crop 130x100+20+20 f-%02d.png
Now you can trim the excess, using a slightly different fuzz as there seems to be less variation here than in the outer edge of the original image. And, I have also put all the commands together in one that does all the steps for all 12 images in one go:
convert grid.png -fuzz 30% -trim -crop 4x3# +repage -crop 130x100+20+20 -fuzz 5% -trim f-%02d.png
You may have to play with the fuzz factor for other images, but you should see how it works now.
I can't explain why convert throws these errors, but I was able to get it to work by using xwd as the transport format:
convert -crop '5072x3552+87+0' image.jpg xwd:- |
convert -crop '1268x1184' xwd:- xwd:- |
convert -crop '1268x1030+0+0' xwd:- C:\folder1\folder2\folder3\square.jpg

JPEG to PNG conversion with 300 DPI

Unable to convert a JPEG image into a 300 DPI PNG image using ImageMagick.
After conversion the PNG image is 72 DPI only. I'm using ImageMagick 6.9.0-0 Q16 x86 and Ghostscript v9.15.
Below is the line I use in my Perl script:
system("\"$imagemagick\" -set units PixelsPerInch -density 300 \"$jpg\" \"$png\"");
Adjusting the units & density will not alter the underlining image data, but updates meta info for rendering libraries. Important for vector to raster, but not very useful for raster to raster. To adjust the DPI of an image, use the -resample operation.
convert source.jpg -resample 300 out.png
You verify the DPI resolution with the following...
identify -format "%[resolution.x] %[resolution.y]\n" out.png
I'm wondering where the 72dpi is coming from. Assuming you are using X and some kind of Unix, ImageMagick defaults to using the screen resolution (72 dpi). I'm not sure what it does under OSX/XQuartz but it's likely similar. Is your screen resolution set to 72dpi (!?).
I'm with #emcconville #ikegami - just do this straight from ImageMagick on the commandline - passing the right options to be sure.
There are image manipulation modules that you can use from perl without having to resort to system commands as well such as Imager::Transformations, Image::Magick, and GD. Here's how to convert with GD.
perl -MGD -E 'my $imgjpg = GD::Image->newFromJpeg("img.jpg");
open my $imgpng, ">", "img.png" or die; print $imgpng $imgjpg->png();'
With most image manipulation packages the original resolution show be maintained during conversion - though some (including GD) will default to lower color depths (8 bit) unless passed a Truecolor flag.
e.g. GD::Image->newFromJpeg("img.jpg", 1);

Tesseract and tiff format - spp not in set {1,3}

While trying to run this command:
tesseract bond111.tif bond111 batch.nochop makebox
I get the next error
Error in pixReadFromTiffStream: spp not in set {1,3}
Error in pixReadStreamTiff: pix not read
Error in pixReadTiff: pix not read
Assuming that spp not in set is the main error here, what does it mean?
At first it had trouble because the bpp was higher than 24 so I reduced it using Gimp but that did not resolve the issue.
It probably means your TIFF image has an alpha channel and therefore the underlying Leptonica library used by Tesseract doesn't support it. If you're using Imagemagick then be aware that operations such as -draw can cause alpha channels to be added. If you're using convert in your workflow and want to remove the channel again immediately, flatten the image before writing by adding -background white -flatten +matte before the output filename, e.g.:
convert input.tiff -fill white -draw 'rectangle 10,10 20,20' -background white -flatten +matte output.tiff
Tesseract (well, Leptonica) accepts PNGs these days and is less picky about them, so it might be easier to migrate your workflow to PNG anyway.
Sources: magick-users mailing list posting; tesseract-ocr mailing list posting
Thanks for your post ZakW, you pointed me to the right direction.
Anyhow i also needed to set '-depth 8'. Quality was not good enough for OCR, whatever I tried.
What worked for me is this solution:
ghostscript -o document.tiff -sDEVICE=tiffgray -r720x720 -g6120x7920 -sCompression=lzw document.pdf
tesseract document.tiff document -l deu
vim document.txt
This way I got perfect text with Umlauts in german.
Adjusting the conversion to the following line did help me.
convert -density 300 input.pdf -depth 8 -background white -alpha Off output.tiff
Note that the other answers did not work for me since they use the deprecated +matte flag instead of -alpha Off.
You can try using the command 'tiffinfo' provided by libtiff_tools to verify the TIFF format of your src image. A number of TIFF formats exist, with different values for Bits-per-pixel (bpp) and Samples-per-pixel (spp).
Error in pixReadFromTiffStream: spp not in set {1,3,4}
An 'spp' value of 2 is invalid for TIFF.
I solved the problem by saving directly to TIFF format from Gimp, instead of converting from .png to .tif using ImageMagick's 'convert'.
See also: TIFF format

How to change background color of an eps file while converting it to jpeg or png

I am converting eps (Encapsulated PostScript) files to jpeg files with ghostscript. A sample command I use is:
gswin32.exe -sDEVICE=jpeg -dJPEGQ=100 -dNOPAUSE -dBATCH -dSAFER -r600x600 -dGraphicsAlphaBits=4 -dUseCIEColor -dEPSCrop -sOutputFile=”a.jpeg” b.eps
The input eps files come with white backgrounds (I only have their clipping path). What I need to do is change this white background to another color in the output images, or it would be even better if I could make them transparent (output file format would be png). How can I do this?
never tried it myself but you should be able to convert your eps file into png by setting:
-sDEVICE=pngalpha
also the pngalpha device has a -dBackgroundColor option:
-dBackgroundColor=16#RRGGBB (RGB color, default white = 16#ffffff) For
the pngalpha device only, set the
suggested background color in the PNG
bKGD chunk. When a program reading a
PNG file does not support alpha
transparency, the PNG library converts
the image using either a background
color if supplied by the program or
the bKGD chunk. One common web browser
has this problem, so when using on a web page you
would need to use
-dBackgroundColor=16#CCCC00 when creating alpha transparent PNG images
for use on the page.
more details here: Details of Ghostscript output devices see section 3.1. PNG file format
After you've obtained your (white background) images from Ghostscript, you could use ImageMagick's convert or GraphicMagick's gm convert commands to change the white to transparent background:
convert -background transparent my.png my_transp.png

Alpha channel in OpenCV

does OpenCV support alpha-channel? Or is there any way to work with transparent png? I need to merge two images, where the first one is background and the second one is image which was rotated by cvWarpAffine. I can do this by merging pixels one by one and omit pixels with some value, which I set in cvScalar in cvWarpAffine. However, I don't think that this is intended solution.
Thanks for suggestions
Updated answer: Use CV_LOAD_IMAGE_UNCHANGED flag to load all four channels (including Alpha) from the image. Then, use mixChannels() and/or split() to separate the alpha channel from others, and threshold on it, as explained below.
Very old answer:
OpenCV does not support alpha channel, only masking. If you want to read in PNG with alpha, use imagemagick first to extract alpha channel:
convert input.png -channel Alpha -negate -separate input-mask.png
Then in OpenCV you can do sth like this:
Mat_<float> mask = imread("input-mask.png", 0);
threshold(mask, mask, 254., 1., THRESH_BINARY);
... to get a real mask (usable as mask matrix in OpenCV operations). Or you can use it in your own operations without the thresholding. To apply the mask it may also be a good idea to extend it to three channels:
std::vector<Mat> marr(3, mask);
Mat_<Vec<float, 3> > maskRGB;
merge(marr, maskRGB);
After that you can test it like this:
imshow("Target", target);
imshow("Mask", mask*255);
imshow("Applied", target.mul(maskRGB));
waitKey();
Note: This is OpenCV 2.0 code.
Here is a bash script that I threw together that will perform the ImageMagick conversion given by ypnos on all of the png files in a directory. You can make it recursive by replacing the * in the third line with a find command.
#!/bin/bash
for file in *
do
if [[ $file =~ (.+)-mask\.png ]]; then
echo "Ignoring mask $file"
elif [[ $file =~ (.+)\.png ]]; then
echo "Generating mask for $file"
basefn=${BASH_REMATCH[1]}
convert "$basefn.png" -channel Alpha -negate -separate "$basefn-mask.png"
fi
done