Search code examples
rmagick

Filling an image using magick package


This image should be filled using smaller images:

enter image description here

library(sp)
library(raster)
library(imager)


im <- load.image("white_rectangle.jpg") 
px <- im > 0.4 #Select pixels of the circle (i.e., those with high luminance)
coord <- where(px)

boundaries <- imager::boundary(px)
boundaries.xy <- where(boundaries)

x_coord <- boundaries.xy$x
y_coord <- boundaries.xy$y
xym <- cbind(x_coord, y_coord)

p = Polygon(xym)
ps = Polygons(list(p),1)
sps1 = SpatialPolygons(list(ps))
plot(sps1)

I am trying to use magick package in order to fill the previous image.

One of the ideas about to solve it, was trying to fill it with a cube of images like a matrix of 3x3.

bigdata <- 
image_read('https://jeroen.github.io/images/bigdata.jpg')
frink <- image_read("https://jeroen.github.io/images/frink.png")
logo <- image_read("https://jeroen.github.io/images/Rlogo.png")
img <- c(bigdata, logo, frink)
img <- image_scale(img, "300x300")
image_info(img)

new_i <- image_append(image_scale(img, "x200"))

My current code returns a vector of 3, I don't understand how to create a matrix of 3 x 3 images:

  image_append(new_i,new_i,new_i stack = TRUE)

Thank you for your guidance.


Solution

  • I am not sure how big your input images are, nor how you want them filled. But if you want them to just be appended side-by-side, then in Imagemagick command line, I would do:

    Input (repeated 3 times):

    enter image description here

    Your image to be filled is 630x380 after subtracting the border. I get that by

    convert img.jpg -fuzz 15% -format "%@" info:
    630x380+10+10
    


    So doing the appending and inserting into your image:

    convert img.jpg \( monet2.jpg monet2.jpg monet2.jpg +append -resize 630x380^ -gravity center -extent 630x380 \) -gravity center -compose over -composite result.jpg
    

    enter image description here

    Is this what you are trying to do?

    If you want a 3x3 grid of images, then adjust (resize and/or crop) the 3 images so that they are 1/3 of your background image in each dimensions The append 3 horizontally. The duplicated that 2 more times and append that set of 3 vertically. Then insert into your backgound image.

    convert img.jpg \( \( monet2.jpg monet2.jpg monet2.jpg -resize 210x127^ -gravity center -extent 210x127 +append \) -duplicate 2 -append \) -gravity center -compose over -composite result1.jpg
    


    enter image description here

    ADDITION:

    Another approach is to just tile out the image.

    convert img.jpg -resize 210x127^ -gravity center -extent 210x127 -write mpr:img +delete -size 630x380 tile:mpr:img result2.png
    


    enter image description here