If(){design}else{art} Processing, le livre

13 juin 2019

Outil de diffusion d’images

Filed under: Non classé — jean-noël Lafargue @ 9 h 21 min

Le programme qui suit sert à diffuser au hasard les images contenues dans le dossier « data ». Les images affichées voient leur format s’adapter à l’écran lorsqu’elles excèdent ses dimensions.

String[] images;
IntList ordre;
int vitesse=1500; // une nouvelle image s'affiche toutes les 1,5 secondes (1500 millisecondes)
int next=0;
color fond=#000000;

void setup(){
  fullScreen();
  imageMode(CENTER);
  ordre=new IntList();
  images = listFileNames(dataPath("")); 
  background(fond);
}

void draw(){
  if(ordre.size()==0){
    for(int a=0;anext){
    affiche(images[ordre.get(0)]);
    ordre.remove(0);
    next=millis()+vitesse;
  }
}

String[] listFileNames(String dir) {
  File file = new File(dir);
  if (file.isDirectory()) {
    String names[] = file.list();
    String newNames[] = new String[0];
    for(String n:names){
      if(match(n, ".*\\.|jpg|jpeg|png|bmp|gif|JPG|JPEG|PNG|BMP|GIF")!=null){
        newNames = (String[]) append(newNames, n);
      }
    } 
    return newNames ;
  } else { 
    return null;
  }
}

void affiche(String im){
  background(fond);
  PImage fichier = loadImage(im);
  int largeur=fichier.width;
  int hauteur=fichier.height;
  if(largeur>width){
    float pc = float(width)/largeur;
    hauteur = round(pc*hauteur);
    largeur=width;
  }
  if(hauteur>height){
    float pc = float(height)/hauteur;
    largeur = round(pc*largeur);
    hauteur=height;
  }
  
  if(largeur>width||hauteur>height){
    largeur=width;hauteur=height;
  } 
  image(fichier, width/2, height/2, largeur, hauteur);
  
}

Aucun commentaire »

No comments yet.

RSS feed for comments on this post. TrackBack URL

Leave a comment

Powered by WordPress