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")
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

梅里雪山GIS

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值