Assembling two images with shift in vertical and horizontal direction

I want to assemble two gray scale images of 100*100 pixels.

• From image two 30 pixels at the left border should be deleted and the remaining image shoud me shifted 10 pixels down.

• After that image 1 and the modified image 2 should be assembled in row.

img1 = Image[Array[0.3 &, {100, 100}]]


img2 = Image[Array[0.8 &, {100, 100}]]


How can the following code be improved/replaced?

Instead of using ImageData (as I did it) I would like to know if a solution exists with image functions like ImageCompose or others.

xshift = 30;
yshift = 10;

img2Data = ImageData[img2];
img3Data = Array[0 &, {100, 100 - xshift}];

img3Data[[1 + yshift ;; 100, 1 ;; 100 - xshift]] =
img2Data[[1 ;; 100 - yshift, 1 + xshift ;; 100]];
assembledImage = ImageAssemble[{img1, Image[img3Data]}]


• What is wrong with the code? Does it not do what you want? What improvements are you looking for? – MarcoB Jul 25 '17 at 20:16
• @MarcoB: Instead of using ImageData (as I did it) I would like to know if a solution exists with image functions like ImageCompose or others, which would be much faster since I have to process thousands of image pairs this way (my original png images are of 1600*1200 pixels). – mrz Jul 26 '17 at 11:50
• @mrz Have a look at ImageTake in the documentation. This does what you want on an Image level. – Thies Heidecke Jul 26 '17 at 12:20

Clear[trim]
trim[image_, xshift_, yshift_] :=
ImageTrim[
image,
{{xshift + 1, yshift + 1}, ImageDimensions[image] + {-1, yshift - 1}},

This works using Image* functions, but on my system it is not faster than your approach, rather surprisingly. However, I am not intimately familiar with the Image* functions, so perhaps a better solution exists.
• Your solution is definitely faster than my code and a great improvement. For the comparison I used 100 gray scale png Images of 1600*1200 pixels, whereby xshift=95 and yshift=11. On my computer AbsoluteTiming of your solution gives about 50 sec and my code needs about 77 sec. – mrz Jul 27 '17 at 10:33