【问题标题】:How to resize an image by saving the resized version under a new name?如何通过以新名称保存调整大小的版本来调整图像大小?
【发布时间】:2015-01-11 01:48:32
【问题描述】:

感谢您加入这个了不起的社区。
在这里,我一直在与我的脚本作斗争,看起来我需要一个提示。

我的文件结构如下:

Mother_Directory/
./child1_Directory
文件1.jpg
文件2.JPG
文件3.jpg
./child2_Directory
文件1.jpg
文件2.JPG
文件3.jpg
./child3_Directory
文件1.jpg
文件2.JPG
文件3.jpg

现在我想使用imagemagick 的转换函数对子目录中的这些文件执行各种操作(比如调整大小),并将它们保存在Mother_Directory_2/(位于外部硬盘驱动器上)下新名称。
例如external_HD_path/Mother_Directory_2/child2_Directory/resized_file1.jpg

这是我的代码:

for f in `find . -name "*.*"`; do convert $f -resize 50% ../Mother_Directory_2/$f; done

这个应该把新文件保存在../Mother_Directory_2/
但没有运气(即使没有名称更改逻辑)。我倾向于相信我想要实现的目标是微不足道的,但我不知道在哪里寻找提示。有没有简单的方法来做到这一点?

【问题讨论】:

  • Mother_Directory_2下是否存在子目录?您能否提供一个问题描述:即究竟正在发生什么,它与您的预期有何不同?
  • 1.我可以通过在 Mother_D_2/ 中使用原始 Mother_D/ 2 中的名称执行 mkdir 来创建子文件夹。现在的问题是如何将调整大小的照片放到 Mother_d_2/ 下的相同命名文件夹中

标签: linux bash resize imagemagick-convert


【解决方案1】:

我认为问题在于您在目标目录中使用 $f 的方式。 $f 通常包含正在转换的文件的完整路径。相反,您可以尝试在脚本中使用“basename”。

for f in `find . -name "*.*"`; do base=$(basename "$f") ; convert $f -resize 50% ../Mother_Directory_2/$base; done

【讨论】:

  • 感谢您的回答。它解决了问题的第二部分,但是我如何将新文件放入正确的目录中,即 ./child1_Directory Mother_Directory_2/ ./child1_Directory resized_file1.jpg
  • 好的,可以像这样得到父名称 parentname="$(basename "$(dirname "$f")")" 会尝试让它工作
【解决方案2】:

鉴于很多人在这里搜索此解决方案,我想根据我经历过的所有 cmets 和帖子发布我设法编写的最简单的解决方案。

步骤 1. 在新的 Mother_Directory_2 中创建所有文件夹(从原始 Mother_Directory 中获取文件夹的名称)

for d in */ ; do mkdir ~/Desktop/Mother_Directory_2/$d; done

第 2 步。
2.1.使用
获取子目录名 parentname="$(basename "$(dirname "$f")")"
2.2.使用
获取文件名 base=$(basename "$f")
3.3 运行下面的脚本(cd Mother_Directory)

for f in `find . -name "*.*"`; do base=$(basename "$f") ; parentname="$(basename "$(dirname "$f")")"; convert -resize 1000x1000  ./$parentname/$base ../Mother_Directory_2/$parentname/resize_$base; done

希望这会有所帮助 - 如果您是 Bash 专家,请提出一种更有效的方法来做同样的事情。再次感谢。

【讨论】:

    猜你喜欢
    • 2013-07-26
    • 2011-10-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多