使用 python 使用 Photoshop 进行基本的 tiff 处理

发布于 2025-01-04 22:22:15 字数 818 浏览 0 评论 0原文

我需要编写一个执行以下操作的脚本:

# open a tiff
# get it's dpi, width, height and colorspace
# set the dpi, width, height and colorspace
# and then save the tiff out with no compression and no layers.

到目前为止,我已经得到:

from win32com.client.dynamic import Dispatch
ps = Dispatch( "Photoshop.Application" )

file_path = "C:\\Users\\me\\myImg.tif"
doc = ps.Open( file_path )

dpi     = doc.Resolution
width   = doc.Width             # in cm
height  = doc.Height            # in cm

# up to here the code works, but then I try
doc.Resolution = 72
ps.ResizeImage( 120 , 120 )
ps.PsColorSpaceType( 3 ) # psSRGB

ps.TiffSaveOptions.ImageCompression = 1 # psNoTIFFCompression
ps.TiffSaveOptions.Layers = False
ps.Save()

# and this last section fails

请帮助,任何想法、提示、灵魂将不胜感激:D

I need to write a script that does the following:

# open a tiff
# get it's dpi, width, height and colorspace
# set the dpi, width, height and colorspace
# and then save the tiff out with no compression and no layers.

So far I've gotten:

from win32com.client.dynamic import Dispatch
ps = Dispatch( "Photoshop.Application" )

file_path = "C:\\Users\\me\\myImg.tif"
doc = ps.Open( file_path )

dpi     = doc.Resolution
width   = doc.Width             # in cm
height  = doc.Height            # in cm

# up to here the code works, but then I try
doc.Resolution = 72
ps.ResizeImage( 120 , 120 )
ps.PsColorSpaceType( 3 ) # psSRGB

ps.TiffSaveOptions.ImageCompression = 1 # psNoTIFFCompression
ps.TiffSaveOptions.Layers = False
ps.Save()

# and this last section fails

Please help, any ideas, tips, soultions would be greatly appreciated :D

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

喜你已久 2025-01-11 22:22:15

经过大量的谷歌搜索和一些试验和错误,然后是更多的试验和错误,我设法想出了下面的代码。

希望这可以帮助别人。

代码

file_path = "C:\\Users\\me\\myImg.tif"
color_settings = "North America General Purpose 2"


from win32com.client.dynamic import Dispatch

ps_app = Dispatch( "Photoshop.Application" )

# set photoshop to use pixels as dimensions
ps_app.Preferences.RulerUnits = 1               # 'for PsUnits --> 1 (psPixels)         
ps_app.Preferences.TypeUnits = 1                # 'for PsTypeUnits --> 1 (psPixels)

doc = ps_app.Open( file_path )                  # Open a file and store open file as doc
dpi     = doc.Resolution
width   = doc.Width         
height  = doc.Height            

cor_res = 1024

ps_app.ChangeColorSettings( color_settings )
doc.ResizeImage( cor_res , cor_res , 72 )


options = Dispatch('Photoshop.TiffSaveOptions')
options.ImageCompression = 1                    # ps_appNoTIFFCompression
options.Layers = False                          # no layers


doc.SaveAs( file_path , options )               # Save with specified options
doc.Close( 2 )                                  # psDoNotSaveChanges

After a lot of googeling and some trial and error and then even more trial and error I've managed to come up with the code below.

Hope this can help someone else.

Code

file_path = "C:\\Users\\me\\myImg.tif"
color_settings = "North America General Purpose 2"


from win32com.client.dynamic import Dispatch

ps_app = Dispatch( "Photoshop.Application" )

# set photoshop to use pixels as dimensions
ps_app.Preferences.RulerUnits = 1               # 'for PsUnits --> 1 (psPixels)         
ps_app.Preferences.TypeUnits = 1                # 'for PsTypeUnits --> 1 (psPixels)

doc = ps_app.Open( file_path )                  # Open a file and store open file as doc
dpi     = doc.Resolution
width   = doc.Width         
height  = doc.Height            

cor_res = 1024

ps_app.ChangeColorSettings( color_settings )
doc.ResizeImage( cor_res , cor_res , 72 )


options = Dispatch('Photoshop.TiffSaveOptions')
options.ImageCompression = 1                    # ps_appNoTIFFCompression
options.Layers = False                          # no layers


doc.SaveAs( file_path , options )               # Save with specified options
doc.Close( 2 )                                  # psDoNotSaveChanges
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文