Imagemagick: TIF converted to JPG looks terrible - php

Im trying to convert a bunch of tif files to jpg I've tried ImageMagick's -quality, -density settings, and still the created file image quality looks terrible.
exec('/usr/bin/convert /mypath/myfile.tif -trim -quality 100 /mypath/converted.jpg');
Is there something I can add to this command to make the jpg look a lot better? Im certain the source file looks good.
Before conversion (.tif):
After conversion (.jpg):

I suspect your TIFF is binary and needs to be converted to 8-bit grayscale. So change your ImageMagick command to
exec('/usr/bin/convert /mypath/myfile.tif -type grayscale -trim -quality 100 /mypath/converted.jpg');
If that does not work, then post a link to your actual tiff file and not one that has been converted to jpg

For your .tiff.pdf files, try this in ImageMagick
convert -density 288 C0000005.tiff.pdf -alpha off -trim +repage -type grayscale -resize 25% -quality 92 C0000005.jpg
This is what I get:
If you want a better quality result, you can try -quality 100, but I doubt it will make the quality much better and will increase the file size. Better would be to increase the -resize 25% to some larger value such as 33% or 50%. That will make the resulting jpg dimensions larger. This process is called supersampling, namely, making the PDF bigger and then resizing afterwards.

Related

Tesseract - Detecting small font size of image and convert to text

I have a screenshot of the bank cheque, I need all the text from this screenshot but tesseract is unable to read it properly. I also tried to pre-process the image but the output fails miserably.
I am using ImageMagick for pre-processing and Tesseract for recognizing text.
The link to the image: https://imgur.com/a/pcgizic
I am able to retrieve the account number, but not IFSC code and person name "SRINIVAS"
The steps I am following is as follows:
magick -density 300 check1.jpg -depth 8 -strip -background white -alpha off check1.png
magick convert check1.png -resize 250% res_check1.png
convert -brightness-contrast 10x30 res_check1.png b_res_check1.png
convert b_res_check1.png -threshold 45% bin_res_check1.png
tesseract bin_res_check1.png o_res_check1
Note: I tried to resize the image upto 400% but it did not work.
Google Vision API is able to read and convert every single text properly.
In ImageMagick, you can use -lat (local area threshold) to process the image to clean the background. I also have a bash shell script, text cleaner, at http://www.fmwconcepts.com/imagemagick/index.php.
Input:
For ImageMagick 6, try
convert input.jpg -negate -lat 25x25+10% -negate result.png
Vary the -lat arguments to see if you can improve the results.
For ImageMagick 7, replace convert with magick.
Does that help? Small fonts are going to be hard to process. Enlarging a raster image, does not usually help much. But you can try with sharpening filters or sharper resize filters, such as -filter catrom. See http://www.imagemagick.org/Usage/filter/
You can also try doing a perspective transformation to rectify the original image before further processing. This might also help. See http://www.imagemagick.org/Usage/distorts/#perspective

imagemagick composite add watermark to pdf and quality

I need to add watermark in pdf, I found 2 solutions-with ghostscript and imagemagick composite. ghostscript with watermark can't call from command line so I use command composite
composite -quality 100 -gravity NorthEast -density 400 -dissolve 80% -size 50x30 -geometry +100+20 watermark.png file.pdf file.pdf
the problem is poor quality of result in pdf. If using image instead pdf quality is ok.
Any ideas?
Not sure exactly what you are hoping for, but you need to be more careful about the ordering of your parameters. Remember there are two (actually three or more) main types of parameter with ImageMagick:
There are settings (like -quality, -density and -gravity) which are remembered till the end of processing. You may as well put all these at the start of your command and get them out of the way generally.
There are operators (like -resize, -crop, -sharpen) which act immediately on all the currently loaded images.
I think the problem in your command is that you have the images after the operators you want to apply instead of before and that your -size isn't resizing anything - it is just setting the size for any canvases your later create - except that you don't:
composite -quality 100 -gravity NorthEast -density 400 -dissolve 80% watermark.png -resize 50x30 -geometry +100+20 file.pdf file.pdf

Trim image background but leave padding in Graphicsmagick

I assemble Graphicsmagick commands in php and then call them using exec(). I need to trim images but retain a padding of 20 pixels. So I want to do the following:
exec('gm convert input.jpg -trim -bordercolor white -border 20x20 output.jpg');
but use the color which was trimmed instead of white. How this can be achieved?
This works for me.
gm convert input.jpg -crop 1x1+0+0 corner.txt
color=`sed -e "s/.* #/#/" corner.txt`
gm convert input.jpg -trim -bordercolor $color -border 20x20 output.jpg
The txt format in GraphicsMagick has no header (unlike ImageMagick) so the (0,0) pixel is in the first line (actually the only line of a 1x1 image).
The corner.txt file for the 1x1 cropped image is simply this:
0,0: (255,255,255) #FFFFFF
It happened to be white in my test image, but it will be whatever the color of the (0,0) pixel.
It seems that this is currently not possible with GraphicsMagick (at the time of this writing: GM v1.4 beta).
In ImageMagick it can be done because IM provides an fx command to sample the color value of a given pixel:
convert myimage.png -format '%[fx:p{10,20}]' info:
This will return the color value of the pixel at position x=10, y=20. The output can then be used in subsequent IM commands to add a border of that color.

ImageMagick - convert multipage PDF into one image

I am using ImageMagick to convert PDF files into images. However, some of the PDF's have multiple pages, which is proving to be a real problem.
My local convert is below.
exec("\"C:\\Program Files (x86)\\ImageMagick-6.8.5-Q16\\convert.exe\" -density 300 -quality 75 \"{$path}{$filename}{$ext}[$page]\" \"{$targetFile}\"");
If i remove [$page] from the exec it works but creates an image per page, which isn't what I want.
I have been searching for a while now and i've ran out of hope and ideas. Is there any way I can get all of the new images into one final image, or convert the PDF straight into one image? Any help would be greatly appreciated, cheers.
Check out the -append and +append options.
-append appends the images vertically, and +append appends them horizontally.
Usage (http://linuxers.org/quick-tips/convert-pdf-file-single-image-using-imagemagick):
According to that link, the output from a multi-page pdf convert would be ${targetFile}-0.png, ${targetFile}-1.png, ${targetFile}-n.png, etc. Once you have converted the pdf into multiple images, use the -append or +append option:
convert ${targetFile}-* -append single_image.png
To put it all together, try something like this (you may have to play with it a bit; I haven't used Imagemagick from the Windows' shell):
// convert pages of pdf
exec("\"C:\\Program Files (x86)\\ImageMagick-6.8.5-Q16\\convert.exe\" -density 300 -quality 75 \"{$path}{$filename}{$ext}\" \"{$targetFile}\"");
// then append them
exec("\"C:\\Program Files (x86)\\ImageMagick-6.8.5-Q16\\convert.exe\" \"{$targetFile}-*\" -append "${someName}\"");
More resources:
http://www.imagemagick.org/script/command-line-options.php#append
http://www.imagemagick.org/Usage/layers/

Imagemagick convert multiple images to pdf size

For a project I am running ImageMagick to convert several images (all of the same size 960x570) into a single PDF.
The command I'm running is:
convert *.jpg pdf/export.pdf
Since the images are all the same size I expect each PDF page to be equal in size but this is not the case as you can see in the screenshot below:
This is bit annoying since I don't know how I can force the size of each PDF page to be the same.
I tested the PDF on both an android device, ubuntu pdf reader and adobe reader and they all show the different sizes.
Thanks for helping!
EDIT1:
After testing out a few more things as suggested by Marc B and Basti I managed to put the same size on the JPG (uploaded) images.
The problem still persists with the images that are generated using webkit2png (Python script that converts a webpage/html file to an image). After trying (almost?) everything in the convert command I can't get these generated images to shop up properly in the PDF as you can see below:
Any help would be much appreciated!
I found the solution:
I needed to add the following options to convert the image saved from webkit2png:
-density 960x570 -units PixelsPerInch
I hope this helps other people out too in the future ;)
After a few seconds of using google, I found this:
-size <geometry> width and height of image
Example given by them:
$ convert -size 320x85 canvas:none -font Bookman-DemiItalic -pointsize 72 \
-draw "text 25,60 'Magick'" -channel RGBA -blur 0x6 -fill darkred -stroke magenta \
-draw "text 20,55 'Magick'" fuzzy-magick.png
http://www.imagemagick.org/script/convert.php

Categories