简体   繁体   English

魔杖输出太多图像

[英]Wand Outputs Too Many Images

So I'm relatively inexperienced with python in general, and brand new to ImageMagick and Wand, however, the tools are doing 95% of what I need so far which means a LOT of time saved in the long term for a project I'm working on.因此,我对 Python 总体而言相对缺乏经验,并且是 ImageMagick 和 Wand 的新手,但是,到目前为止,这些工具完成了我所需的 95%,这意味着从长远来看,我为我正在做的项目节省了大量时间工作。

I am simply taking a folder of images, converting them from .tiff to .png, then saving them to a subfolder.我只是取一个图像文件夹,将它们从 .tiff 转换为 .png,然后将它们保存到一个子文件夹中。 Seems simple, right?看起来很简单吧? And the code below is working:下面的代码正在运行:

# Import os functions for setting path and getting files from directory
import os

# Import Wand functions to get the image and the required colors to convert
from wand.image import Image
from wand.color import Color

# Set the path where the images are being held
sourcePath = '/somePath/'
destinationPath = '/somePath/batch/'

# Set the image extenion we want to process and convert to
sourceExt = 'tiff'
destinationExt = 'png'

# Use os to get all files held in that directory
files = os.listdir(sourcePath)

# Loop through every file
for file in files:

    # Get the current image file's name and extension
    filename, file_extension = os.path.splitext(sourcePath + file)
    print('Filename: ' + filename)
    print('File extension: ' + file_extension)

    updatedFilename = filename.replace(sourcePath, '')

    # Only process for images with the .tiff extension
    if file_extension == ('.' + sourceExt):

        with Image(filename=filename + file_extension) as img:
            img.format = 'png'
            with Color('#FFFFFF') as white:
                img.transparent_color(white, alpha=0.0)
            img.save(filename=destinationPath + updatedFilename + '.' + destinationExt)

In general, the code is working well, outputting .png versions of my input files with the white background stripped out.一般来说,代码运行良好,输出我的输入文件的 .png 版本,并去除了白色背景。

However, I am taking in 80 images and the output is a total of 712 images.但是,我拍摄了 80 张图像,输出总共 712 张图像。 Is there something with Wand specifically that is causing this, or is it my loop in python? Wand 有什么特别的原因导致了这种情况,还是我在 python 中的循环?

My .tiff files were secretly multi-page files, and I was getting all of the pages rather than just the first.我的 .tiff 文件是秘密的多页文件,我得到了所有的页面,而不仅仅是第一个。 I've resolved this issue accidentally by exporting my images in a better supported .png format (with the added benefit of my dumped images not having a background to be stripped out anymore) but this was very helpful.我通过以更好的支持 .png 格式导出我的图像意外地解决了这个问题(我的转储图像不再有背景被剥离的额外好处),但这非常有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM