rescale images in canvas after resetting it's ratio
After some research and some testing i found the following solution.
I changed the for loop that scales the objects to this:
for (var i = 0; i < objects.length; i++) {
var previousH = (((objects[i].getHeight() / canvas.getHeight()) * canvasHeightDefault) * PrevHeightRatio);
var previousW = (((objects[i].getWidth() / canvas.getWidth()) * canvasWidthDefault) * PrevWidthRatio);
console.log(previousH + " - " + previousW);
var multiplierSizeH = 1 / PrevHeightRatio;
var multiplierSizeW = 1 / PrevWidthRatio;
var onMaxH = objects[i].getHeight() * multiplierSizeH;
var onMaxW = objects[i].getWidth() * multiplierSizeW;
objects[i].height = onMaxH * HeightRatio;
objects[i].width = onMaxW * WidthRatio;
var multiplierW = 1 / PrevWidthRatio;
var multiplierH = 1 / PrevHeightRatio;
var originalStartX = objects[i].left * multiplierW;
var originalStartY = objects[i].top * multiplierH;
objects[i].left = originalStartX * WidthRatio;
objects[i].top = originalStartY * HeightRatio;
objects[i].setCoords();
}
This will keep track of the size before and after the scaling which fixes the issue.
Resize an image without distortion OpenCV
You may try below. The function will keep the aspect rate of the original image.
def image_resize(image, width = None, height = None, inter = cv2.INTER_AREA):
# initialize the dimensions of the image to be resized and
# grab the image size
dim = None
(h, w) = image.shape[:2]
# if both the width and height are None, then return the
# original image
if width is None and height is None:
return image
# check to see if the width is None
if width is None:
# calculate the ratio of the height and construct the
# dimensions
r = height / float(h)
dim = (int(w * r), height)
# otherwise, the height is None
else:
# calculate the ratio of the width and construct the
# dimensions
r = width / float(w)
dim = (width, int(h * r))
# resize the image
resized = cv2.resize(image, dim, interpolation = inter)
# return the resized image
return resized
Here is an example usage.
image = image_resize(image, height = 800)
Hope this helps.
How to save PNG file from NSImage (retina issues)
If you have an NSImage
and want to save it as an image file to the filesystem, you should never use lockFocus
! lockFocus
creates a new image which is determined for getting shown an the screen and nothing else. Therefore lockFocus
uses the properties of the screen: 72 dpi for normal screens and 144 dpi for retina screens. For what you want I propose the following code:
+ (void)saveImage:(NSImage *)image atPath:(NSString *)path {
CGImageRef cgRef = [image CGImageForProposedRect:NULL
context:nil
hints:nil];
NSBitmapImageRep *newRep = [[NSBitmapImageRep alloc] initWithCGImage:cgRef];
[newRep setSize:[image size]]; // if you want the same resolution
NSData *pngData = [newRep representationUsingType:NSPNGFileType properties:nil];
[pngData writeToFile:path atomically:YES];
[newRep autorelease];
}
How to scale an image down to fit inside a canvas, without distorting the image?
1) Determine which dimension is the one which exceeds the size of the window you want to put the image in. If both exceed the size calculate both factors and use the largest:
width_factor = imagewidth / allowable_width
height_factor = imageheight / allowable_height
2) Scale both the height and the width by the same factor. Eg. if the width is exceeded:
factor = imagewidth / allowable_width
Related Topics
What Does the Snapshot/Observer Code Do in Firebase
Swift MACos Popover Detect Change Dark Mode
How to Check for Value in Firebase That Is Held Under a Autoid Child
Swift Generic Func Cannot Convert Value of Type to Expected Argument Type
Need Clarification for Swift Type Properties
Show Datepicker Hourandminute on Print Statement Swiftui
Getting the Count of an Optional Array as a String, or Nil
Update Text in While Loop Swift
Sending Variables with a Segue
Initializer for Conditional Binding Must Have Optional Type, Not '[String:Any]'
How to Pass Closure as a Parameter in Perform(Selector, Withobject)
No Such Module Crashlytics - Pod Seems to Be Missing
Swift Conditional Conformance with Any
Swift How to Design Uiwebview Auto Resize Full Screen in Story Board
Tabview, Tabitem: Running Code on Selection or Adding an Ontapgesture