python 加载图像剪辑并读取像素

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python 加载图像剪辑并读取像素相关的知识,希望对你有一定的参考价值。

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)

以上是关于python 加载图像剪辑并读取像素的主要内容,如果未能解决你的问题,请参考以下文章

Python - 删除源自图像边界的黑色像素

UIButton 的剪辑图像

剪辑属性 CSS 值

为啥用Python的openCV读取图片与PIL读取的图片像素值会不一样?

在 python 中使用 matplotlib 保存图像

CSS 剪辑 - 精灵图像