Skip to content

Instantly share code, notes, and snippets.

@JokerMartini
Last active July 5, 2017 14:19
Show Gist options
  • Save JokerMartini/3d1d227dbdad608bb3a3 to your computer and use it in GitHub Desktop.
Save JokerMartini/3d1d227dbdad608bb3a3 to your computer and use it in GitHub Desktop.
Nuke: This snippet of code takes all read nodes within a given Nuke comp and resizes the images and then saves them out to either png or jpeg.
#import a python module so we can do the search&replace stuff
import re
import os
#select all nodes in the root graph
nuke.selectAll()
############# variables
writeNodeList = [];
imgType = "png" # use either 'png' / 'jpeg'
resizeType = "percent" # use either 'pixel' / 'percent'
sizePercent = .5 # use value between '0.1 - 10.0'
sizePixel = 1024 # only supports square images in this mode
#loop through all selected read nodes to
for n in nuke.selectedNodes('Read'):
#get the full filepath
filepath = n.knob('file').value()
#replace extension with .jpg
#(root, ext) = os.path.splitext(filepath)
root = os.path.splitext(filepath)[0]
#############
# Reformt
# percentage: uses a value between 0 - 10 to resize image
# pixel: uses a specified pixel value to control width/height of resized image
#############
sizeNode = nuke.createNode('Reformat')
sizeNode.setInput(0,n)
if resizeType == "percent":
sizeNode["type"].setValue("scale")
sizeNode["scale"].setValue(sizePercent)
if resizeType == "pixel":
sizeNode["type"].setValue("to box")
sizeNode["box_width"].setValue(sizePixel)
sizeNode["box_height"].setValue(sizePixel)
sizeNode["box_fixed"].setValue(False)
sizeNode["resize"].setValue("fit")
sizeNode["black_outside"].setValue(True)
#############
# Save Image Output
# jpeg: saves a jpeg out with highest qualit
# png: saves our png with lossless compression
#############
writeNode = nuke.createNode('Write')
writeNode.setInput(0,sizeNode)
if imgType == "jpeg":
newFilepath = root + ".jpeg"
writeNode.knob('file').fromUserText(newFilepath)
writeNode["_jpeg_quality"].setValue(1.0)
writeNode["channels"].setValue("rgb")
writeNode["colorspace"].setValue("sRGB")
writeNodeList.append(writeNode);
if imgType == "png":
newFilepath = root + ".png"
writeNode.knob('file').fromUserText(newFilepath)
writeNode["datatype"].setValue("8_bit")
writeNodeList.append(writeNode);
for n in writeNodeList:
nuke.execute(n, 1, 1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment