I'm trying to find out the capabilities and limitations of what i can actually do with imagemagick and would like to get some feedback on this.
What I ultimately want to do is use imagemagick to output images to the browser with custom ttf fonts in it, so that the user enters some text and it displays all the various images with the different fonts.
What are the limitations of imagemagick, can i specify the location of where to draw the fonts on the image, and will it display differently on different systems or browsers?
I will appreciate it if you can include anything else that I need to know about working with imagemagick, before i jump into this little project.
You should check imagemagick example page: http://www.imagemagick.org/Usage/
It is really powerful and there are so many you can do.
And yes, you can specify font convert -t a.ttf ...
You can definitely tell imagemagick the location of your text in the image and since it is generated on your server, it will be the same on any browser. The problem may be more how you are displaying those images (any css or javascript tricks ?)
The thing is that all those fonts will need to be installed on your server where the image is going to be created.
Check this about converting text to images: http://www.imagemagick.org/Usage/text/
Another useful command is convert -list font which will list the system fonts, their paths, and aliases. Very useful when porting scripts from one machine to another
convert -list font|grep -i arial
Font: Arial
family: Arial
glyphs: /Library/Fonts/Arial.ttf
Font: ArialB
family: Arial
glyphs: /Library/Fonts/Arial Bold.ttf
Font: ArialBI
family: Arial
glyphs: /Library/Fonts/Arial Bold Italic.ttf
Font: ArialBk
family: Arial Black
glyphs: /Library/Fonts/Arial Black.ttf
This makes it really easy to do things like bold or italicize with imagemagick.
convert -background none -fill black -font ArialB -pointsize 15 \
-size 380x caption:'i <3 stackoverflow' \
summary_text.png
Related
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
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(...))
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
I would like to change the black border of this image :
http://dev.loungeup.net/im/
to transparent with Imagick in PHP.
The result image should have the gray border visible, the image inside the gray border visible, and everything outside the gray border (today in black) should be transparent and let the underlying content (in HTML page for example) visible.
I have been through the documentation several times and tried several solutions but did not figure out how to achieve it.
Any help is appreciated.
Thanks
Here's one method you could use which is similar to Photoshop's magic wand tool:
convert original.jpg -alpha set -channel alpha -fuzz 18% \
-fill none -floodfill +0+0 black transparent-border.png
Here are the commands broken down:
convert original.jpg: start Imagemagick with the original image
-alpha set: activate the alpha channel
-channel alpha: have subsequent operators act on alpha channel
-fuzz 18%: see -floodfill...
-fill none: see -floodfill...
-floodfill +0+0 black: start from the upper left corner (+0+0) and find neighboring pixels within -fuzz color distance of black and replace it will -fill
transparent-border.png: the output image
Here's the result:
Unfortunately, as you can see, this method still leaves some dark pixels with the image you provided because the border is not pure black and blends a bit with the inner gray border, and the image itself is quite small.
You will get much better results if you have a better-quality source image, or a larger one which you could then reduce in size after modifying.
If you're stuck with these small images (or if you just want to use another method), I would recommend going a different route where you create your own mask shape that is just smaller than the shape of the original photo, then add back your own gray border. I put together an example of this process below.
A possible command for this method would be:
convert original.jpg mask.png -compose CopyOpacity -composite \
-compose src-over new-border.png -composite clean-result.png
... broken down ...
convert original.jpg mask.png: start Imagemagick with original image and bring in mask.png as the second layer (mask.png is a white rounded-rectangle shape of the photo on a black background, but the shape is slightly smaller than that of the original - the result will remove the original's gray and black border).
-compose CopyOpacity -composite: use mask.png to "knock out" shape from original.png
-compose src-over: reset composite method to a simple overlay
new-border.png -composite: overlay a gray border (png is a 3 px wide border, 1px on each side of mask edge on a transparent background)
clean-result.png: the output image
I created mask.png and new-border.png in Photoshop. You could use Imagemagick's vector tools and do this all in one command using just original.jpg, but it wouldn't be easy.
The result of the above:
On a final note, I'm not sure whether you're using PHP's Imagick or the Imagemagick from the command line. Several years ago I tried using Imagick but quickly became frustrated from the lack of documentation compared to the command line (perhaps that's changed, though). Instead, I execute the commands from PHP (e.g., from exec() or passthru()). Some or many others will probably tell you that you should never execute shell commands from PHP, but as long as you carefully escape any arguments I have yet to see a convincing argument against doing so. Then you'll have the entire Imagemagick documentation at your disposal (http://www.imagemagick.org/Usage/).
Cheers.
I'm trying to crop paper documents from photographs. For example, someone takes a picture of a document and sends it to the server and it will get edited to look like a scanned document. How can I detect the border of the document and crop it using ImageMagick?
Thanks
You want to use the convert command with the -trim option to crop out the solid color borders, and since the images are scanned, the -fuzz option to make sure trim completely crops the border even if the color isn't perfectly solid.
So something like this:
convert input.jpg -fuzz 2% -trim output.jpg
Translating that into PHP code, you'd end up with this:
$image = new Imagick('input.jpg');
$image->trimImage(2); // Trim the image with a 2% fuzz
$image->writeImage('output.jpg');