linux convert thumbnail but move position - linux

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.

Related

Resizing does not respect resolution specification

I've got a 16x512 image comprised of 16x16 images. I want to seperate them, individually scale each one down to 16x8, then put them all back into their full 16x512. I've got a basic idea, but I'm having trouble executing it.
Using the commands from unix stackexchange, I split by file by using convert -crop 16x16 my_image.png crop-%d.png, which yields 32 images (512 / 16 == 32). My next step was where trouble has started. From askubuntu, I found the command mogrify -resize 16x8 crop-*.png, however this does not yield 16x8 images, but rather 8x8, which I do not want. Furthermore, this post on stackoverflow gives me the command for merging these images, which is convert crop-*.png -append my_image_cropped.png, however it does not yield a 16x512 like I want, but rather 8x256 (the 8 is due to the previous bug, but I still want a height of 512, not 256).
What do I need to accomplish my goals? The image in question can be found on imgur.
Edit: Here are some images which will describe the basic idea
The full image:
Both 16x16 and 16x8 side by side
The finalized image, basically the 16x8 will sit in the 16x16 area (right at the bottom part, that is essential), but won't fully fill it.
I am not sure I understand what you want to do. But if you resize 16x8, Imagemagick will keep aspect ratio. If you want to force it to be exactly 16x8 and can accept distortion, then use the ! flag. But you then say you want to put the 32 pieces back to form 16x512, but the resize will make it 16x256, since you have 32 image of height 8. So you have to resize again. Here is how to do that, if that is really what you want.
Create a gradient image for testing:
convert -size 16x512 gradient: grad.png
Do the processing:
convert grad.png -crop 16x16 -resize 16x8! -append -resize 16x512! newgrad.png
Note that proper Imagemagick syntax reads the input first.
ADDITION:
Given your new information in your comment, try this:
convert grad.png -crop 16x16 -resize 16x8 -gravity northwest -background none -extent 16x16 -append newgrad.png
Change the background color as desired and the gravity setting as desired for positioning.

Imagemagick crop from northeast

I am trying to crop an image starting from top-right and cut out a 48x48 box.
This is the image I'm working with
I've tried this
in.png -gravity northeast -crop 48x48 out.png
out-0.png
out-1.png
out-2.png
out-3.png
Which creates like 4 files, none of which are what I want.
When I add x and y values (which I don't want to), it crops from the northeast correctly with only 1 output image, but the box is not 48x48, its 46x38
in.png -gravity northeast -crop 48x48+0+0 out.png
out.png
This gives different outputs for different images. I just tried another and ended up with a 33x48 output.
I need to use the gravity setting instead of the x and y offsets because I'm batch processing a lot of images that are different sizes.
This is the desired output
Can someone please explain to me what I'm doing wrong? Thanks!
If your input image has paging information, the result of your crop may not be what you expect. When working with unknown images, you might do a "+repage" right after reading in the image. Also, when you "-trim" an image, the paging information from the original image remains. The "-crop" will use that paging information instead of the actual height and width, so "+repage" after a "-trim" unless you know you'll need that information. Try this...
convert inimage.png -trim +repage -gravity northeast -crop 48x48+0+0 outimage.png
You should also use "+repage" after any crops if you intend to continue processing the images.

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.

How to batch resize PNG files with commandline on Mac?

I want to resize a folder of PNG images to a specified scaled size like (50%) on Mac. What is the easiest way to do that with commandline?
Thanks,
The ImageMagick command you need is mogrify if you want to do a whole directory (folder) full of images.
# First, check current sizes of PNGs
identify *png
a.png PNG 2480x3508 2480x3508+0+0 8-bit sRGB 25.4KB 0.000u 0:00.009
b-0.png[1] PNG 2480x3508 2480x3508+0+0 8-bit sRGB 2c 2.18KB 0.000u 0:00.000
b-1.png[2] PNG 2480x3508 2480x3508+0+0 8-bit sRGB 2c 2.2KB 0.000u 0:00.000
# Now reduce those puppies in half
mogrify -resize 50x50% *png
# Re-check their sizes
identify *png
a.png PNG 1240x1754 1240x1754+0+0 8-bit sRGB 1.42KB 0.000u 0:00.000
b-0.png[1] PNG 1240x1754 1240x1754+0+0 8-bit sRGB 2c 1.43KB 0.000u 0:00.000
b-1.png[2] PNG 1240x1754 1240x1754+0+0 8-bit sRGB 2c 1.46KB 0.000u 0:00.000
Updated
If you want to do fancier things, you may prefer to use a little loop:
#!/bin/bash
shopt -s nullglob
for f in *.png; do
new=${f/.png/thumb.png}
echo convert "$f" -resize 50x50% "$new"
done
So, this will loop through all PNG files and calculate a new name for each (in the variable new) and then use ImageMagick's convert to resize the images and save with the new name.
Back up your images first and if you like the way it works, remove the word echo from the second to last line to make it actually do anything rather than just tell you what it plans to do.
Just install Imagemagick then you will be able to use convert and mogrify commands which do that sort of things easily.

create non-padded thumb using ImageMagick

I'm trying to create thumbs of an equal size, and I want no padding whatsoever. The thumb should be 154x208 pixels. The original can vary in shape and size.
I'm using ImageMagick, first I tried this:
convert org.jpg -thumbnail 154x208 dest.jpg
This will create a thumbnail that is maximum 154 pix wide AND maximum 208 pix of height. I want an image that is 154x208 pixels though. Without padding.
I tried this:
convert org.jpg -thumbnail x208 -crop 154x208+0+0 dest.jpg
This works great on an image in landscape mode, but a picture in portrait mode results in a thumb that's too narrow. -extent instead of -crop gives me the right end-result, but that ads padding to the thumbnail, and I don't want that.
I'm looking for a thumbnail from a picture that either has the full height and crops the width to fit, or the full width and crops the height, always resulting in a 154x208 thumbnail with no padding, using ImageMagick
I solved it by treating images with a smaller width-to-height ratio different from ones with a larger one:
a smaller width-to-height ratio than 154x200 image:
convert org.jpg -thumbnail 154 -gravity center -crop 154x200+0+0 dest.jpg
and a higher ratio:
convert org.jpg -thumbnail x208 -gravity center -crop 154x200+0+0 dest.jpg
It is an extra step in my coding, and I am still interested in an answer that doesn't need this extra step, but it does the job for now!

Resources