编写脚本来复制特定尺寸的图像 [英] Writing a script to copy images of certain dimensions

查看:54
本文介绍了编写脚本来复制特定尺寸的图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要做的是通过照片目录(即 C:\user\userid\Pictures)运行脚本,包括子目录,并复制大于 LxH 尺寸(即 1920x1080)的那些到另一个文件夹(即 C:\user\userid\Destination).这将在 Windows 机器上运行,其主文件夹中有超过一百万张照片和超过 300 个子目录.速度对我来说不是问题(任何事情都比手动浏览每个子目录要快),所以即使是需要几天时间才能完成的事情也绰绰有余.

What I'm looking to do is have a script run through a directory of photos (i.e. C:\user\userid\Pictures), including sub-directories, and copy ones that are greater than LxH dimension (i.e. 1920x1080) into another folder (i.e. C:\user\userid\Destination). This is going to be run on a windows machine with over one million photos and over 300 sub-directories in its main folder. Speed isn't an issue to me (anything is faster than going through each sub-directory by hand), so even something that takes literal days to complete this is more than enough.

所以算法可能看起来像这样:

So the algorithm may look something like this:

Function findLargeImages(curr, dest):
    For each file/folder in curr:
        if file:
            if file is image:
                if (image.width >= ####) AND (image.height >= ####)
                    copy image to dest
        else if folder:
            findImage(folder, dest)
    return (the sound of happiness)

但我可能完全忽略了使用脚本语言的意义.

But I could be completely missing the point of using a scripting language.

我已经看到 Powershell 有一个 Copy-ItemGet-ChildItem,但我不知道如何使用它们,因为我对 Powershell 的了解是它是蓝色的并替换"了命令行.我缺乏安全遍历文件系统和获取某种属性来确定图像高度/宽度的知识.

I've seen that Powershell has a Copy-Item and Get-ChildItem, but I wouldn't know how to use those, since my knowledge of Powershell is that it's blue and "replaced" the command line. I lack the knowledge to both safely traverse the file system, and get some sort of property to determine the image height/width.

Powershell 是我开始研究它时遇到的第一件事,但如果它可以在 Windows 上运行,那么我会非常高兴.如果需要,我可以轻松安装大多数其他语言并运行该代码.如果已经有一些软件可以做到这一点,那么您的 Google-fu 远比我的要好.

Powershell is the first thing I came across when I began looking into this, but if it can be run on Windows, then I'll be more than happy. I could easily install most other languages and run that code if needed. If there is some software out there to do this already, then your Google-fu is far greater than mine.

万一我丢失了登录信息,非常感谢任何提前提供帮助的人!

In case I manage to lose my login info, a HUGE thanks to anyone who helps in advance!

推荐答案

您可以使用 .NET System.Drawing.Image 类:

You can do this using the .NET System.Drawing.Image class:

$source      = 'C:\user\userid\Pictures'
$destination = 'C:\user\userid\Destination'
$maxWidth    = 1920
$maxHeight   = 1080

# if the destination path does not exist, create it
if (!(Test-Path -Path $destination -PathType Container)) {
    New-Item -Path $destination -ItemType Directory | Out-Null
}

# Add System.Drawing assembly
Add-Type -AssemblyName System.Drawing

Get-ChildItem -Path $source -File -Recurse | ForEach-Object {
    # capture the full filename so we can use it in the catch block
    $fileName = $_.FullName
    # Open image file
    try {
        $img = [System.Drawing.Image]::FromFile($fileName)
        # use '-ge'  if you want to copy files with a width and/or height Greater Or Equal To the max dimensions
        # use '-and' if you want to copy files where both the Width and the Height exceed the max dimensions
        if ($img.Width -gt $maxWidth -or $img.Height -gt $maxHeight) {
            $_ | Copy-Item -Destination $destination -Force
        }
    }
    catch {
        Write-Warning "Could not open file '$fileName'. Not an image file?"
    }
}

这篇关于编写脚本来复制特定尺寸的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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