i have been able to improve the text from a scan by adjusting the brightness and contrast with magick from ImageMagick. Also i reduced by ~10 times the file size by reducing the number of colors and depth. I'd like to know if some improvement can be suggested for making the text edges more sharp. below is my command, and the result.
magick myfile.png -brightness-contrast -20x40 -depth 4 -colors 4 myfile_2.png
Related
convert 0101.jp2 -threshold 50% -type bilevel -monochrome -compress LZW ../0101.tiff
The resulting image looks jagged when I use the above command to convert a colored scanned text page to a black/white image (must be one bit per pixel). I want to make it of a higher resolution to look smoother. How can I use convert to do so?
Note that SO automatically converts tif image to jpg format so the output image shown below is not the same as the output image. You will need to run the convert command to get the true output image in tif.
If instead of thresholding you apply a strong contrast the gray pixels on the edge remain in a range of grays and the output is not jagged.
convert Original.jpg -sigmoidal-contrast 30 Corrected.jpg
(there are several ways to increase contrast in Magick)
I need to generate low resolution black and white images of texts in ImageMagick. These images will be plotted in a small LED matrix. The text need to have 7 pixels of height.
For now, I'm using:
convert -negate -threshold 15% -font Courier -size 80x11 caption:'hello' out.bmp
Output image:
Even with the height being more than I need, due to low resolution and anti-aliasing correction, the letters are not pretty and symmetric. Has anyone did this and can help me out?
Version: ImageMagick 6.8.9-9 Q16 x86_64
The solution I found was to use an TrueType font. Just got a free font from the internet and used it in the size it was built for.
P.S.: Switched for OpenCV as well. My Python app generates images dynamically. The cost for invoking ImageMagick several times(could get close to hundred) per minute is too high.
Posting a snippet, hope it helps.
import cv2 as cv
from PIL import ImageFont, ImageDraw, Image
# Creates a black image with 80x10 size
img = Image.new('RGBA', (80, 10), (0,0,0,0))
draw = ImageDraw.Draw(img)
# Load TrueType font of height size 8
font = ImageFont.truetype("font.ttf", 8)
# Draw text using the loaded font
draw.text((0, 0), "Hello World!", font=font)
img.save("out.bmp")
Output Image:
I would be inclined to output the letters larger than required, then to trim any extraneous spare space so as to make the most of the available resolution, then resize down to your specific needs:
convert -size 320x32 -font Courier label:'hello' -trim +repage -resize 80x8 +write out.gif
Mark, I think he wants a binary result. But you have an excellent idea.
Let's take Mark's result, threshold and then scaling down to 8 pixels tall. This ImageMagick command seems to work better than my earlier post.
Mark's Output:
convert wcwuj.gif -threshold 60% +write thresh.gif -scale x8 result.gif
Threshold Result:
Scaled Result:
Perhaps making Mark's image much larger and choosing a better threshold will produce a better result.
You have not told us what version of ImageMagick nor platform and you do not show your result for us to see what might be wrong. Also your ImageMagick syntax is not proper, though ImageMagick 6 is rather forgiving.
This is what I get using ImageMagick 6.9.10.8 Q16 Mac OSX Sierra. The first output is 8 pixels tall and the second output is scaled by 1000% (10x).
This forum does not seem to convert bmp to a usable format for display, so I am using GIF in place of BMP. But my results look the same whether BMP or GIF
convert -size x8 -font Courier label:'hello' -negate -threshold 20% +write out.gif -scale 1000% out2.gif
I have tried changing threshold, but much larger or smaller values make it worse. A range from about 10-30% produces the same results.
I have also tried using -monochrome in place of -threshold and get the following:
convert -size x8 -font Courier label:'hello' -negate -monochrome +write out3.gif -scale 1000% out4.gif
You might try a dot-matrix type font. See https://www.1001fonts.com/digital+dot-matrix-fonts.html?page=1&items=10. I have not tried any of them.
You could try some of the old X11 fonts. These were hand-drawn rather than being rendered from a set of curves, so they look good at very small sizes.
For example, if I run xfontsel I get things like this (enlarged for clarity):
Take a look in /usr/share/fonts/X11/misc.
im trying to resize image from 72x72 to 512x512 with following command
convert input.png -resize 512x512 output.png
but the output image (output.png) become blur
how to prevent resized images from becoming blurred
how to prevent resized images from becoming blurred
If you want a pixelated image of the original use -sample
# Create small image.
convert -size 72x72 plasma: 72x72.png
# Magnify the image with pixel subsampling.
convert 72x72.png -sample 512 512x512_sample.png
It's true that you can't restore missing data when upscaling images, but there's a lot of various algorithms to calculate what may be missing.
Try using the -filter option in addition to -resize, and checkout the wonderful usage examples here.
Probably the best you can do is use a sharper -filter such as catrom and then do post processing using -unsharp.
convert input.png -filter catrom -resize 512x512 -unsharp 0xSigma output.png
where sigma is the sharpening value, try sigma=1 or 2 (or as desired)
But it will not maintain the same quality as the input as others have mentioned above.
See -unsharp at http://www.imagemagick.org/script/command-line-options.php#unsharp
You can't. For an equal sharpness, you would need more data in the bigger image. Since you have only the data from a small image the result is blurred.
Look at it the other way, if what you asked was possible, instead of compressing the 512x512 image, we would first scale it down to 72x72, compress that (much smaller file) and sent it over with instructions to scale it up to 512x512.
I known some ways to compare 2 images with ImageMagick or OpenCV
Using Objective-C, is there any way to compare two images and get a % difference value returned?
http://www.imagemagick.org/Usage/compare/#methods
How can I quantify difference between two images?
Image comparison - fast algorithm.
But for my case, I also have the same character with different position.
Image1:
Image2:
or
or
So, what should I do now to find the % difference value between Image1 and Image2?
This actually answers your question - which doesn't in fact ask anything about images 3 and 4 - but I fear it will not help you much.
As #GPPK suggests, you need to trim the extraneous material off around your kanji characters, which you can do with the -trim command in ImageMagick. I have added a thin red border so you can see where the edges are:
convert kanji2.png -trim kanji2-t.png
If you want do that to images 1 and 2, and then compare them, you can do that all in one go like this:
convert -metric ae kanji1.png kanji2.png -trim -compare -format "%[distortion]" info:
0
which shows there are zero pixels different in the resulting images if you trim kanji1 and kanji2.
If you compare the trimmed kanji1 and kanji3 like this, you get:
convert -metric AE kanji1.png kanji3.png -trim -compare -format "%[distortion]" info:
893184
which indicates 900,000 pixels of 5,000,000 are different.
Likewise, if you compare kanji1 and kanji4:
convert -metric AE kanji1.png kanji4.png -trim -compare -format "%[distortion]" info:
1.14526e+06
or 1.1 million of 5 million.
But this doesn't help when your images are a different size (scale), or rotated.
You could scale your images to a normalised size before comparing, and I guess that might help you become a bit more "scale invariant":
convert -metric AE kanji1.png kanji4.png -trim -scale 1000x1000! -compare -format "%[distortion]" info:
You could also rotate your images using a little iterative procedure that rotates the images through say +/- 20 degrees and chooses the one with the smallest trimmed bounding box to become a little more "orientation invariant". But then you will still have a problem if the characters are sheared, or fatter, or thinner, or brighter, or darker, or contrastier... I think you need to look into "Template Matching".
What flag in magick.exe convert <flags> will reduce the dimensions of my image?
This is the command I'm using: magick.exe input.png[0] -depth 8 -type Grayscale -dresize 400x300.
The [0] after the source-image filename is meant to strip the first frame of any animated gifs.
How can I do reduce my output gif sizes? My file sizes are too large: my outputs need to be less than 100k. Any methods other than reducing the dimensions are also welcome.
convert test.gif -fuzz 10% -layers Optimize result.gif
Adding a -fuzz 2% produced a better optimization, but still not very good. At -fuzz 15% It isolated the differences for frame optimization to just the visible color band changes I noted before.
At 25% the differences were almost to just the text changes.
Finally at a massive 30% fuzz factor (ignore color changes below that figure, did it optimize to just the text changes.
You can try gifsicle:
gifsicle -O3 old.gif -o new.gif
If it's an animation, you could try skipping frames (see how it works).
magick.exe convert -resize 100x100 .\step1.jpg .\step2.jpg -delay 100 -loop 0 animation.gif
Thanks to Nate.
//IMPORTANT: -resize should come first.
//BY DEFAULT: the aspect ratio will be locked with the longest dimension being set to 100px.
//GENERALIZATION: this order `magick convert 1st<input_file(s)> 2nd<switch(es)> 3rd<output_file>`.
//IMPORTANT-RELATED: `-delay` will not work on second brush! Use `-set delay` for existing files.
Please, also be sure to refresh your folder view to ensure that you're not viewing old output files: Windows Explorer (F5).
Try to use the option:
-type Palette
It might help to reduce your gif file sizes smaller, oh and I believe -depth 8 can only be used for png images.