Notifications
Clear all

[Closed] Reading imabes column by column not working.Why?

First, I want to apologyse for my english.
I try to make a script that read images line by line and column by column and change the color of some pixels. The code that open images, read first, second…etc. line and change the pixels colors work well.Here is it:

(		
	theBitmap = openBitMap(GetDir #scripts + "\\Combined_UVs_.tga")
	newBmpA = bitmap theBitmap.width theBitmap.height
	newBmpA = copy theBitmap
	
	--	loop verticaly
	for y = 1 to theBitmap.height do
	(
		pixRow = getPixels theBitmap [0,y-1] theBitmap.width -- read all pixels of a single line
		countPix = #()
		for x = 1 to theBitmap.width do --go through all pixels in the line
		(
			if (pixRow[x] == (color 0 0 0)) then
			(
				append countPix 1
			)
			else
			(
				setPixels newBmpA [x-1,y-1] #(red)
			)
		) 				
	) 
	close theBitmap
	newBmpA.filename = theBitmap.filename
	Save newBmpA
	Close NewBmpA
	gc()
)

After executing this code the images change from this:
to this: and that is the correct result.

But after executing the code for reading column by column the result is very strange. Here the code:

(		
	theBitmap = openBitMap(GetDir #scripts + "\\Combined_UVs_.tga")
	newBmpA = bitmap theBitmap.width theBitmap.height
	newBmpA = copy theBitmap
		
	--	loop horizontaly
	for x = 1 to theBitmap.width do
	(
		pixColumn = getPixels theBitmap [x-1,0] theBitmap.width --read all  pixels of a single column
		countPix = #()
		for y = 1 to theBitmap.height do --go through all pixels in the column
		(
			if (pixColumn[y] == (color 0 0 0)) then
			(
				append countPix 1
			)
			else
			(	
				setPixels newBmpA [x-1,y-1] #(green)
			)
		) 				
	)
	close theBitmap
	newBmpA.filename = theBitmap.filename
	Save newBmpA
	Close NewBmpA
	gc()
)

The final image looks in this way:
The first column is black(this is ok), but all other columns are green. When i check the pixColumn variable for first column:
pixColumn = #((color 0 0 0),(color 0 0 0),(color 0 0 0)…)
but for every other columns:
pixColumn = #()

Where is my mistake in the seccond code?

Thanks.

4 Replies
 lo1

getPixels always reads rows, not columns. There is no native maxscript way of getting a column of pixels.

possible complicated workaround (untested) : rotate the bitmap using bitmaptexture 90 degrees and use rendermap to get a bitmap from that, read rows of that bitmap, save it to temp file and rotate it back 90 degrees and render another bitmap from it :shrug:

Hi Kosta,

I see you swap the order of “for y … ( for x … )” in the second script.
Excluded that, they are identical. Note: [i]setPixels/i works by row.

Yes, I swap them, but i did not know that the getPixels() work only with rows.

Both Get/Set Pixels works with array of values (but can be limited to single pixel).
Ok, nm, that should be fine (and optimized I hope) –

(
srcBmp = openBitMap "C:\\Combined_UVs_.tga"
newBmp = bitmap srcBmp.width srcBmp.height
 
for y = 1 to srcBmp.height do
(
	pixRow = getPixels srcBmp [0,y-1] srcBmp.width
	for c = 1 to pixRow.count where pixRow[c] != black do pixRow[c] = green
	setPixels newBmp [0,y-1] pixRow
)
 
close srcBmp
newBmp.filename = srcBmp.filename
save newBmp
close newBmp
gc()
) 

You can made a function to pass the color as argument but I leave this to you