How to resize image only if width exceeds with Graphics/Image Magick
You can easily achieve that by using an 'unlikely large' height value:
convert orig.png -resize '64x10000>' scaled.png
This will resize the original PNG only if its width was larger than 64 pixels or if its height is larger than 10 000 pixels. But the chance that the command will ever encounter an input that's higher than 10 000 pixels is very low. So it will (almost) only encounter input files where the width value matches.Original PNGs with widths lower than 64 pixels will remain untouched (unless their height is above 10 000 pixels...).
CollectionFS Graphicsmagick resize width only if larger than
This works:
gm(readStream, fileObj.name()).resize(1200, 1200, '>').quality(100).autoOrient().stream().pipe(writeStream);
However, after using it with the gridfs package you may get this error:Error: <ID> does not exist
. This is a package's bug, so this topic would go separately. Bash: batch resize images if width/height exceeds a specific value
A possible solution:
#!/bin/sh
set -e
maxwidth="1900" # in pixels, the widest image you want to allow.
#find all .jpg in current dir and subdirectories
FILES="$(find . -iname '*.jpg')"
for imagefile in $FILES
do
if [ -f "$imagefile" ]; then
imgwidth=`sips --getProperty pixelWidth "$imagefile" | awk '/pixelWidth/ {print $2}'`
else
echo "Oops, "$imagefile" does not exist."
exit
fi
if [ $imgwidth -gt $maxwidth ]; then
echo " - Image too big. Resizing..."
sips --resampleWidth $maxwidth "$imagefile" > /dev/null 2>&1 # to hide sips' ugly output
imgwidth=`sips --getProperty pixelWidth "$imagefile" | awk '/pixelWidth/ {print $2}'`
imgheight=`sips --getProperty pixelHeight "$imagefile" | awk '/pixelHeight/ {print $2}'`
echo " - Resized "$imagefile" to $imgwidth""px wide by $imgheight""px tall";
fi
done
resizing image with rmagick/imagemagick to be less than a specified product of its width and height
You need to properly calculate the ratio
, which is a square root from your desired dimension divided by (row
multiplied by col
):
row, col = [2793, 1970]
ratio = Math.sqrt(4_000_000.0 / (row * col))
[row, col].map &ratio.method(:*)
#⇒ [
# [0] 2381.400006266842,
# [1] 1679.6842149465374
#]
[row, col].map(&ratio.method(:*)).reduce(:*)
#∞ 3999999.9999999995
Resize images that are larger than X in Ubuntu
You might want to use ImageMagick. It isn't included in the default installations of Ubuntu and many other Linux distributions, so you will have to install it first. Use the following command:
sudo apt-get install imagemagick
You can specify a width (or height) and ImageMagick will resize the image for you while preserving the aspect ratio.The following command will resize an image to a width of 2000:
convert example.png -resize 2000 example.png
There is also an option so that it will only shrink images to fit into the given size. It won't enlarge images that are smaller. This is the '>' resize option. Think of it only applying the resize to images 'greater than' the given size, the syntax might be a little counter-intuitive.convert example.png -resize 2000\> example.png
You can use
bash
to apply the command to all of your images,for file in *.png; do convert $file -resize 2000\> $file; done
Increase the length of first image in graphics magick
Your problem with the resize is caused by mixing .in()
methods which insert things direct on the command line with the graphicsmagic library methods which apply some extra heuristics on what to do. Adding a .resize
attempts to resize the main graphic object (which is then ignored by .mosaic
which sizes the final image according to the contents).
The easiest solution is to use your .in()
-based syntax for the resize:
gm()
.in('-page', '+0+0')
.in('-resize', '50x100!')
.in('http://localhost:8080/user1')
.in('-page', '+50+0')
.in('http://localhost:8080/user2')
.in('-page', '+50+50')
.in('http://localhost:8080/user3')
.mosaic()
.write('C:/images/output1.jpg', function (err) {
if (err) console.log(err);
});
This works on my test with local images.
Related Topics
Automatically Adding Proxy to All Http Connections in Ruby
Adding Two Activerecord::Relation Objects
Filtering Sensitive Data with Vcr
How to Print a Multi-Dimensional Array in Ruby
How to Change "3 Errors Prohibited This Foobar from Being Saved" Validation Message in Rails
How to Calculate a String's Width in Ruby
What Is Toplevel_Binding in Ruby
One or More Params in Model Find Conditions with Ruby on Rails
Rails: How Does "New" Action Called "Create" Action
Get Route for Base Class of Sti Class in Rails
More Ruby Way of Doing Project Euler #2
How to Set in a Middleware a Variable Accessible in All My Application
Ruby How to Generate a Tree Structure Form Array
Gem Install Rmagick Fails on Os X El Capitan
Gem Ransack Doesn't Return Any Results When Searched with Full Name
How Does Ruby's Sort Method Work with The Combined Comparison (Spaceship) Operator