I am creating a whole bunch of TImages (~100) and at some point I need to have a quick look at all of them before continuing to process the information they contain. I neither want to save them all and then browse them with an external viewer, nor do I want to open a hundred canvases, nor do I want to create one canvas with 100 thumbnails. The best thing would be to load them all into one Canvas or GUI and screen them with a “previous” and a “next” button.
Since, I am sure something like this is already out there, I would like to know what you would recommend for me to use.
You can find a short script showImages.C below viewing images in a directory. Read comments.
Rene
TCanvas *c2 = 0;
void showOne();
void showImages(const char *ftype=".jpg")
{
//this script scans the current directory for all files containing the string ftype in the name (default is .jpg)
//a canvas is created and divided in as many subpads as images found in the directory.
//when moving the mouse of a pad thumbnail, a zoomed view of the image appears.
//Author:: Rene Brun
const char *path = ".";
void *dir = gSystem->OpenDirectory(path);
if (!dir) {
printf("invalid directory name: %s\n",path);
return;
}
TList *files = new TList();
const char *afile, *afilefull, *hname;
TFile *f;
Int_t fn = 0;
char fname[20];
TNamed *f;
while ((afile = gSystem->GetDirEntry(dir))) {
if (!strstr(afile,ftype)) continue;
afilefull = Form("%s/%s",path,afile);
printf("\n\nopening file: %s\n",afilefull);
fn++;
sprintf(fname,"f%d",fn);
f = new TNamed(fname,afilefull);
files->Add(f);
}
//we divide the canvas in sqrt(fn) files
if (fn <= 0) {
printf("no files named %s in this directory\n");
return;
}
TImage *images[1000];
Int_t ndiv = (Int_t)TMath::Sqrt(fn)+1;
TCanvas *c1 = new TCanvas("c1","c1",800,800);
c1->Divide(ndiv,ndiv);
for (Int_t i=0;i<fn;i++) {
c1->cd(i+1);
f = (TNamed*)files->At(i);
images[i] = TImage::Open(f->GetTitle());
images[i]->SetConstRatio(kFALSE);
images[i]->Draw("x");
}
c1->AddExec("showone","showOne()");
gSystem->FreeDirectory(dir);
}
void showOne() {
TObject *select = gPad->GetSelected();
if(!select) return;
if (!select->InheritsFrom("TImage")) return;
printf("pad=%s, select=%s\n",gPad->GetName(),select->GetName());
if (!c2) c2 = new TCanvas("c2","c2",850,10,600,600);
c2->cd();
c2->Clear();
select->Draw();
c2->Update();
}