Skip to content

Instantly share code, notes, and snippets.

@ivogrig
Last active August 3, 2020 11:37
Show Gist options
  • Save ivogrig/33dfadb7273106371c4d to your computer and use it in GitHub Desktop.
Save ivogrig/33dfadb7273106371c4d to your computer and use it in GitHub Desktop.
Loading an image clip and reading a pixel
import lxu
import lxu.select
def loadImage(filepath, width, height):
'''
Loads an image as clip and returns it's image interface object.
'''
if not os.path.exists(filepath):
raise AttributeError("Path does not exist")
# Load image
sel_svc = lxu.select.SceneSelection()
current_scene = sel_svc.current()
scn_svc = lx.service.Scene()
chan = current_scene.Channels(lx.symbol.s_ACTIONLAYER_EDIT, 0.0)
cout = lx.object.ChannelWrite(chan)
vclip = current_scene.ItemAdd(scn_svc.ItemTypeLookup(lx.symbol.sITYPE_VIDEOSTILL))
idx = vclip.ChannelLookup(lx.symbol.sICHAN_VIDEOSTILL_FILENAME)
cout.String(vclip, idx, filepath)
# Get to the image object
scene = vclip.Context()
channels = lx.object.ChannelRead (scene.Channels(None, 0.0) )
channel = vclip.ChannelLookup ("imageStack")
filter = lx.object.ImageFilter (channels.ValueObj (vclip, channel))
image = lxu.object.Image (filter.Generate (width, height, None))
return image
def readPixel(image, x, y):
'''
Returns RGB or RGBA pixel values from an image
'''
# Has Alpha?
numComponents = image.Components()
imageType = None
if numComponents == 1:
imageType = lx.symbol.iIMV_GREY
elif numComponents == 3:
imageType = lx.symbol.iIMV_RGB
elif numComponents == 4:
imageType = lx.symbol.iIMV_RGBA
# Read pixel
storage = lx.object.storage('b', numComponents)
image.GetPixel(x, y, imageType, storage)
return storage.get()
img = loadImage('c:/temp/test.tga', 12, 12)
print readPixel(img, 0, 0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment