Combine Two Images into One New Image

How to combine two UIImages into a single Image in Swift?

You are drawing both images into the same rect. You also should not use force-unwrapping. That causes your app to crash if anything goes wrong.

There are also various other small mistakes.

Change your function like this:

// Return an Optional so we can return nil if something goes wrong
func combine(bottomImage: Data, topImage: Data) -> UIImage? {

// Use a guard statement to make sure
// the data can be converted to images
guard
let bottomImage = UIImage(data: bottomImage),
let topImage = UIImage(data: topImage) else {
return nil
}
// Use a width wide enough for the widest image
let width = max(bottomImage.size.width, topImage.size.width)

// Make the height tall enough to stack the images on top of each other.
let size = CGSize(width: width, height: bottomImage.size.height + topImage.size.height)
UIGraphicsBeginImageContext(size)
let bottomRect = CGRect(
x: 0,
y: 0,
width: bottomImage.size.width,
height: bottomImage.size.height)

// Position the bottom image under the top image.
let topRect = CGRect(
x: 0,
y: bottomImage.size.height,
width: topImage.size.width,
height: topImage.size.height)

bottomImage.draw(in: bottomRect)

topImage!.draw(in: topRect)

let newImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return newImage
}

(And you should really be using a UIGraphicsImageRenderer rather than than calling UIGraphicsBeginImageContext()/ UIGraphicsEndImageContext().)

Edit:

Note that if the 2 images are different widths the above code will leave a "dead space" on the right of the narrower image. You could also make the code center the narrower image, or scale it up to be the same width. (If you do scale it up I suggest scaling it up in both dimensions to preserve the original aspect ratio. Otherwise it will look stretched and unnatural.)

Merging Two Images into one Image

If you want to create a side-by-side merged image you will need to create a result bitmap with 2 times the width of first image, or, more scalably, the sum of the widths of the images:

Currently, you are creating a result image with width firstImage.getWidth(). They will clearly overlap or be off the canvas.

Also, you will need to place the second image at x == firstImage.getWidth()

Check out this code (untested):

private Bitmap createSingleImageFromMultipleImages(Bitmap firstImage, Bitmap secondImage) {
Bitmap result = Bitmap.createBitmap(firstImage.getWidth() + secondImage.getWidth(), firstImage.getHeight(), firstImage.getConfig());
Canvas canvas = new Canvas(result);
canvas.drawBitmap(firstImage, 0f, 0f, null);
canvas.drawBitmap(secondImage, firstImage.getWidth(), 0f, null);
return result;
}

Bitmap mergedImages = createSingleImageFromMultipleImages(firstImage, secondImage);

im.setImageBitmap(mergedImages);

Combine two single color images into one with CLI (imagemagick?)

Thanks to answers I found elsewhere on transparency, and above here from Bruce on 'composite', I ended up doing:

convert screen-output-red.png -fuzz 30% -transparent white -fill red -opaque black trans.png
composite trans.png screen-output.png combo.png

Haven't figured out if I could do it in one step, but this is good enough for now.

Combining two image label to one label

You can clip the values of your segmentation mask to the range [0, 1] (instead of [0, 2]): After clipping, all pixels with label > 0 (non-background) will have label 1.

Merge two Mat images into one

There is several way to do this, but the best way I found is to use cv::hconcat(mat1, mat2, dst) for horizontal merge orcv::vconcat(mat1, mat2, dst) for vertical.

Don't forget to take care of empty matrix merge case !



Related Topics



Leave a reply



Submit