Showing posts with label krita. Show all posts
Showing posts with label krita. Show all posts

Thursday, 21 April 2022

Krita python export png per layer

 

寫這個工具是因為專案上有需要批次輸出圖層檔案
因為Krita 免費,又可以用Python撰寫
花了一些時間找了一些教學
總總約花了半天,寫出這樣的工具。

執行位置: 工具>指令稿>指令稿工具






基本用途:
依開始的圖檔位置,自動建立一個存放輸出圖片的資料夾,並依圖層名層為檔案名稱。
目前每處理一個圖層都要按一次確定,因為管方文件好像有問題無法使用,所以也不確定要如何解決,但比起要手動一張一張輸出圖片,還是要方便跟輕鬆多了吧。

Python code: 下載位置

 '''  
 @Jiapei Lu 2022  
 This tool is to export png per layer with baselayer, and make a folder for store all those exported pics.  
 It applys the name of the layer as a filename automatically.  
 Exported folder will be created under where the opened file is.  
 '''  
 from krita import *  
 import os  
 export_png_folder_name = 'export_png' # you can change the folder name here.  
 #Krita.instance().action('python_scripter').trigger()  
 doc = Krita.instance().activeDocument()#access to current file  
 doc_dir = os.path.dirname( doc.fileName( ))#get the file path  
 exportDir = os.path.join( doc_dir, export_png_folder_name)# building the path for exporting  
 #If folder doesn't exist then create the folder for export pics  
 if not os.path.isdir( exportDir):  
   os.makedirs( exportDir)  
 batchNodes = []#this for containing all the layer nodes  
 #First: Go over all layer turn off it's visible except basic layer.  
 for node in doc.topLevelNodes()[ 1:] :  
     node.setVisible( 0)   
     batchNodes.append( node )  
 #Second: Trun on the visible of the layers one by one then exporting it.  
 for node in batchNodes:  
   node.setVisible( 1)  
   doc.refreshProjection() #This is supper imported to refresh view. Otherwise the exported pic could be wrong.  
   info = InfoObject()# InfoObject is a dictionary with specific export options  
   info.setProperty( "alpha", True)  
   info.setProperty( "compression", 9)  
   info.setProperty( "forceSRGB", False)  
   info.setProperty( "indexed", False)  
   info.setProperty( "interlaced", False)  
   info.setProperty( "saveSRGBProfile", False)  
   info.setProperty( "transparencyFillcolor", [0,0,0])     
   path = os.path.join(exportDir, node.name() + ".png")  
   #node.save(path, doc.resolution(), doc.resolution(), info)  
   doc.setActiveNode( node)  
   doc.exportImage( path, info)    
   node.setVisible( 0)  
   doc.refreshProjection( )   
 Application.setBatchmode( False)  
 QMessageBox.information( Application.activeWindow().qwindow(), "Done and Done", "All done!")