python开发photoshop将图片导出为tiff
python开发photoshop将图片导出为tiff
# -*- coding:utf-8 -*-
# @Time : 2022/5/7 15:50
# @Author : xuyongsai
# @Email : 1815222521@qq.com
# @File : PhotoShop.py
# @Software: PyCharm
# @Version : 1.0
import os
import sys
reload(sys)
sys.setdefaultencoding('utf8')
from comtypes.client import CreateObject
class pdfConverter:
def __init__(self):
# word文档转化为pdf文档时使用的格式为17
# self.wdFormatPDF = 17
# self.wdToPDF = CreateObject("Word.Application")
# ppt文档转化为pdf文档时使用的格式为32
# self.pptFormatPDF = 32
# self.pptToPDF = CreateObject("Powerpoint.Application")
# self.pptToPDF.Visible = 1
# ppt文档转化为pdf文档时使用的格式为32
self.jpgToTiff = CreateObject("Photoshop.Application")
# def wd_to_pdf(self, folder):
# # 获取指定目录下面的所有文件
# files = os.listdir(folder)
# # 获取word类型的文件放到一个列表里面
# wdfiles = [f for f in files if f.endswith((".doc", ".docx"))]
# for wdfile in wdfiles:
# # 将word文件放到指定的路径下面
# wdPath = os.path.join(folder, wdfile)
# # 设置将要存放pdf文件的路径
# pdfPath = wdPath
# # 判断是否已经存在对应的pdf文件,如果不存在就加入到存放pdf的路径内
# if pdfPath[-3:] != 'pdf':
# pdfPath = pdfPath + ".pdf"
# # 将word文档转化为pdf文件,先打开word所在路径文件,然后在处理后保存pdf文件,最后关闭
# pdfCreate = self.wdToPDF.Documents.Open(wdPath)
# pdfCreate.SaveAs(pdfPath, self.wdFormatPDF)
# pdfCreate.Close()
#
# def ppt_to_pdf(self, folder):
# files = os.listdir(folder)
# pptfiles = [f for f in files if f.endswith((".ppt", ".pptx"))]
# for pptfile in pptfiles:
# pptPath = os.path.join(folder, pptfile)
# pdfPath = pptPath
# if pdfPath[-3:] != 'pdf':
# pdfPath = pdfPath + ".pdf"
# pdfCreate = self.pptToPDF.Presentations.Open(pptPath)
# pdfCreate.SaveAs(pdfPath, self.pptFormatPDF)
# pdfCreate.Close()
def jpg_to_tiff(self, folder):
try:
listAllFile = []
for root, dirs, files in os.walk(folder, topdown=False):
for name in files:
if name.endswith(".jpg"):
print(os.path.join(root, name))
listAllFile.append(os.path.join(root, name))
for pptfile in listAllFile:
pdfPath = os.path.splitext(pptfile)[0]
if pdfPath[-3:] != 'tiff':
pdfPath = pdfPath + ".tiff"
tiffSaveOptions = CreateObject("Photoshop.TiffSaveOptions")
pdfCreate = self.jpgToTiff.Open(pptfile)
pdfCreate.SaveAs(pdfPath, tiffSaveOptions)
pdfCreate.Close()
print ("seccess")
except:
print ("error")
def addAllTiff(self, folderPath , SavePath):
try:
#合并图片的最大宽度作为合并文档的宽度
maxWidth = 0
#合并图片的高度和作为合并文档的高度
totalHeight = 0
#遍历文件夹下所有的文件
listAllFile = []
for root, dirs, files in os.walk(folderPath, topdown=False):
for name in files:
if name.endswith(".tif"):
print(os.path.join(root, name))
listAllFile.append(os.path.join(root, name))
for pptfile in listAllFile:
#filepath = os.path.join(folderPath, pptfile)
doc = self.jpgToTiff.Open(pptfile)
totalHeight += doc.height
if doc.width > maxWidth:
maxWidth = doc.width
doc.close()
#创建一个新的文档保存合并之后的图片
newDoc = self.jpgToTiff.documents.add(maxWidth, totalHeight, 72, "mytiff")
nextTop = 0
for pptfile in listAllFile:
doc1 = self.jpgToTiff.Open(pptfile)
#复制打开的图层
doc1.activeLayer.copy()
#设置新建的文档作为激活文档
self.jpgToTiff.activeDocument = newDoc
#粘贴刚才拷贝的图层到激活的文档
self.jpgToTiff.activeDocument.paste()
#获取激活文档的图层边界
bounds = self.jpgToTiff.activeDocument.activeLayer.bounds
if (nextTop == 0):
self.jpgToTiff.activeDocument.activeLayer.translate(0, 0 - bounds[1])
else:
self.jpgToTiff.activeDocument.activeLayer.translate(0, nextTop - bounds[1])
nextTop += bounds[3] - bounds[1]
doc1.close()
#创建保存Tiff的属性对象
tiffSaveoptions = CreateObject("Photoshop.TiffSaveOptions")
newDoc.SaveAs(SavePath, tiffSaveoptions)
print ("seccess")
newDoc.Close()
del listAllFile
except:
print ("error")
if __name__ == "__main__":
converter = pdfConverter()
#converter.jpg_to_tiff((r"C:\Users\Administrator\Desktop\ee").decode("utf-8"))
converter.addAllTiff((r"C:\Users\Administrator\Desktop\ee").encode("utf-8"),r"C:\Users\Administrator\Desktop\er\q.tif")
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· Vue3状态管理终极指南:Pinia保姆级教程