I'm very new to R and appreciate any help or direction. I have 16000+ images that are short and wide, the sizes are not standard. I need to resize them all to 90 pixels high, maintaining width proportions as height is resized. Then, for each image I need to crop 90 pixels across the width of the image to the end. I need to save each file as something like file-name-1_1.png, file-name-1_2.png, file-name-1_3.png and so on.
I have figured out how to manually crop images but I'm having trouble getting the automation to work. So far I've gotten the image_read function to work on one image at a time only.
Below is what I've done so far, I've tested this and it works fine - just need advice on:
a) how to automate over a folder of 1600+ images,
b) how to handle the last images if they aren't 90 pixels wide (IF statement? ), and
c) how to save the files by adding a sequential number at the end of the file. The number should start over for each different file name:
image <- image_read("filepath/ file-name-1.png.png") image_scaled <- image_scale(image, "x90") # scaled height to 90 pixels, scales width proportionately # image_scaled is 90(h) x 1586(w), so there will be 17 full 90x90 crops, and 1 partial crop - maybe there is a way to do an IF statement just add white space to the right if the width is less than 90? image_cropped <- image_crop(image_scaled, "90x90+0") image_write(image_cropped, path = "filepath/new_crops/a01-000u-s00-01_1.png") image_cropped2 <- image_crop(image_scaled, "90x90+90") image_write(image_cropped2, path = "filepath/new_crops/a01-000u-s00-01_2.png") image_cropped3 <- image_crop(image_scaled, "90x90+180") image_write(image_cropped3, path = "filepath/new_crops/a01-000u-s00-01_3.png")