如何通过以新名称保存调整大小的版本来调整图像大小?

时间:2015-01-11 01:48:32

标签: linux bash resize imagemagick-convert

感谢您加入这个令人惊叹的社区。
在这里,我一直在与我的剧本作斗争,看起来我需要一个提示。

我有以下文件结构:

  

Mother_Directory /
  ./child1_Directory
    file1.jpg
    file2.JPG
    file3.jpg
  ./child2_Directory
    file1.jpg
    file2.JPG
    file3.jpg
  ./child3_Directory
    file1.jpg
    file2.JPG
    file3.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/
但没有运气(即使没有名称更改逻辑)。我倾向于相信我想要达到的目标是微不足道的,但我不知道在哪里寻找暗示。有一个简单的方法吗?

2 个答案:

答案 0 :(得分:0)

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

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

答案 1 :(得分:0)

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

第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专家,请建议一种更有效的方法。再次感谢。