ImageMagick to preprocess image for tesseract-ocr - image-processing

Is there anyway to process an image like this with ImageMagick so that I can use tesseract-ocr to convert it to text?
Because of the lines in the background I get nonsense from conventional methods. Does anyone know how to deal with an image such as this?
'convert -density 300 -units PixelsPerInch -type Grayscale +compress input.png input.tif' followed by 'tesseract input.tif output -l eng' gives me utter garbage.
Or are there any alternatives to ImageMagick that I can use to pre-process such an image whether through command-line or in python?

Have you tried morphology operations Morphology of Shapes after converting image to grayscale?

Related

Can the `cwebp` tool convert a specific color to transparent pixels in its output?

I have some source images which have a black background, and I would like to convert them into WebP images that have a transparent background.
I don't understand the talk about alpha channels from their documentation, so I am unsure if this is even possible with cwebp. I tried some guesswork with the arguments, but none worked.
The command I use for direct conversion from JPG to WebP is:
cwebp ./input.jpg -o ./output.webp
What would I need to add to this in order to get the black background from the input JPG to be transparent in the output WebP?
I think cwebp's options are mostly focused on compression with some limited other options for manipulating the image like cropping and sharpness.
To accomplish this task I would recommend image magick which is a general purpose image manipulation tool. that can remove the transparency, and then you can send that to cwebp.
imagick convert image.jpg -fuzz 2% -transparent black image.png
cwebp image.png -o image.webp

Magick equivalent of GIMP Hue-Chroma transformation

Using Gimp, given an input image, I can improve its contrast using Colors > Hue Chroma... by setting Chroma=50 (in a scale between -100 and 100) and leaving Hue=0 and Lightness=0. So it appears I'm doing an HCL transformation.
Is there an equivalent command for Magick?
The following image shows the GIMP effect:
Image
Updated Answer
Not sure about this at all. I think you can get pretty close with -modulate if you go into an LCH colourspace, but I have no idea if it will work consistently. I got:
magick cXDv3.jpg -define modulate:colorspace=LCH -modulate 100,150 result.jpg
If that doesn't work, or is not to your liking, read on...
Generic Method for any GIMP filters
The method below should allow you to replicate any GIMP filter with ImageMagick - as long as it is a pure "point process", I mean one where each pixel's output value is purely derived from its input value and not an "area process" where surrounding pixels contribute - such as blurring or median filtering, for example.
It's called a HALD-CLUT. You would create a HALD-CLUT something like this:
magick hald:16 clut.png
Then take that file (clut.png) into GIMP and apply your GIMP processing on it and save the result as GIMP-H0-C50-L0.png so we know how GIMP affects each colour. You do that just once.
Then you go back to ImageMagick and apply that CLUT to your image:
magick input.png GIMP-H0-C50-L0.png -hald-clut result.png
That gives me this:
and I think you'll agree the left side looks pretty similar to the right side of your input image.
Original Answer
I don't know what that command does in GIMP, but you can convert to HCL colourspace in ImageMagick and select the Chroma channel for modification like this:
magick INPUT.PNG -colorspace HCL -channel G ...
You then want to do something ? to affect the Chroma channel, so try -auto-level for now, and then return to sRGB colourspace and save:
magick INPUT.PNG -colorspace HCL -channel G -auto-level +channel -colorspace sRGB RESULT.PNG
Then you need to provide more clues or experiment more with what that command does in GIMP - or provide examples.

How to get raw binary data of an image or others files?

I want to get the raw binary data of an image but I don't know how to do that. Is any module available there to do that? I need some help. If anyone knows the solution to this help me.
The easiest way is probably just with ImageMagick in your Terminal. Say you have a JPEG and want the RGB pixel values in binary without headers or anything:
magick INPUT.JPG -depth 8 RGB:output.bin
Or you have a PNG and want RGBA data:
magick INPUT.PNG -depth 8 RGBA:output.bin

How do I convert EXR to PNG and adjust brightness at the same time

I was able to convert my EXR image to a PNG using the techniques outlined in Image conversion from IFF and EXR formats to JPEG format .
convert 0007.exr /tmp/0007.png
Unfortunately the PNG looks quite dim.
What should I add to the imagemagick convert command line to increase the brightness?
Starting with this:
You could try -auto-gamma:
convert start.jpg -auto-gamma result.jpg
If the -auto-gamma overcooks the image for your liking, you could apply a percentage of it. So, here I clone the original image and apply auto-gamma to the clone but then only blend 80% back into the original because I feel auto-gamma overdoes it:
convert start.jpg \( +clone -auto-gamma \) \
-define compose:args=80 -compose blend -composite result.jpg
Or, another option, you could experiment with your particular images and maybe try using -modulate for the brightness, where 100% means "do nothing", so numbers over 100 increase the brightness:
convert start.jpg -define modulate:colorspace=LCHuv -modulate 160 result.jpg
You can try -auto-level, which will take the minimal value and the maximal value of your picture and then stretches the values to the full range of values:
convert input.exr -auto-level output.jpg
Note that if you picture was too bright and this does not help, then it might be that your image is stored with 32 Bit, while ImageMagick is working with 16 Bit and no HDRI support. 32 Bit input is supported if convert --version
either show Q32 as part of the version string or lists HDRI under features.
Depending on your operating system you might be able to install another variant of ImageMagick. For example, for Debian Buster we can use sudo apt list imagemagick* to see that the package imagemagick-6.q16hdri is available. Installing this package provides convert-im6.q16hdri, which allows reading 32 Bit EXR images.
EXR is in linear RGB colorspace. You want to convert it to non-linear sRGB colorspace in Imagemagick as:
Input:
convert image.exr -set colorspace RGB -colorspace sRGB output.png

Crop and scale SVG to PNG using ImageMagick, without pixellation

I have an SVG with many polygons:
https://github.com/barrycarter/bcapps/blob/master/sample-data/current-temps.svg
that looks somewhat like this:
Cropping this and converting to PNG works fine:
convert -crop 100x100+200+150 current-temps.svg /tmp/exhb2.png
Cropping and scaling, however, fails:
convert -crop 100x100+200+150 -scale 1000x750 current-temps.svg /tmp/exhb3.png
How do I make ImageMagick "zoom into" the SVG before cropping?
(I realize ImageMagick can only read, not write, the SVG format, but
it should still be able to do what I want?)
EDIT/SOLVED:
Thanks, robermorales.
inkscape -z -e out4.png -w 1000 -h 1000 -a 200:200:400:400 current-temps.svg
(for example) worked like a charm.
I also realized that copying the SVG, tweaking the transform line:
<g transform="scale(3,3) rotate(-90) translate(-90,180)">
and then converting to PNG is another solution.
Try doing scale before crop.
However, doing that using inkscape cli is easier.
Sorry for no links, afk
Don't crop an SVG into PNG.
You can use viewBox to re-define the crop area then convert that SVG into PNG in highest solution as possible.
Check this post https://www.sarasoueidan.com/blog/svg-coordinate-systems/ explain what is viewBox and you will got my idea.
Proper ImageMagick syntax here is to read the input, then crop, then resize. See http://www.imagemagick.org/Usage/basics/#why Though that is not likely the issue. The issue is that -scale will replicated pixels and so make it blocky. You should replace -scale with -resize. That will be smoother, but blurry for the amount of magnification you are requesting. Try this command:
convert current-temps.svg -crop 100x100+200+150 -resize 1000x750 exhb3.png

Resources