Imagemagick: Convert to fixed width, proportional height - imagemagick

Using the imagemagick convert command, how can I resize an image to a fixed width and proportional height e.g. using the -resize or the -thumbnail option?

Use -resize 100x to resize images to 100 pixels in width while maintaining the height's aspect ratio.
Read the fine ImageMagick manual for details.

Imagemagick geometry: 'width'x'height'
If you leave one part empty, this means resize proportional.
Examples:
100x200 # width = 100, height = 200
300x # width = 300, height = proportional
x300 # width = proportional, height = 300

Related

Crop half of an image in OpenCV

How can I crop an image and only keep the bottom half of it?
I tried:
Mat cropped frame = frame(Rect(frame.cols/2, 0, frame.cols, frame.rows/2));
but it gives me an error.
I also tried:
double min, max;
Point min_loc, max_loc;
minMaxLoc(frame, &min, &max, &min_loc, &max_loc);
int x = min_loc.x + (max_loc.x - min_loc.x) / 2;
Mat croppedframe = = frame(Rect(x, min_loc.y, frame.size().width, frame.size().height / 2));
but it doesn't work as well.
Here's a the python version for any beginners out there.
def crop_bottom_half(image):
cropped_img = image[image.shape[0]/2:image.shape[0]]
return cropped_img
The Rect function arguments are Rect(x, y, width, height). In OpenCV, the data are organized with the first pixel being in the upper left corner, so your rect should be:
Mat croppedFrame = frame(Rect(0, frame.rows/2, frame.cols, frame.rows/2));
To quickly copy paste:
image = YOURIMAGEHERE #note: image needs to be in the opencv format
height, width, channels = image.shape
croppedImage = image[int(height/2):height, 0:width] #this line crops
Explanation:
In OpenCV to select a part of an image,you can simply select the start and end pixels from the image. The meaning is:
image[yMin:yMax, xMin:xMax]
In human speak: yMin = top | yMax = bottom | xMin = left | xMax = right |
" : " means from the value on the left of the : to the value on the right
To keep the bottom half we simply do [int(yMax/2):yMax, xMin:xMax] which means from half the image to to the bottom. x is 0 to the max width.
Keep in mind that OpenCV starts from the top left of an image and increasing the Y value means downwards.
To get the width and height of an image you can do image.shape which gives 3 values:
yMax,xMax, amount of channels of which you probably won't use the channels. To get just the height and width you can also do:
height, width = image.shape[0:2]
This is also known as getting the Region of Interest or ROI

Xcode: I've set my UIImageView to both Min and Max width, then why is it always the smallest size?

I've put the following constraints to my UIImageView (Name = RunwayGallery):
Width: >= 200
Width: <= 600
Align Center X to: Superview
100:133 Ratio to: RunwayGallery
Top Space to: ImageAbove, Equals 10
So why is the UIImageView always 200 width? Even when there is enough screen space on both sides (and below) to enlarge the UIImageView to fill the screen.
Many thanks.
Don't define the width of the image view in constraints. Define the padding that you want to the left and the right of the image view. Then auto layout can make your image view as large as possible without growing too large.
You need to add more constraints to tell the UIImageView which width between 200 and 600 to choose.
What do you expect the width of the superview to be? Assuming the superview's width will be between 200 and 600, you can tell the UIImageView to fill the width of the superview by removing both width constraints and adding the following constraints:
Leading Space to: Superview Equals: 0
Trailing Space to: Superview Equals: 0
Now the UIImageView knows to stretch so that there is no space between its leading (left) and trailing (right) sides and the superview's leading and trailing sides.
Alright, I've mixed some of the answers of you guys and came up with this:
Leading Space to: Superview Equals: 0 (priority 900)
Trailing Space to: Superview Equals: 0 (priority 900)
width <= 600 (priority 1000)
This fully works. It makes sense if you think about it. It is filling to both sides with the leading and trailing space, UNLESS it becomes greater than 600.
Thanks for the answers! Appreciated.
EDIT: This is still the same:
Align Center X to: Superview
100:133 Ratio to: RunwayGallery
Top Space to: ImageAbove, Equals 10

Which is the right way to calculate number of points of an image?

View.Bounds gave me values of (0.0, 0.0, 375.0, 667.0) to help calculate view dimensions. And CGImage, size of bitmap gave me pixel count of 490 - pixel width and 751 - pixel height. I don't understand why I get UIView bounds content size less than CGImage pixel width and height when scale factor gave me value of 2. But how can I calculate number of points from the dimensions ? How can I take out 375.0 and 667.0 and calculate ? Below code helped me to get view dimensions and scale factor.
let ourImage: UIImage? = imageView.image
let viewBounds = view.bounds
print("\(viewBounds)")
var scale: CGFloat = UIScreen.mainScreen().scale
print("\(scale)")
And this is the code I worked to receive pixel height and width of 490 * 751.
public init?(image: UIImage) {
guard let cgImage = image.CGImage else { return nil }
// Redraw image for correct pixel format
let colorSpace = CGColorSpaceCreateDeviceRGB()
var bitmapInfo: UInt32 = CGBitmapInfo.ByteOrder32Big.rawValue
bitmapInfo |= CGImageAlphaInfo.PremultipliedLast.rawValue & CGBitmapInfo.AlphaInfoMask.rawValue
width = Int(image.size.width)
height = Int(image.size.height)
.... }
Or can I use (pixelWidth * 2 + pixelHeight * 2) as to calculate number of points ? I need to calculate or fix number of points (n) to substitute in further equation of image segmentation using active contour method.
An image view does not resize to match the image property you set to it. Nor does the image you set resize to match the image view's frame.
Instead, the image view presents a representation of that image scaled to whatever size matches the image view (based on the rules you pick, aspect fit, aspect fill, scale to fill, others, etc).
So the actual size of the image is exactly whatever is returns from the image's size property.
To get the actual image's actual width, use:
image.size.width
Likewise, to get the actual image's actual height, use:
image.size.height

Explain Aspect Ratio and respective terms in iOS?

I want to understand Aspect Ratio.
Here, I am setting Aspect Ratio of an UIImageView.
These are options when I click this constraint.
How this constraint works and what are "PRESETS", Reverse Multiplier and Convert to Decimal.
Thanks.
Aspect ratio constraint is used to control the width and height of a view as per a aspect ratio that you set here. There are some standard presets such as 1:1 which means width will be equal to height. Similarly other presets calculates the dimensions based on a ratio
Reverse Multiplier is just used to reverse the ratio. E.g. 4:3 will be 3:4
Convert to decimal just represents the ratio as a decimal. E.g. 4:3 will be 1.33
If you want a view to always maintain an aspect ratio then you can use this constraint. In your case if its image view and you know the aspect ratio of the image that will be set then you can set that aspect ratio as the constraint so that the image is always sized according to the image that is set to that image view,
If you select Aspect Ratio for a single item, the width of the item is
used as the numerator for the ratio, and the height is used for the
denominator. If you select Aspect Ratio for multiple items, Auto
Layout chooses the width of one of the items for the numerator and the
height of another item for the denominator. To change the initial
aspect ratio, edit the Multiplier field of the Attributes inspector
for the constraint. To change which item to use for the width or the
height, use the First Item and Second Item pop-up menus in the
Attributes inspector.
Read more here
Constraints are something like equations in maths.
Ex:
let
X- known value (20)
Y- Unknown value (?)
m- multiplier (like 2 or 3 times)
C- constant (+3 or -3)
to find Y value we use this equation.
Y = m * X + C
Y = 2 * 20 + 3
Y = 43
Constraint equation:
First Object = (Multipler * Second Object ) + constant
width = (0.5 * Height) + 20
In Aspect Ratio condition
Note : one value should be fixed ( Height or width )
A) PRESETS
1)Width = 1 * Height
Width/ Height = 1/1 (1:1)
2)Width = 3/4 * height
Width / Height = 3 / 4 (3:4)
B) REVERSE MULTIPLIER
Before Reverse
Width = 1/2 * Height (1:2)
After Reverse
Width = 2/1 * Height (2:1)
C) CONVERT TO DECIMAL
Before conversion
Width = 1/2 * Height
After Conversion
Width = 0.5 * Height (0.5)

Trying to save image with high resolution(200 ppi)

In ios, by default while saving the image. It will renders 72 ppi image. But i want 200 or more dpi image.Resolution should not be change.
For example if i have 1200x1200 dimension image, while saving image it is 72 ppi but i wanted it as 200 or more ppi with same 1200x1200 dimensions
Code:
CGSize finalCanvasSize = CGSizeMake(1200*1200);
UIGraphicsBeginImageContextWithOptions(finalCanvasSize, NO, 0.0);
[canvasImage drawInRect:CGRectMake(0,0,finalCanvasSize.width,finalCanvasSize.height)];
resizedCanvasImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

Resources