I'm writing a script to resize gif images. I've notice that the images I create have a smaller image size, but take up more room on disk. I think I've tracked down the cause of the problem. The original images when examined in photoshop only have 4 colors in their color table (white, gray, grayer, black) while the new images have 256 colors in their color table. I can't find a way to copy the color table from the incoming image, is there some way to control this? Here's my script
use Image::Magick;
my $image = Image::Magick->new;
$image -> Read( 'test.gif' );
$image -> Resize(geometry=>"50%");
$image->Write( 'test-out.gif' );
Any advice much appreciated.
You can use Sample() instead of Resize() to keep the same number of colours
$image->Sample(geometry=>"50%");
You can limit the number of output colors using the -colors option. It's a good idea to include the +dither option, even if you're sure you have the same number of colors in both input and output. In straight ImageMagick, it would look like this:
convert test.gif +dither -colors 4 test-out.gif
I don't know the syntax for the perlMagick API, but this should get you pretty close.
I am currently working on a automation script to convert imgaes for use on websites. Every image gets a white and black border and is combined with a prepared alpha channel image. The black border (outer border) combined with the alpha channel will look like a shadow when everything is done.
I've got the following files:
test.jpg (the test image)
test.tga (the alpha channel)
And I convert the input image in 2 steps:
convert test.jpg -bordercolor #FFFFFF -border 15 -bordercolor #000000 -border 30 test.png
(adds the white and black border to the image and saves as 'test.png')
composite -compose CopyOpacity test.tga test.png test2.png
(joins image file with alpha channel from test.tga and saves as test2.png)
So now my question is: Is there any way to invoke both steps as one command? I know that convert also supports the -compose options, if I got that right from the manual, but I could not get it working. It would be best, if I could edit and join the image with the alpha channel with one command. (Command will be executed from my Application and I don't want to execute many sub-processes)
Maybe someone knows a solution for this.
Thanks in advance!
Am trying to extract data from reciepts and bills using Tessaract , am using tesseract 3.02 version .
am using only english data , Still the output accuracy is about 60%.
Is there any trained data available which i just replace in tessdata folder
This is the image nicky provided as a "typical example file":
Looking at it I'd clearly say: "Forget it, nicky! You cannot train Tesseract to recognize 100% of text from this type of image!"
However, you could train yourself to make better photos with your iPhone 3GS (that's the device which was used for the example pictures) from such type of receipts. Here are a few tips:
Don't use a dark background. Use white instead.
Don't let the receipt paper crumble. Straighten it out.
Don't place the receipt loosely on an uneven underground. Fix it to a flat surface:
Either place it on a white sheet of paper and put a glas platen over it.
Or use some glue and glue it flat on a white sheet of paper without any bend-up edges or corners.
Don't use a low resolution like just 640x480 pixels (as the example picture has). Use a higher one, such as 1280x960 pixels instead.
Don't use standard exposure. Set the camera to use extremely high contrast. You want the letters to be black and the white background to be really white (you don't need the grays in the picture...)
Try to make it so that any character of a 10-12 pt font uses about 24-30 pixels in height (that is, make the image to be about 300 dpi for 100% zoom).
That said, something like the following ImageMagick command will probably increase Tesseract's recognition rate by some degree:
convert \
http://i.stack.imgur.com/q3Ad4.jpg \
-colorspace gray \
-rotate 90 \
-crop 260x540+110+75 +repage \
-scale 166% \
-normalize \
-colors 32 \
out1.png
It produces the following output:
You could even add something like -threshold 30% as the last commandline option to above command to get this:
(You should play a bit with some variations to the 30% value to tweak the result... I don't have the time for this.)
Taking accurate info from a receipt is not impossible with tesseract. You will need to add image filters and some other tools such as OpenCV, NumPy ImageMagick alongside Tesseract. There was a presentation at PyCon 2013 by Franck Chastagnol where he describes how his company did it.
Here is the link:
http://pyvideo.org/video/1702/building-an-image-processing-pipeline-with-python
You can get a much cleaner post-processed image before using Tesseract to OCR the text. Try using the Background Surface Thresholding (BST) technique rather than other simple thresholding methods. You can find a white paper on the subject here.
There is an implementation of BST for OpenCV that works pretty well https://stackoverflow.com/a/22127181/3475075
i needed exactly the same thing and i tried some image optimisations to improve the output
you can find my experiment with tessaract here
https://github.com/aryansbtloe/ExperimentWithTesseract
I'm working with this excellent example of converting an image to grayscale: Convert Image to B&W problem CGContext - iPhone Dev
However, for my purposes, I would like to have only pure black and pure white left in the image.
It appears that to do so, I need to pass a black and white color space to the recolor method using a call:
CGColorSpaceRef colorSpace = CGColorSpaceCreateWithName(/*black and white name*/);
However, I was unable to find the proper iOS color space names. What I found was from Mac, and the "color space names" referenced from the iOS docs does not point anywhere.
How can I properly create a black and white CGColorSpaceRef?
Thank you!
I am not familiar with a black and white only color space but what you can do is calculate the total average RGB value from all the pixels (lets call it totalAvg) and use it as a threshold. Meaning for each pixel if its rgb average is greater than the calculated totalAvg than set it to pure white, otherwise set it to pure black.
I agree it is a bit of more work but thats whay I can think of unless you find the colorspace you are looking for.
You might try creating a gray color space, then creating an indexed color space with two colors (black and white, obviously) and using that.
I have a PNG image with an alpha channel (i.e. transparency), and I need to create versions with the image layer composed onto a white background. I want to use a scriptable command using a CLI tool such as Image Magick to directly convert PNG to PNG losslessly.
An example of a non-working Image Magick command which results in an error is:
convert input.png -background white -flatten output.png
-background white -alpha remove -alpha off
Example:
convert image.png -background white -alpha remove -alpha off white.png
Feel free to replace white with any other color you want. Imagemagick documentation says this about the -alpha remove operation:
This operation is simple and fast, and does the job without needing
any extra memory use, or other side effects that may be associated
with alternative transparency removal techniques. It is thus the
preferred way of removing image transparency.
This works for me:
convert -flatten img1.png img1-white.png
Documentation references:
-flatten command-line option
-layers command-line option (-flatten is equivalent to -layers flatten)
Flattening image and applying background image is straight forward in ImageMagick
However, order of the commands is very important
To apply any background on a transparent image and flatten it, first apply the background than flatten it. The reverse doesn't work.
$ convert sourceimage.png -background BackgroundColor -flatten destinationimage.png
The only one that worked for me was a mix of all the answers:
convert in.png -background white -alpha remove -flatten -alpha off out.png
here's how to replace the same image in all folders in a directory with white instead of transparent:
mogrify -background white -flatten */*.png
Using -flatten made me completely mad because -flatten in combination with mogrify crop and resizing simply doesn't work. The official and for me only correct way is to "remove" the alpha channel.
-alpha remove -alpha off (not needed with JPG)
See documention: http://www.imagemagick.org/Usage/masking/#remove
The Alpha Remove section of the ImageMagick Usage Guide suggests using the -alpha remove option, e.g.:
convert in.png -background white -alpha remove out.png
...using the -background color of your choosing.
The guide states:
This operation is simple and fast, and does the job without needing any extra memory use, or other side effects that may be associated with alternative transparency removal techniques. It is thus the prefered way of removing image transparency.
It additionally adds the note:
Note that while transparency is 'removed' the alpha channel will remain turned on, but will now be fully-opaque. If you no longer need the alpha channel you can then use Alpha Off to disable it.
Thus, if you do not need the alpha channel you can make your output image size smaller by adding the -alpha off option, e.g:
convert in.png -background white -alpha remove -alpha off out.png
There are more details on other, often-used techniques for removing transparency described in the Removing Transparency from Images section.
Included in that section is mention of an important caveat to the usage of -flatten as a technique for removing transparency:
However this will not work with "mogrify" or with a sequence of multiple images, basically because the "-flatten" operator is really designed to merge multiple images into a single image.
So, if you are converting several images at once, e.g. generating thumbnails from a PDF file, -flatten will not do what you want (it will flatten all images for all pages into one image). On the other hand, using the -alpha remove technique will still produce multiple images, each one having transparency removed.
It appears that your command is correct so the problem might be due to missing support for PNG (). You can check with convert -list configure or just try the following:
sudo yum install libpng libpng-devel
This is not exactly the answer to your question, but I found your question while trying to figure out how to remove the alpha channel, so I decided to add this answer here:
If you want to remove alpha channel using imagemagick, you can use this command:
mogrify -alpha off ./*.png
Welp it looks like my decision to install "graphics magick" over "image magick" has some rough edges - when I reinstall genuine crufty old "image magick", then the above command works perfectly well.
edit, a long time later — One of these days I'll check to see if "graphics magick" has fixed this issue.
I needed either: both -alpha background and -flatten, or -fill.
I made a new PNG with a transparent background and a red dot in the middle.
convert image.png -background green -alpha off green.png failed: it produced an image with black background
convert image.png -background green -alpha background -flatten green.png produced an image with the correct green background.
Of course, with another file that I renamed image.png, it failed to do anything. For that file, I found that the color of the transparent pixels was "#d5d5d5" so I filled that color with green:
convert image.png -fill green -opaque "#d5d5d5" green.png replaced the transparent pixels with the correct green.
I saw this question and answers which really help me but then I was needed to do it for a lot of files, So in case you have multiple images (PNG images) in one folder and you want to do it for all:
find ./ -name "*.png" -exec convert {} -flatten {} \;
this creates an image just placing the 1st with transparency on top of the 2nd
composite -gravity center ImgWithTransp.png BackgroundSameSizeOfImg.png ResultImg.png
originally found the tip on this post
To actually remove the alpha channel from the file, use the alpha off option:
convert in.png -background white -alpha off out.png
Tried all, none worked. This one did:
convert input.png -channel rgba -alpha set \
-fill none -opaque white \
-fill white -opaque black \
-fill white -opaque none \
-alpha off output.png
It's -alpha off, NOT -alpha remove! iOS app store upload fails when there is an alpha channel in any icon!!
Here's how to do it:
mogrify -alpha off *.png
This does the job for me:
magick convert OLD.png -background white -alpha remove NEW.png
Here is a starter image with a transparent background in case it helps with testing:
Also, for one-offs on PC, you can always open the PNG file in Windows Paint and click Save. This will automatically turn the transparency to opaque white.