需要帮助完成批处理脚本?

时间:2021-05-18 23:22:15

I would appreciate any help how to start or anything.

我很感激任何帮助如何开始或任何事情。

I just found out that there is a tool 'ImageMagick' who can command-line Auto resize images. Based on that knowledge I think that is smarter to do the following:

我刚发现有一个工具'ImageMagick'可以命令行自动调整大小图像。基于这些知识,我认为做以下事情更聪明:

Folder structure:

  1. C:\TEST --> subfolders --> import, export
  2. C:\ TEST - >子文件夹 - >导入,导出

  3. import --> subfolders --> We do not know the exact name of the folder, it can be anything!
  4. import - >子文件夹 - >我们不知道文件夹的确切名称,它可以是任何东西!

  5. export --> subfolders --> files from 'import'
  6. 导出 - >子文件夹 - >'import'中的文件

When I put 'X' number of folders (each folder contains, a 'X' number of pictures) to 'import' I want to:

当我将'X'个文件夹(每个文件夹包含一个'X'个图片)放到'import'时,我想:

  1. Copy first 'X' folder in 'import' (and all images inside) to 'export' -> resize to 400x300px and rename as '1a'

    将'import'中的第一个'X'文件夹(以及里面的所有图像)复制到'export' - >调整大小为400x300px并重命名为'1a'

  2. Copy first 'X' folder in 'import' (and all images inside) to 'export' -> resize to 200x150px and rename as '1b'

    将'import'中的第一个'X'文件夹(以及里面的所有图像)复制到'export' - >调整大小为200x150px并重命名为'1b'

    *. Under the assumption that there are ten pictures copied in first 'X' folder in 'export'... It will look like this - 1a,1b, 2a,2b, 3a,3b ... 10a,10b

    *。假设在'export'中第一个'X'文件夹中有十张图片被复制......看起来像这样 - 1a,1b,2a,2b,3a,3b ...... 10a,10b

When finish the first 'X' folder...start again, until it reaches the end of 'X' folder in 'import'.

完成第一个'X'文件夹后...再次开始,直到它到'import'中的'X'文件夹结尾。


Example #: This is just an assumption, the names and numbers of files and folders we dont no!

示例#:这只是一个假设,文件和文件夹的名称和数量我们都没有!

Folder structure:

  1. C:\TEST --> subfolders --> import, export
  2. C:\ TEST - >子文件夹 - >导入,导出

  3. import --> subfolders --> f1, e7, DD --> we have three folders
  4. import - >子文件夹 - > f1,e7,DD - >我们有三个文件夹

  5. export --> subfolders --> f1, e7, DD

    export - >子文件夹 - > f1,e7,DD

    1. Resize all pictures in 'C:\TEST\import\f1' to 'C:\TEST\export\f1\1a'
    2. 将“C:\ TEST \ import \ f1”中的所有图片调整为“C:\ TEST \ export \ f1 \ 1a”

    3. Resize all pictures in 'C:\TEST\import\f1' to 'C:\TEST\export\f1\1b'

      将“C:\ TEST \ import \ f1”中的所有图片调整为“C:\ TEST \ export \ f1 \ 1b”

    4. Resize all pictures in 'C:\TEST\import\e7' to 'C:\TEST\export\e7\1a'

      将“C:\ TEST \ import \ e7”中的所有图片调整为“C:\ TEST \ export \ e7 \ 1a”

    5. Resize all pictures in 'C:\TEST\import\e7' to 'C:\TEST\export\e7\1b'

      将“C:\ TEST \ import \ e7”中的所有图片调整为“C:\ TEST \ export \ e7 \ 1b”

    6. Resize all pictures in 'C:\TEST\import\DD' to 'C:\TEST\export\DD\1a'

      将“C:\ TEST \ import \ DD”中的所有图片调整为“C:\ TEST \ export \ DD \ 1a”

    7. Resize all pictures in 'C:\TEST\import\DD' to 'C:\TEST\export\DD\1b'
    8. 将“C:\ TEST \ import \ DD”中的所有图片调整为“C:\ TEST \ export \ DD \ 1b”


ImageMagick syntax for resize in command-line:

用于在命令行中调整大小的ImageMagick语法:

mogrify -resize 400x300 source file

mogrify -resize 400x300源文件

mogrify -resize 400x300 C:\picture_name.jpg

mogrify -resize 400x300 C:\ picture_name.jpg

I found batch script on their website http://www.imagemagick.org/discourse-server/viewtopic.php?f=1&t=16486 maybe it can help.

我在他们的网站上找到了批处理脚本http://www.imagemagick.org/discourse-server/viewtopic.php?f=1&t=16486也许它可以提供帮助。

1 个解决方案

#1


0  

Sure. Here's how to write a batch script to do what you need done (in Python): Save it as runMogrify.py

当然。以下是编写批处理脚本以执行所需操作的方法(在Python中):将其另存为runMogrify.py

import os
import string
import fnmatch

source = "c:\\TEST\\import"
target = "c:\\TEST\\export"

def mogrify(parmSource, parmTarget):
    mkdirList = []
    copyList = []
    mogrifyList = []

    mkdirList.append('mkdir "' + parmTarget + '"')

    for dirpath, dirnames, filenames in os.walk (parmSource):
        # print dirpath, dirnames, filenames
        for (index,file) in enumerate(sorted(filenames)):
            if fnmatch.fnmatch(file.lower(), '*.jpg') or \
               fnmatch.fnmatch(file.lower(), '*.gif') or \
               fnmatch.fnmatch(file.lower(), '*.bmp'):
                sourceFDirPathFile = dirpath + "\\" + file

                targetFDirPath     = os.path.join (parmTarget, dirpath[1+len (parmSource):])
                targetADirPathFile = targetFDirPath + "\\" + str(index+1) + "a" + os.path.splitext(file)[1]
                targetBDirPathFile = targetFDirPath + "\\" + str(index+1) + "b" + os.path.splitext(file)[1]

                if 'mkdir "' + targetFDirPath + '"' not in mkdirList:
                    mkdirList.append('mkdir "' + targetFDirPath + '"')

                copyList.append('copy "' + sourceFDirPathFile + '" "' + targetADirPathFile + '"')
                copyList.append('copy "' + sourceFDirPathFile + '" "' + targetBDirPathFile + '"')

                mogrifyList.append('mogrify -resize 400x300 "' + targetADirPathFile + '"')
                mogrifyList.append('mogrify -resize 200x150 "' + targetBDirPathFile + '"')

    return mkdirList, copyList, mogrifyList

def main():
    mkdirList, copyList, mogrifyList = mogrify(source, target)

    f = open('RUNMOGRIFY.BAT', 'w')
    f.writelines( list("%s\n" % item for item in mkdirList  ) )
    f.writelines( list("%s\n" % item for item in copyList   ) )
    f.writelines( list("%s\n" % item for item in mogrifyList) )
    f.close()

if __name__=="__main__": main()

And, here's the resulting batch file (RUNMOGRIFY.BAT) after I ran the Python script above
on a test directory similar to yours:

并且,在我在类似于您的测试目录上运行上面的Python脚本之后,生成的批处理文件(RUNMOGRIFY.BAT):

mkdir "c:\TEST\export"
mkdir "c:\TEST\export\folder1"
mkdir "c:\TEST\export\folder2"
mkdir "c:\TEST\export\folder3"
copy "c:\TEST\import\folder1\a.jpg" "c:\TEST\export\folder1\1a.jpg"
copy "c:\TEST\import\folder1\a.jpg" "c:\TEST\export\folder1\1b.jpg"
copy "c:\TEST\import\folder1\b.jpg" "c:\TEST\export\folder1\2a.jpg"
copy "c:\TEST\import\folder1\b.jpg" "c:\TEST\export\folder1\2b.jpg"
copy "c:\TEST\import\folder1\c.jpg" "c:\TEST\export\folder1\3a.jpg"
copy "c:\TEST\import\folder1\c.jpg" "c:\TEST\export\folder1\3b.jpg"
copy "c:\TEST\import\folder2\a.jpg" "c:\TEST\export\folder2\1a.jpg"
copy "c:\TEST\import\folder2\a.jpg" "c:\TEST\export\folder2\1b.jpg"
copy "c:\TEST\import\folder2\b.jpg" "c:\TEST\export\folder2\2a.jpg"
copy "c:\TEST\import\folder2\b.jpg" "c:\TEST\export\folder2\2b.jpg"
copy "c:\TEST\import\folder2\c.jpg" "c:\TEST\export\folder2\3a.jpg"
copy "c:\TEST\import\folder2\c.jpg" "c:\TEST\export\folder2\3b.jpg"
copy "c:\TEST\import\folder3\a.jpg" "c:\TEST\export\folder3\1a.jpg"
copy "c:\TEST\import\folder3\a.jpg" "c:\TEST\export\folder3\1b.jpg"
copy "c:\TEST\import\folder3\b.jpg" "c:\TEST\export\folder3\2a.jpg"
copy "c:\TEST\import\folder3\b.jpg" "c:\TEST\export\folder3\2b.jpg"
copy "c:\TEST\import\folder3\c.jpg" "c:\TEST\export\folder3\3a.jpg"
copy "c:\TEST\import\folder3\c.jpg" "c:\TEST\export\folder3\3b.jpg"
mogrify -resize 400x300 "c:\TEST\export\folder1\1a.jpg"
mogrify -resize 200x150 "c:\TEST\export\folder1\1b.jpg"
mogrify -resize 400x300 "c:\TEST\export\folder1\2a.jpg"
mogrify -resize 200x150 "c:\TEST\export\folder1\2b.jpg"
mogrify -resize 400x300 "c:\TEST\export\folder1\3a.jpg"
mogrify -resize 200x150 "c:\TEST\export\folder1\3b.jpg"
mogrify -resize 400x300 "c:\TEST\export\folder2\1a.jpg"
mogrify -resize 200x150 "c:\TEST\export\folder2\1b.jpg"
mogrify -resize 400x300 "c:\TEST\export\folder2\2a.jpg"
mogrify -resize 200x150 "c:\TEST\export\folder2\2b.jpg"
mogrify -resize 400x300 "c:\TEST\export\folder2\3a.jpg"
mogrify -resize 200x150 "c:\TEST\export\folder2\3b.jpg"
mogrify -resize 400x300 "c:\TEST\export\folder3\1a.jpg"
mogrify -resize 200x150 "c:\TEST\export\folder3\1b.jpg"
mogrify -resize 400x300 "c:\TEST\export\folder3\2a.jpg"
mogrify -resize 200x150 "c:\TEST\export\folder3\2b.jpg"
mogrify -resize 400x300 "c:\TEST\export\folder3\3a.jpg"
mogrify -resize 200x150 "c:\TEST\export\folder3\3b.jpg"

#1


0  

Sure. Here's how to write a batch script to do what you need done (in Python): Save it as runMogrify.py

当然。以下是编写批处理脚本以执行所需操作的方法(在Python中):将其另存为runMogrify.py

import os
import string
import fnmatch

source = "c:\\TEST\\import"
target = "c:\\TEST\\export"

def mogrify(parmSource, parmTarget):
    mkdirList = []
    copyList = []
    mogrifyList = []

    mkdirList.append('mkdir "' + parmTarget + '"')

    for dirpath, dirnames, filenames in os.walk (parmSource):
        # print dirpath, dirnames, filenames
        for (index,file) in enumerate(sorted(filenames)):
            if fnmatch.fnmatch(file.lower(), '*.jpg') or \
               fnmatch.fnmatch(file.lower(), '*.gif') or \
               fnmatch.fnmatch(file.lower(), '*.bmp'):
                sourceFDirPathFile = dirpath + "\\" + file

                targetFDirPath     = os.path.join (parmTarget, dirpath[1+len (parmSource):])
                targetADirPathFile = targetFDirPath + "\\" + str(index+1) + "a" + os.path.splitext(file)[1]
                targetBDirPathFile = targetFDirPath + "\\" + str(index+1) + "b" + os.path.splitext(file)[1]

                if 'mkdir "' + targetFDirPath + '"' not in mkdirList:
                    mkdirList.append('mkdir "' + targetFDirPath + '"')

                copyList.append('copy "' + sourceFDirPathFile + '" "' + targetADirPathFile + '"')
                copyList.append('copy "' + sourceFDirPathFile + '" "' + targetBDirPathFile + '"')

                mogrifyList.append('mogrify -resize 400x300 "' + targetADirPathFile + '"')
                mogrifyList.append('mogrify -resize 200x150 "' + targetBDirPathFile + '"')

    return mkdirList, copyList, mogrifyList

def main():
    mkdirList, copyList, mogrifyList = mogrify(source, target)

    f = open('RUNMOGRIFY.BAT', 'w')
    f.writelines( list("%s\n" % item for item in mkdirList  ) )
    f.writelines( list("%s\n" % item for item in copyList   ) )
    f.writelines( list("%s\n" % item for item in mogrifyList) )
    f.close()

if __name__=="__main__": main()

And, here's the resulting batch file (RUNMOGRIFY.BAT) after I ran the Python script above
on a test directory similar to yours:

并且,在我在类似于您的测试目录上运行上面的Python脚本之后,生成的批处理文件(RUNMOGRIFY.BAT):

mkdir "c:\TEST\export"
mkdir "c:\TEST\export\folder1"
mkdir "c:\TEST\export\folder2"
mkdir "c:\TEST\export\folder3"
copy "c:\TEST\import\folder1\a.jpg" "c:\TEST\export\folder1\1a.jpg"
copy "c:\TEST\import\folder1\a.jpg" "c:\TEST\export\folder1\1b.jpg"
copy "c:\TEST\import\folder1\b.jpg" "c:\TEST\export\folder1\2a.jpg"
copy "c:\TEST\import\folder1\b.jpg" "c:\TEST\export\folder1\2b.jpg"
copy "c:\TEST\import\folder1\c.jpg" "c:\TEST\export\folder1\3a.jpg"
copy "c:\TEST\import\folder1\c.jpg" "c:\TEST\export\folder1\3b.jpg"
copy "c:\TEST\import\folder2\a.jpg" "c:\TEST\export\folder2\1a.jpg"
copy "c:\TEST\import\folder2\a.jpg" "c:\TEST\export\folder2\1b.jpg"
copy "c:\TEST\import\folder2\b.jpg" "c:\TEST\export\folder2\2a.jpg"
copy "c:\TEST\import\folder2\b.jpg" "c:\TEST\export\folder2\2b.jpg"
copy "c:\TEST\import\folder2\c.jpg" "c:\TEST\export\folder2\3a.jpg"
copy "c:\TEST\import\folder2\c.jpg" "c:\TEST\export\folder2\3b.jpg"
copy "c:\TEST\import\folder3\a.jpg" "c:\TEST\export\folder3\1a.jpg"
copy "c:\TEST\import\folder3\a.jpg" "c:\TEST\export\folder3\1b.jpg"
copy "c:\TEST\import\folder3\b.jpg" "c:\TEST\export\folder3\2a.jpg"
copy "c:\TEST\import\folder3\b.jpg" "c:\TEST\export\folder3\2b.jpg"
copy "c:\TEST\import\folder3\c.jpg" "c:\TEST\export\folder3\3a.jpg"
copy "c:\TEST\import\folder3\c.jpg" "c:\TEST\export\folder3\3b.jpg"
mogrify -resize 400x300 "c:\TEST\export\folder1\1a.jpg"
mogrify -resize 200x150 "c:\TEST\export\folder1\1b.jpg"
mogrify -resize 400x300 "c:\TEST\export\folder1\2a.jpg"
mogrify -resize 200x150 "c:\TEST\export\folder1\2b.jpg"
mogrify -resize 400x300 "c:\TEST\export\folder1\3a.jpg"
mogrify -resize 200x150 "c:\TEST\export\folder1\3b.jpg"
mogrify -resize 400x300 "c:\TEST\export\folder2\1a.jpg"
mogrify -resize 200x150 "c:\TEST\export\folder2\1b.jpg"
mogrify -resize 400x300 "c:\TEST\export\folder2\2a.jpg"
mogrify -resize 200x150 "c:\TEST\export\folder2\2b.jpg"
mogrify -resize 400x300 "c:\TEST\export\folder2\3a.jpg"
mogrify -resize 200x150 "c:\TEST\export\folder2\3b.jpg"
mogrify -resize 400x300 "c:\TEST\export\folder3\1a.jpg"
mogrify -resize 200x150 "c:\TEST\export\folder3\1b.jpg"
mogrify -resize 400x300 "c:\TEST\export\folder3\2a.jpg"
mogrify -resize 200x150 "c:\TEST\export\folder3\2b.jpg"
mogrify -resize 400x300 "c:\TEST\export\folder3\3a.jpg"
mogrify -resize 200x150 "c:\TEST\export\folder3\3b.jpg"