Skip to content

Green screen

  • by

You want to make objects with transparency you can add to something else. I don’t know if you are supposed to do it this way but seems like it might work.

Photograph object on green

remove the green

#!/usr/bin/perl
use strict;
use warnings;
use Image::Imlib2;
use feature "say";
#can read directory and make an array of images, @files
my ($image); 
my $file1=("./_MG_7784.png");

#for (my $i=0;$i<=$#files;$i++){
#$file=$files[0];
say $file1;
make_transparent($file1);
$image->save("Wood_trans.png");
say "done";
exit;


sub make_transparent{
my $file=$_[0];
say $file;
$image=Image::Imlib2->load($file);
$image->has_alpha(1);
$image->will_blend(0);
$image->set_colour(0,0,0,0); 
my $width = $image->width;
my $height = $image->height;
for my $y (0 .. $height - 1) {
# can experiment with the RGB values	
   for my $x (0 .. $width - 1) {
     my  ($red, $green, $blue, $alpha) = $image->query_pixel($x, $y);
     #say "$red, $green, $blue, $alpha";
     #if($green>200 && $red<50 ||$green>200 && $blue<100 ||$green>235  ){ 
 #    if($green>100 && $red<$green){
  if($green>100&&$red<40){      
         $image->draw_point($x, $y);
           }
 #          if($green>100 && $red==0){ 
#		$image->draw_point($x, $y);	   
#    }
    }
  }	
return;	
	}

there are issues in that the green is reflected in the object. I tried linear polarising filter on the lights and the lens which seems to help a bit. What seems to work is to photograph the object first on a white/neutral background.

can make a palette from that to use with ffmpeg.

ffmpeg -i whiteBG.png -vf "palettegen=max_colors=256:reserve_transparent=1" -frames:v 1 palette.png

this makes a 16×16 pixel image which is shown bigger here

and apply that to the transparent image that has green

#!/usr/bin/perl
use strict;
use warnings;

my $ffmpeg_command="ffmpeg -y -f concat -safe 0 -i image_list.txt -i palette.png -filter_complex \"[0:v]scale=1936:-1,setsar=1[vid];[vid][1:v]paletteuse\"  save_name%04d.png";
system($ffmpeg_command);

image_list.txt is a list of files preceded by “file”. I just have it like that if want to change more than one file.

file ./images/Wood.png
file ./images/_MG_7784.png

Wants refining a bit but looks like might work