ImageMagick - Convert a GIF to PDF portrait(letter) on half page using convert command - linux

I have a image of dimension 1400x800, which I need to convert to a PDF so that the PDF is in in Portrait form & contains the image in top/bottom half of the page.
I need to use ImageMagick 6.4.8 2009-09-21 as that is the only available on my production server. Cannot use Ghostscript.
Image Details
>identify sample.gif
sample.gif GIF 1400x800 1400x800+0+0 8-bit PseudoClass 256c 29.3kb
I have tried all the options that I found out & nothing works. It always converts to a Landscape with the image occupying the complete page.
Here are some of the options that I tried:
convert -bordercolor none -border 100x100 -page Letter -density 72 sample.gif -resize 792x612\! sample.pdf
convert -bordercolor none -border 100x100 -page Letter -density 72 sample.gif -resize 612x792\! sample.pdf
convert -density 72 -size 1400x800 sample.gif -page Letter -density 72 -resize 504x288 -bordercolor none -border 11%x14% sample.pdf
and many such combinations, but nothing works. I use -resize 612x792 so that its letter size, but then the image also gets stretched vertically. Is it possible to achieve what I am trying to, what options am I missing?
Note: I have to do this in a program so need a command based solution using convert.

My guess is that you are using this to print shipping labels. Either way try this
convert -size 1700x2200 xc:white sample.gif -geometry 1400x800+150+150 -composite -page Letter -quality 100 sample.pdf

Related

Can't fit text to image with ImageMagick

I need to fit my text to image. My image have different sizes so i can't set constant pointsize.
My command looks something like this
convert
-fill white
-font Winter Calligraphy
-size `${options.width}x${options.height}`
label: KJHGFD
test.gif
on output you can see cropped part on top of picture.
Output:
I have problem only with this fonts, other fonts works great.
I tried to add white border on top. Unfortunately, this only moved the damaged text to bottom.
I can't change area size.
Text must fill as much space as possible.
I need to use Winter Calligraphy font
Here is a slightly kludgy way of getting the result you want. Here are the steps:
First, use caption: to get ImageMagick to tell you the pointsize it would use to fill your text box and extract that info
Create a canvas twice as wide and twice as tall as the one you really want and draw your text in the middle of that - it is bound to fit!
Now trim away the extraneous background around the text so you have the absolute minimum bounding box to contain the text
Resize the result to your desired size.
#!/bin/bash
# Width, height and text
w=600
h=150
text="KJHGFD"
# Get pointsize ImageMagick thinks is good
pointsize=$(convert -gravity center -background black -fill white -size ${w}x${h} \
-font "Winter Calligraphy.ttf" caption:"$text" -format "%[caption:pointsize]" info:)
echo ImageMagick likes pointsize: $pointsize
# So draw text in that size on larger canvas, trim to bounds of letters and resize to desired size
wb=$((w*2))
hb=$((h*2))
convert -gravity center -fill white -size ${wb}x${hb} xc:black \
-font "Winter Calligraphy.ttf" -pointsize $pointsize -annotate 0 "$text" \
-trim +repage -resize ${w}x${h}\! result.png
This works for me in ImageMagick 6.9.10.97 Q16 Mac OSX. I have added -background white -fill black -gravity center to your command.
convert -background white -fill black -font "/library/fonts/Winter Calligraphy.ttf" -size 569x196 -gravity center label:KJHGFD test.gif

Resize canvas of svg images in command line

I have bunch of svg images (1.svg 2.svg ...) and i would like to center them and resize canvas to the same value for all of them. Let's say that image 1.svg has w. 600 and h. 800 and 2.svg has w. 1000 and h. 400. I would like to set canvas for both as 1000x800 and center images. As the result the images won't be resized, but they will have extra space on sides -> 1.svg will still be 600x800 but in canvas with size 1000x800 and 2.svg will be 1000x400 but in canvas with size 1000x800. Is it possible to do in command line? Using inkscape, imagemagick etc.?
Thanks in advance!
You need the -extent option to ImageMagick, like this:
convert 1.svg -gravity center -background yellow -extent 1000x800 result.png
So, let's make some images:
convert -size 600x800! xc:red 1.png
convert 1.png -gravity center -background yellow -extent 1000x800 result.png
Or the other way:
convert -size 1000x400! xc:red 2.png
convert 2.png -gravity center -background blue -extent 1000x800 result.png

ImageMagick best fit text within rectangle?

I have an image like this, with a rectangle at specific coordinates:
(for illustratory purposes I put the coordinates of the rectangle and its size and center in there)
Now I want to render some text with ImageMagick, so that it fits exactly within the rectangle.
If it's a very short (narrow) string, the rectangle's height will be the limiting factor:
On the other hand with a long (wide) string, the rectangle's width will determine the size:
In either case, independent of how short or long the text is, I would like to print it in one line (i.e. no word wrapping or multi line), and have it fit exactly in the rectangle, and make sure it's centered (the center of the text is in the center of the rectangle).
My questons:
How to do this 'best fit' feature with ImageMagick (I don't know how to dynamically determine the required -pointsize for this)
How to get the text centered, when I use -gravity center it seems to apply to the position of the text within the entire image, i.e. text coordinates become relative to the entire image's center. But I want to specify exact (absolute) coordinates, and that should be the center of the text.
For example, if I do this:
convert test.jpg -font Arial -fill yellow \
-pointsize 65 -draw "text 398,90 'Hello'" test2.jpg
I'm getting:
Note how the coordinates I specify (the rectangle's center) become the bottom left anchor point for the text! (this surprised me)
And if I do:
convert test.jpg -font Arial -fill yellow \
-pointsize 65 -gravity center -draw "text 148,-94 'Hello'" test3.jpg
I get:
Which is kinda OK, but note the weird text coordinates I have to use to get that. And besides I wouldn't know how to automatically calculate the pointsize (did the above by trial and error).
Updated Answer
With what I have now gathered, I think this may be your best option.
convert sea.jpg \( -size 173x50 -background none label:"A" -trim -gravity center -extent 173x50 \) -gravity northwest -geometry +312+66 -composite result.png
And this:
convert sea.jpg \( -size 173x50 -background none label:"A very much longer label" -trim -gravity center -extent 173x50 \) -gravity northwest -geometry +312+66 -composite result.png
Basically, I am using some "aside processing" in parentheses to generate the text and then compositing it onto the page afterwards. I generate the text with label: to the best automatic size, then trim off any excess space around the text. I then centre the trimmed text, using -gravity center and expand the background out (using -extent) so that the text box is always the same size, then I can position it (with -geometry) when compositing it relative to the top-left corner as I reset -gravity to NorthWest.
Original Answer
If you want ImageMagick to do its best to fit your text in a given box, you should use caption rather than annotate, label or -draw "text".
So you want to load your sea image, set the size for the caption, draw the caption and composite that onto the image at the correct spot using -geometry:
convert sea.jpg -size 173x50! caption:"Your text" -geometry +312+66 -composite result.png
Or, with longer text:
convert sea.jpg -size 173x50! caption:"A considerably longer text that will result in a smaller font being chosen" -geometry +312+66 -composite result.png
If you want a blank background, use -background none before caption:
convert sea.jpg -size 173x50! -background none caption:"Your text" -geometry +312+66 -composite result.png
If you want to centre your text, you can do that with PANGO, like this, but I believe you then give up the auto-sizing feature:
convert sea.jpg -size 173x50! -background none -gravity center pango:'<span foreground="yellow">ABC</span>' -gravity northwest -geometry +312+66 -composite result.png

how to control the distorted and stretched image in graphicsmagick node.js

I want an image to be display by keeping its aspect ratio same by re-sizing it, but the image looks stretched. I have three images all of 50 by 50. I want the left image to be re-sized by maintaining its aspect ratio. The other two images will be shown up and down parallel to the first image. I think mosaic() build the four blocks. but I want three blocks, in fitst block first image will come up and it would be re-sized all the way till end, and the next two blocks the other two images will come up and down and show parallel to the first one. Below is the code
gm()
.in('-page', '+0+0')
.in('-resize', '50x100!')
.in('http://localhost:8080/image1')
.in('-page', '+50+0')
.in('http://localhost:8080/image2')
.in('-page', '+50+50')
.in('http://localhost:8080/image3')
.mosaic()
.write('C:/images/output.jpg', function (err) {
if (err) console.log(err);
});
I want an image like below but with keeping its aspect ratio, black image looks stretched, I don't want it be stretched, and black image should cover whole left area till end
If I remove ! the image then looks like, I want an image as above but not stretched
I don't use node but I think you will be able to adapt this:
Make our three images:
convert -size 50x50 -background black -fill white -gravity center -pointsize 36 label:"1" image1.jpg
convert -size 50x50 -background red -fill white -gravity center -pointsize 36 label:"2" image3.jpg
convert -size 50x50 -background red -fill white -gravity center -pointsize 36 label:"3" image3.jpg
Now resize and arrange:
convert image1.jpg -resize x100 -gravity center -extent 50x100 \( image2.jpg image3.jpg -append \) +append result.png
Essentially, I am making image1 100 pixels tall with -resize x100 and then extracting the central 50 pixel wide band down the middle with -gravity center -extent 50x100. Then, in parentheses and "on-the-side" I am loading image2 and image3 and placing one below the other with -append. That pair of images is then appended to the right of the original image with +append and the result is written out to result.png.

linux convert thumbnail but move position

I have used linux convert command to successfully create thumbnails as follows:
$disposition='200x200';
$str="convert -size $disposition \"$fullPath\" -resize $disposition +profile '*' \"$fullPathThumb\"";
$result=`$str`;
However what I want to also do is "slide the sized down image some so that the upper left corner of the image is moved around, usually negative value up and negative value left, to center and crop. How would I do that? Thanks.
I am not too sure I understand your "sliding around" idea, but hopefully the following will explain how you can crop with offsets and resize. If not, please ask further questions.
Let's start with an image made of 8 blocks, each 100x100 pixels and off-square so we know which is side is which.
convert -size 100x100 \
\( xc:red xc:blue xc:green xc:black +append \) \
\( xc:cyan xc:magenta xc:yellow xc:white +append \) -append out.png
Now, we check its size is indeed 400x200:
identify out.png
out.png PNG 400x200 400x200+0+0 8-bit sRGB 8c 467B 0.000u 0:00.000
So, first we will do a simple resize, which will preserve the 2:1 aspect ratio:
convert out.png -resize 300x300 simple_resize.png
identify simple_resize.png
simple_resize.png PNG 300x150 300x150+0+0 8-bit sRGB 47c 672B 0.000u 0:00.000
Now we use the bang operator (!) to tell ImageMagick rather forcefully "Do what I said"
convert out.png -resize 300x300! simple_resize_changed_aspect.png
identify simple_resize_changed_aspect.png
simple_resize_changed_aspect.png PNG 300x300 300x300+0+0 8-bit sRGB 52c 894B 0.000u 0:00.000
And finally, we come to what is hopefully the bit you want, which is crop and resize. So let's crop an area 150 pixels wide by 100 pixels high starting 150 pixels across to the right from the top left corner and 50 pixels down from the top left corner:
convert out.png -crop 150x100+150+50 -resize 300x300 x.png
That looks right, but if we use identify we will see that ImageMagick has remembered too much about where our image came from and is considering it a part of the bigger original image:
identify x.png
x.png PNG 300x200 800x400+300+100 8-bit sRGB 30c 732B 0.000u 0:00.000
So, actually we better tell ImageMagick to reset the dimensions of the image so it is sitting on a canvas of "just the right size"... by using the +repage option on the previous command like this:
convert out.png -crop 150x100+150+50 -resize 300x300 +repage x.png
and check again
identify x.png
x.png PNG 300x200 300x200+0+0 8-bit sRGB 30c 690B 0.000u 0:00.000
That's better - probably best to use +repage whenever you crop. I hope that addresses your question, as I said, please ask further if not.

Resources