Search code examples
rimage-processingmagick-r-package

Processing multiple images with Magick (in R) with transformations


I need to automate some image transformations to do the following: - read in 16,000+ images that are short and wide, sizing is not the same. - rescale each image to 90 pixels high - crop 90 pixels over the width of the image, so multiple 90x90 crops over 1 image - then do it all over again for the next image - each 90x90 image needs to be saved as file-name_1.png, file-name_2.png and so on in sequential order

I've completed a test on 8 images, and using the magick package I was able to rescale and create multiple crops from each image manually. The problem is when I try to do multiple, I am able to resize the images easily but when it comes to saving them there is a problem.

# capture images, file paths in a list
img_list <- list.files("./orig_images", pattern = "\\.png$", full.names = TRUE)

# get all images in a list
all_images <- lapply(img_list, image_read)

# scale each image height - THIS DOESN'T WORK, GET NULL VALUE
scale_images <- 
  for (i in 1:length(all_images)) {
  scale_images(all_images[[i]], "x90")
    }

# all images added into one
all_images_joined <- image_join(all_images)

# scale images - THIS WORKS to scale, but problems later
all_images_scaled <- 
  image_scale(all_images_joined, "x90")

# Test whether a single file will be written or multiple files; 
# only writes one file (even if I 
for (i in 1:length(all_images_scaled)) {
  image_write(all_images_scaled[[i]], path = "filepath/new_cropimages/filename")
}

Ideally, I would scale the images with a for loop. That way I can save the scaled images to a directory. This didn't work - I don't get an error, but when I check the contents of the variable it is null. The image_join function puts them all together and scales the height to 90 (width is also scaled proportionately) but I can't write the separate images to directory. Also, the next piece is to crop each image across the width and save the new images file-name_1.png, and so on for every image 90x90, move over 90 pixels, crop 90x90, and so on. I chose magic because it was easy to individually scale and crop, but I'm open to other ideas (or learning how to make that package work). Thanks for any help.

Here are some images:

[Original Image, untransformed][1]
[Manual 90x90 crop][2]
[Another manual 90x90 crop, farther down the same image][3]


  [1]: https://i.sstatic.net/8ptXv.png
  [2]: https://i.sstatic.net/SF9pG.png
  [3]: https://i.sstatic.net/NyKxS.png

Solution

  • I don't speak R, but I hope to be able to help with the ImageMagick aspects and getting 16,000 images processed.

    As you are on a Mac, you can install 2 very useful packages very easily with homebrew, using:

    brew install imagemagick
    brew install parallel
    

    So, your original sentence image is 1850x105 pixels, you can see that in Terminal like this:

    magick identify sentence.png
    sentence.png PNG 1850x105 1850x105+0+0 8-bit Gray 256c 51626B 0.000u 0:00.000
    

    If you resize the height to 90px, leaving the width to follow proportionally, it will become 1586x90px:

    magick sentence.png -resize x90 info:
    sentence.png PNG 1586x90 1586x90+0+0 8-bit Gray 51626B 0.060u 0:00.006
    

    So, if you resize and then crop into 90px wide chunks:

    magick sentence.png -resize x90 -crop 90x chunk-%03d.png
    

    you will get 18 chunks, each 90 px wide except the last, as follows:

    -rw-r--r--  1 mark  staff  5648  6 Jun 08:07 chunk-000.png
    -rw-r--r--  1 mark  staff  5319  6 Jun 08:07 chunk-001.png
    -rw-r--r--  1 mark  staff  5870  6 Jun 08:07 chunk-002.png
    -rw-r--r--  1 mark  staff  6164  6 Jun 08:07 chunk-003.png
    -rw-r--r--  1 mark  staff  5001  6 Jun 08:07 chunk-004.png
    -rw-r--r--  1 mark  staff  6420  6 Jun 08:07 chunk-005.png
    -rw-r--r--  1 mark  staff  4726  6 Jun 08:07 chunk-006.png
    -rw-r--r--  1 mark  staff  5559  6 Jun 08:07 chunk-007.png
    -rw-r--r--  1 mark  staff  5053  6 Jun 08:07 chunk-008.png
    -rw-r--r--  1 mark  staff  4413  6 Jun 08:07 chunk-009.png
    -rw-r--r--  1 mark  staff  5960  6 Jun 08:07 chunk-010.png
    -rw-r--r--  1 mark  staff  5392  6 Jun 08:07 chunk-011.png
    -rw-r--r--  1 mark  staff  4280  6 Jun 08:07 chunk-012.png
    -rw-r--r--  1 mark  staff  5681  6 Jun 08:07 chunk-013.png
    -rw-r--r--  1 mark  staff  5395  6 Jun 08:07 chunk-014.png
    -rw-r--r--  1 mark  staff  5065  6 Jun 08:07 chunk-015.png
    -rw-r--r--  1 mark  staff  6322  6 Jun 08:07 chunk-016.png
    -rw-r--r--  1 mark  staff  4848  6 Jun 08:07 chunk-017.png
    

    Now, if you have 16,000 sentences to process, you can use GNU Parallel to get them all done in parallel and also get sensible names for all the files. Let's do a dry-run first so it actually doesn't do anything, but just shows you what it would do:

    parallel --dry-run magick {} -resize x90 -crop 90x {.}-%03d.png ::: sentence*
    

    Sample Output

    magick sentence1.png -resize x90 -crop 90x sentence1-%03d.png 
    magick sentence2.png -resize x90 -crop 90x sentence2-%03d.png
    magick sentence3.png -resize x90 -crop 90x sentence3-%03d.png
    

    That looks good, so remove the --dry-run and do it again and you get the following output for the three (identical copies) of your sentence I made:

    -rw-r--r--  1 mark  staff  5648  6 Jun 08:13 sentence1-000.png
    -rw-r--r--  1 mark  staff  5319  6 Jun 08:13 sentence1-001.png
    -rw-r--r--  1 mark  staff  5870  6 Jun 08:13 sentence1-002.png
    -rw-r--r--  1 mark  staff  6164  6 Jun 08:13 sentence1-003.png
    -rw-r--r--  1 mark  staff  5001  6 Jun 08:13 sentence1-004.png
    -rw-r--r--  1 mark  staff  6420  6 Jun 08:13 sentence1-005.png
    -rw-r--r--  1 mark  staff  4726  6 Jun 08:13 sentence1-006.png
    -rw-r--r--  1 mark  staff  5559  6 Jun 08:13 sentence1-007.png
    -rw-r--r--  1 mark  staff  5053  6 Jun 08:13 sentence1-008.png
    -rw-r--r--  1 mark  staff  4413  6 Jun 08:13 sentence1-009.png
    -rw-r--r--  1 mark  staff  5960  6 Jun 08:13 sentence1-010.png
    -rw-r--r--  1 mark  staff  5392  6 Jun 08:13 sentence1-011.png
    -rw-r--r--  1 mark  staff  4280  6 Jun 08:13 sentence1-012.png
    -rw-r--r--  1 mark  staff  5681  6 Jun 08:13 sentence1-013.png
    -rw-r--r--  1 mark  staff  5395  6 Jun 08:13 sentence1-014.png
    -rw-r--r--  1 mark  staff  5065  6 Jun 08:13 sentence1-015.png
    -rw-r--r--  1 mark  staff  6322  6 Jun 08:13 sentence1-016.png
    -rw-r--r--  1 mark  staff  4848  6 Jun 08:13 sentence1-017.png
    -rw-r--r--  1 mark  staff  5648  6 Jun 08:13 sentence2-000.png
    -rw-r--r--  1 mark  staff  5319  6 Jun 08:13 sentence2-001.png
    -rw-r--r--  1 mark  staff  5870  6 Jun 08:13 sentence2-002.png
    -rw-r--r--  1 mark  staff  6164  6 Jun 08:13 sentence2-003.png
    -rw-r--r--  1 mark  staff  5001  6 Jun 08:13 sentence2-004.png
    -rw-r--r--  1 mark  staff  6420  6 Jun 08:13 sentence2-005.png
    -rw-r--r--  1 mark  staff  4726  6 Jun 08:13 sentence2-006.png
    -rw-r--r--  1 mark  staff  5559  6 Jun 08:13 sentence2-007.png
    -rw-r--r--  1 mark  staff  5053  6 Jun 08:13 sentence2-008.png
    -rw-r--r--  1 mark  staff  4413  6 Jun 08:13 sentence2-009.png
    -rw-r--r--  1 mark  staff  5960  6 Jun 08:13 sentence2-010.png
    -rw-r--r--  1 mark  staff  5392  6 Jun 08:13 sentence2-011.png
    -rw-r--r--  1 mark  staff  4280  6 Jun 08:13 sentence2-012.png
    -rw-r--r--  1 mark  staff  5681  6 Jun 08:13 sentence2-013.png
    -rw-r--r--  1 mark  staff  5395  6 Jun 08:13 sentence2-014.png
    -rw-r--r--  1 mark  staff  5065  6 Jun 08:13 sentence2-015.png
    -rw-r--r--  1 mark  staff  6322  6 Jun 08:13 sentence2-016.png
    -rw-r--r--  1 mark  staff  4848  6 Jun 08:13 sentence2-017.png
    -rw-r--r--  1 mark  staff  5648  6 Jun 08:13 sentence3-000.png
    -rw-r--r--  1 mark  staff  5319  6 Jun 08:13 sentence3-001.png
    -rw-r--r--  1 mark  staff  5870  6 Jun 08:13 sentence3-002.png
    -rw-r--r--  1 mark  staff  6164  6 Jun 08:13 sentence3-003.png
    -rw-r--r--  1 mark  staff  5001  6 Jun 08:13 sentence3-004.png
    -rw-r--r--  1 mark  staff  6420  6 Jun 08:13 sentence3-005.png
    -rw-r--r--  1 mark  staff  4726  6 Jun 08:13 sentence3-006.png
    -rw-r--r--  1 mark  staff  5559  6 Jun 08:13 sentence3-007.png
    -rw-r--r--  1 mark  staff  5053  6 Jun 08:13 sentence3-008.png
    -rw-r--r--  1 mark  staff  4413  6 Jun 08:13 sentence3-009.png
    -rw-r--r--  1 mark  staff  5960  6 Jun 08:13 sentence3-010.png
    -rw-r--r--  1 mark  staff  5392  6 Jun 08:13 sentence3-011.png
    -rw-r--r--  1 mark  staff  4280  6 Jun 08:13 sentence3-012.png
    -rw-r--r--  1 mark  staff  5681  6 Jun 08:13 sentence3-013.png
    -rw-r--r--  1 mark  staff  5395  6 Jun 08:13 sentence3-014.png
    -rw-r--r--  1 mark  staff  5065  6 Jun 08:13 sentence3-015.png
    -rw-r--r--  1 mark  staff  6322  6 Jun 08:13 sentence3-016.png
    -rw-r--r--  1 mark  staff  4848  6 Jun 08:13 sentence3-017.png
    

    A word of explanation about the parameters to parallel:

    • {} refers to "the current file"
    • {.} refers to "the current file without its extension"
    • ::: separates the parameters meant for parallel from those meant for your magick command

    One note of warning, PNG images can "remember" where they came from which can be useful, or very annoying. If you look at the last chunk from above you will see it is 56x90, but that following that, it "remembers" it came from a canvas 1586x90 at offset 1530,0:

    identify sentence3-017.png 
    sentence3-017.png PNG 56x90 1586x90+1530+0 8-bit Gray 256c 4848B 0.000u 0:00.000
    

    This can sometimes upset subsequent processing which is annoying, or sometimes be very useful in re-assembling images that have been chopped up! If you want to remove it, you need to repage, so the command above becomes:

    magick input.png -resize x90 -crop 90x +repage output.png