使用Magick(R)处理变换处理多个图像 [英] Processing multiple images with Magick (in R) with transformations

查看:97
本文介绍了使用Magick(R)处理变换处理多个图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要自动执行一些图像转换以执行以下操作: -读取16,000多个短而宽的图像,尺寸不一样. -将每个图像重新缩放到90像素高 -在图片的整个宽度上裁剪90像素,因此在1张图片上进行90x90的多个裁剪-然后针对下一张图片再次进行全部裁剪 -每个90x90图像都需要按顺序保存为file-name_1.png,file-name_2.png等

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

我已经完成了对8张图像的测试,并且使用magick包,我能够手动缩放比例并从每个图像创建多个作物.问题是当我尝试执行多个操作时,我能够轻松调整图像的大小,但是在保存图像时会出现问题.

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")
}

理想情况下,我将使用for循环缩放图像.这样,我可以将缩放后的图像保存到目录中.这没有用-我没有收到错误,但是当我检查变量的内容时,它为null. image_join函数将它们放在一起并将高度缩放到90(宽度也按比例缩放),但是我无法将单独的图像写入目录.另外,下一块是在整个宽度上裁剪每个图像,并保存新图像文件名_1.png,依此类推,对于每个图像90x90,移动90像素以上,裁剪90x90,依此类推.我选择魔术是因为它易于单独缩放和裁剪,但是我对其他想法持开放态度(或学习如何使该程序包起作用).感谢您的帮助.

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.

以下是一些图片:

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


  [1]: https://i.stack.imgur.com/8ptXv.png
  [2]: https://i.stack.imgur.com/SF9pG.png
  [3]: https://i.stack.imgur.com/NyKxS.png

推荐答案

我不会讲R,但是我希望能够在ImageMagick方面提供帮助并处理16,000张图像.

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

就像在Mac上一样,您可以使用 homebrew 来轻松安装2个非常有用的软件包,方法是:

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

brew install imagemagick
brew install parallel

因此,您的原始句子图像为1850x105像素,您可以在终端中看到这样的图像:

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

如果将高度调整为90px,而宽度按比例跟随,则它将变为1586x90px:

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

因此,如果您调整大小然后裁切成90像素宽的块:

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

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

您将获得18个块,除最后一个块外,每个块宽90像素,如下所示:

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


现在,如果您要处理16,000个句子,则可以使用 GNU Parallel 并行完成所有操作,并获得所有文件的明智名称.让我们先进行试运行,以便它实际上什么也不做,而只是向您显示它会做什么:


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*

示例输出

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

这看起来不错,因此删除--dry-run并再次执行,您得到的三句话(相同副本)的输出如下:

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

parallel的参数的解释:

  • {}引用当前文件"
  • {.}引用当前文件,没有扩展名"
  • :::将用于parallel的参数与用于magick命令的参数分开
  • {} 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

一个警告提示,PNG图像可以记住" ,它们从哪里来可能有用或非常烦人.如果您从上方看最后一个块,您将看到它是56x90,但是紧随其后的是记住" ,它来自画布1586x90,偏移量为1530,0:

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 

这篇关于使用Magick(R)处理变换处理多个图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆