• Welcome to Smashboards, the world's largest Super Smash Brothers community! Over 250,000 Smash Bros. fans from around the world have come to discuss these great games in over 19 million posts!

    You are currently viewing our boards as a visitor. Click here to sign up right now and start on your path in the Smash community!

Help With Computer Science (Python)

Tugnus

Smash Cadet
Joined
Feb 23, 2011
Messages
70
Location
Boston/Jersey
So I'm pretty lost in Python. My teacher doesn't explain the commands for Python so I'm really confused on how to write this. I have to write a green screen type program that takes a picture with a green background and replaces it with another picture by looking at the color levels of each pixel and if the green color is a certain amount larger than blue/red then it will replace it with the other pictures pixel.

The pseudo code I have is:

#show the before pictures
#for each pixel in the foreground image:
# we first get the red, green and blue of this pixel
# if the red plus blue is greater than green (*)
# get the pixel from the background image at foreground pixel's location (**)
# set the background pixel's color to be the color obtained from the fg
#show the resulting picture (repaint)
#write the new picture to a file

You can use the writePictureTo(picture, path) function to save the image.

I know the basics to the program with the getRed()/blue/green and setRed(), but I don't know what to do about finding certain pixels in the foreground. What code would you do to make this work?

We are using JES version of python in class as well. Any help is greatly appreciated.
 

Zankoku

Never Knows Best
Administrator
BRoomer
Joined
Nov 8, 2006
Messages
22,906
Location
Milpitas, CA
NNID
SSBM_PLAYER
wouldn't you just getRGB of the corresponding pixel at X/Y location of the "background" image and then setRGB the "foreground" image pixel accordingly? I don't quite get what you're asking here.
 

Tugnus

Smash Cadet
Joined
Feb 23, 2011
Messages
70
Location
Boston/Jersey
That sounds right, but I do not know the proper code for that which is what I'm trying to figure out.
 

Zankoku

Never Knows Best
Administrator
BRoomer
Joined
Nov 8, 2006
Messages
22,906
Location
Milpitas, CA
NNID
SSBM_PLAYER
I don't know Python syntax, so I'll pseudocode something up

Code:
if (fg.getRed(x,y) + fg.getBlue(x,y)) > fg.getGreen(x,y)
     *do nothing*
else
     fg.setRGB(x,y,bg.getRGB(x,y))
Pretty sure setRGB is a valid call in Python, though.
 

Ganonsburg

Smash Lord
Joined
Jun 5, 2009
Messages
1,083
I don't know Python syntax, so I'll pseudocode something up

Code:
if (fg.getRed(x,y) + fg.getBlue(x,y)) > fg.getGreen(x,y)
     *do nothing*
else
     fg.setRGB(x,y,bg.getRGB(x,y))
Pretty sure setRGB is a valid call in Python, though.
Python doesn't like it when you do nothing within an if statement/for loop/while loop/etc. So I'd suggest:

for i in range(0,x):
for j in range(0,y):
if fg.getGreen(x,y) < fg.getRed(x,y) + fg.getBlue(x,y):
fg.setRGB(x,y,bg.getRGB(x,y)

Those for loops are just a suggestion; if you have a better way to loop through each x and y value by all means do that. I've always been particularly bad with graphics. I also never used setRGB or get<color>, but they'd be easy to define as functions in any case (I wouldn't recommend as a method; you'd then have to make every pixel an object). But check in any case; I only got a small intro to graphics in my intro class.
 
Top Bottom