備忘錄_20160105(定位) 修改 回首頁

程式 2022-06-17 16:59:26 1655456366 100
在 blender 3 的 python 中,畫圖,複製其他圖片部分像素,存檔,讀取圖片檔案。

在 blender 3 的 python 中,畫圖,複製其他圖片部分像素,存檔,讀取圖片檔案。
import bpy
import numpy as np

def setPixel(oImg,iWidth,iHeight,x,y,r,g,b,a):
    iIdx=(y*iWidth+x)*4
    oImg.pixels[iIdx+0]=r # red
    oImg.pixels[iIdx+1]=g # green
    oImg.pixels[iIdx+2]=b # blue
    oImg.pixels[iIdx+3]=a # alpha

oData=bpy.data

iWidth=10
iHeight=10

oImgA=oData.images.new(name='picOutA',width=iWidth,height=iHeight)
oImgB=oData.images.new(name='picOutB',width=iWidth,height=iHeight)
#iNumPixels=len(oImg.pixels)

for i in range(iWidth):
    for j in range(iHeight):
        setPixel(oImgA,iWidth,iHeight,i,j,1,0,0,1)

for i in range(iWidth):
    for j in range(iHeight):
        setPixel(oImgB,iWidth,iHeight,i,j,0,1,0,1)

bufferA=np.reshape(oImgA.pixels,(iWidth,iHeight,4))
bufferB=np.reshape(oImgB.pixels,(iWidth,iHeight,4))

y1=5
y2=6
x1=0
x2=10
bufferB[y1:y2,x1:x2]=bufferA[y1:y2,x1:x2]

oImgB.pixels=np.reshape(bufferB,(iWidth*iHeight*4))

oImgB.alpha_mode='STRAIGHT'
oImgB.filepath_raw="e:\\test20220617.png"
oImgB.file_format='PNG'
oImgB.save()

bpy.data.images.load("I:\\temp\\20220608_tb14_reading\\images\\bg_tb03.jpg", check_existing=True)