Point ImageMagick at file format to use - php

I wonder if it's not possible to give ImageMagick a hint what type the input string (or file) is.
E. g. treat .sql as textfile.
I would need it to be used in php (Imagick).
The only thing I get is an exception that the file format is unknown:
no decode delegate for this image format `' # error/blob.c/BlobToImage/361

You can tell ImageMagick that a file contains text like this:
convert TEXT:stuff.sql result.png
which will give you an image of the contents of the text file stuff.sql.
So, if you want an image of a directory listing, you can do:
ls -l | convert TEXT:- listing.png
Or, if you have some SQL in a file commands.sql:
convert -background yellow -fill magenta text:commands.sql -trim result.png
Or, if you want it centred and with a larger border:
convert -background yellow -fill magenta text:commands.sql -trim \
-gravity center -extent 120%x120% result.png

As #MarkSetchell mentioned you can tell ImageMagick the (custom) filetype by prefixing it.
FILETYPE:FILEPATH
eg
TXT:/path/to/file.sql
I suppose you have to prefix a file format from specific list; identify -list format should give you that list of supported formats on linux
This works also with php: $im = new Imagick("txt:/path/to/file.sql");
Maybe someone knows how/if it works when having file contents as blob ($im->readImageBlob(...))

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

ImageMagic resize and accidental redirection

I am using ImageMagick to generate an image thumbnail, and i'm experiencing a weird problem. Weird because this has worked before and I dunno what changed to make it start failing.
Anyway, my commandline looks like this: (except that I've simplified the filenames)
convert -size 160x160 a1.jpg -thumbnail 144x144 -bordercolor #F00 -border 160-gravity center -crop 160x160+0+0 -repage -format jpg -quality 70 ./a2.jpg
What's happening is that the system is redirecting to a file named " -bordercolor"
Any ideas why this is happening and how to avoid?
On bash everything after the # will be considered part of a commment.
Maybe enclosuring the color with quotes

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