Now I have a lots of pictures. they comes in many dimensions,aspect ratios. for example. 100px*500px,600px*200px,1000px*700px.
Now I want a tool for me to resize all the images to square shape , but never distort or stretch the image , padding white background instead.
the result I want , 500*500 , 600*600, 1000*1000, but have white background.
I find irfanview can not make it.
thanks.
Use ImageMagick thumbnailing capabilities with -extent and -background white options: see this documentation.
Related
I am in a situation where I have a set of images with variable width and height and I need to ensure that those images comply with an aspect ratio of 2:1.
The only assumption I can make is that those images have an aspect ratio greater than 2 so I need to expand the canvas.
With ImageMagick I can do this:
magick input.jpg -background black -gravity north -extent 2:1 output.jpg
But this crops instead of adding canvas as desired. Is there an option to avoid cropping, i.e. force image padding? Remember I do not know the image resolution beforehand.
Of course with a more complex script I can extract the image dimensions and compute the desired resolution but I am trying to keep things simple if possible.
P.S.: The images comes from a laser scanner and are going to be used in a 360 panorama viewer hence the need to meet an aspect ration without cropping.
With ImageMagick v7 you can do some inline calculations that will adjust the results of the -extent operation to pad any size input to a 2:1 ratio. See this command as an example...
magick input.png -gravity center -extent "%[fx:max(w,h*2)]x%[fx:max(w/2,h)]" result.png
That will pad any extra space with black. Add -background <somecolor> before the extent operation to change the padding color.
I need to convert many PDF pages to images, containing simple shapes such as rectangles or triangles. Texts and composed shapes consist of lines are also in the PDFs.
Example shape from a screenshot out of a PDF:
Note: The screenshot is unsharp because I've used Windows' print function in order to separate a single page. It seems to be that this function converts PDFs to images and then saves them as PDF file.
Requirements of images:
sharp edges, a staircase is desired without any grey values
lines of equal thickness
relatively low resolution (<300 dpi would be wonderful)
black & white only
I'd like to work with ImageMagick because it provides a lot of features. I guess anti-aliasing, black-threshold and sharpen are relevant to this, but I haven't reached my desired results.
PDF example of a similar shape
Something like this, maybe?
magick -density 288 -background white ~/Desktop/drawsvg.pdf -flatten -colorspace gray result.png
I'm trying to overlay a green image over another image, and then convert the green color to transparent. The code I use is below. It works okay, however it leaves a slight green border. What's the best way to have this green border not show up? I tried using fuzz, however results with this vary. Sometimes it cuts out other colors. Is there a better way to prevent the green border than using fuzz?
convert image.png ../greenoverlay.png -layers merge image.png;
convert image.png -fuzz 41% -transparent \#00FF00 final.png;
Thank you all!
With dragonfly I can do the following:
image.thumb('300x300#')
Which will resize the image, maintaining aspect ratio and cropping it centrally (effectively chopping off the ends of the longest edge).
However, if the image has an edge smaller than 300px, then it is scaled upwards. What I would prefer is that in this case the image is not resized, but instead white padding is added where necessary.
So basically, if both edges are 300px or over I want the normal behaviour from 300x300#, but if any edge is smaller than 300px, then the image is not resized at all, but still cropped to 300x300 with whitespace added where necessary.
Is this possible with either of Dragonfly's built in processors (#thumb or #convert? Or do I need to build my own processor? If so, what sort of imagemagick commands do I need to be looking at?
Best solution would be to create a white canvas image that is 300x300 then composite your image, centered, on top of the canvas image. Then crop it with center gravity (centered). This would yield a 300x300 image with a white canvas on any vertical or horizontal edges that had a dimension smaller than 300.
** For this solution you may need to install the RMagick gem, as I do not believe Dragonfly has extended the ImageMagick operations that you will need.
This is how I would approach it:
#Set file path first and load a white image called canvas that is 300x300 into Imagmagik
canvas_file_path = "#{Rails.root}/app/assets/images/canvas.png"
canvas_image = Magick::Image.read(canvas_file_path).first
#Then perform the compositing operation. This overlays your image on top of the canvas, center gravity ensures that your image is centered on the canvas.
canvas_image.composite!(<YOUR IMAGE>, CenterGravity, Magick::OverCompositeOp)
#then write the file to a temporary file path so you can do something with it
temporary_file_path = "#{Rails.root}/tmp/#{#model.id}"
canvas_image.write(temporary_file_path)
Be sure to add the require statement in your file, pay close attention to the capitalization, it is RMagick not Rmagick
require "RMagick"
For reference here is the ImageMagick example from the documentation to perform the compositing operation that you will need
composite -gravity center smile.gif rose: rose-over.png
Rmagick Documentation on how to composite images - http://www.imagemagick.org/RMagick/doc/image1.html#composite
Rmagick Gem - https://github.com/rmagick/rmagick
ImageMagick reference to compositing - http://www.imagemagick.org/script/composite.php
I have a habit of answering my own questions of SO...
The following can be done with Dragonfly:
def thumb_url
if image.width < 300 || image.height < 300
image.convert('-background white -gravity center -extent 300x300').url
else
image.thumb('300x300#').url
end
end
My mission is to let users upload their own photos then the site crops the face+hair and pastes them on an ecard. The tough part is that I'm not allowed to use flash =/
I understand there are ajax or js face detection solutions out there, but what stumps me is how do I crop an odd shaped pattern. Imagemagick/graphicsmagick as I know can only crop a rectangular or square shaped object (please correct me if I'm wrong). Will this pretty much destroy the whole idea until im has the functionality to crop odd shapes? or are there other ways to crop circles or polygons?
An idea popped up to maybe allow the user to draw some lines in their photo for cropping then maybe the site converts the lines to a vector and fills a color around the lines, the colors are then converted by im to transparent... but then I have no idea how to start this one.. probably not possible at the moment (?).
I'm running out of ideas :(
I have just successfully created a crop :) although not perfect it should work in android or iphone/ipad.
Basically I used a js plotting tool to mark the coordinates, save all those coordinates somewhere.
Then use the coordinates for imagemagick, it appears that although IM doesn't really crop odd shapes it can however use 'mask' to convert the bg of the photo by combining the stencil (where we draw the lines using our coordinates, fill the bg with #000000 color) and the original photo.
The result should be the crop we are looking for :) now we added in some feathering since it's kinda edgy. All this using imagemagick.
Now all we needed are curvier lines since I used 'path' in IM which doesn't really offer a smooth crop. Someone suggested to use 'cubic curves' but may require additional coding since it needs some parameters for each coordinate.
This command will create our stencil (the long set of numbers are our coordinates):
convert -size 450x125 xc:black -fill white -stroke black -draw "path 'M +60+9 +94+18 +96+19 +84+27 +92+36 +97+43 +103+56 +102+58 +109+66 +109+74 +101+68 +98+76 +98+84 +95+88 +98+91 +106+95 +110+99 +111+103 +99+106 +89+108 +73+112 +56+109 +40+109 +26+103 +37+97 +46+91 +48+88 +39+80 +36+71 +32+78 +27+72 +30+61 +35+55 +42+41 +30+37 +40+24 +51+14 +156+9 +197+6 +236+8 +269+16 +265+36 +248+50 +222+52 +213+35 +198+24 +174+18 +155+13 +60+9'" stencil.gif
We will then combine the stencil with the original image (which should bring out our transparent 'crop'):
convert original.jpg stencil.gif -alpha off -compose CopyOpacity -composite combined.png
Finally we feather out the edges:
convert combined.png -alpha set -virtual-pixel transparent -channel A -blur 0x0.7 -level 50,100% +channel -background none -flatten final.png
This is how it works now : https://lh6.googleusercontent.com/_2lSoW37_zqo/TYCD65Vu4zI/AAAAAAAAEcc/vjlCPM54FTI/s800/theoryinpractice.jpg
That's about it, I hope this helps someone.