[英]Wand Outputs Too Many Images
因此,我對 Python 總體而言相對缺乏經驗,並且是 ImageMagick 和 Wand 的新手,但是,到目前為止,這些工具完成了我所需的 95%,這意味着從長遠來看,我為我正在做的項目節省了大量時間工作。
我只是取一個圖像文件夾,將它們從 .tiff 轉換為 .png,然后將它們保存到一個子文件夾中。 看起來很簡單吧? 下面的代碼正在運行:
# 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)
一般來說,代碼運行良好,輸出我的輸入文件的 .png 版本,並去除了白色背景。
但是,我拍攝了 80 張圖像,輸出總共 712 張圖像。 Wand 有什么特別的原因導致了這種情況,還是我在 python 中的循環?
我的 .tiff 文件是秘密的多頁文件,我得到了所有的頁面,而不僅僅是第一個。 我通過以更好的支持 .png 格式導出我的圖像意外地解決了這個問題(我的轉儲圖像不再有背景被剝離的額外好處),但這非常有幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.