Re: problem with calling subprogram in a nested for-loop




guba@xxxxxxxxxx wrote:
Hello,

I want to call a subprogram PMM_TILE.PLX (see below) with do '...'
multiple times in a nested for-loop from a program Batch_TILE.PLX.
The outer loop works but the inner loop runs only one time
with $p = 1 (only one image for every jpg in the $sourcefolder_path
is generated instead of $pmm_number images).

Did someone see the reason why the inner loop is aborted
after $p =1 and how can this be corrected?

Thank you very much!!

Guenter



*Batch_TILE.PLX*********************************
#Input

Always start your script with the following two lines:

use strict;
use warnings;

Declare variables with 'my' when you first use them.

$pmm_number = '10';

I haven't tried running this, but this line is a problem in that you
are assigning a string to $ppm_number, but using it as a number later.

my $ppm_number = 10;

$sourcefolder_path = 'C:/ART/IM_PlaneCovering/1_Sourceimages/';
$pmm_resultfolder_path =
'C:/ART/IM_PlaneCovering/2_Tiles/2_01_pmm-Tiles/';


#program
opendir(DIR, "$sourcefolder_path") || die "no such folder: $!";
@sourcefolder_list = grep(/\.jpg$/, readdir(DIR));
$sourcefolder_number = @sourcefolder_list;

for ($k = 0; $k <= $sourcefolder_number; $k +=1) {

$sourcefile_name = $sourcefolder_list[$k];


It looks like you've been programming in C (nothing wrong with that),
but you don't really need $sourcefolder_number and the for loop:

foreach my $sourcefile_name (@sourcefolder_list) {

for ($p = 1; $p <= $pmm_number; $p +=1) {do 'PMM_TILE.PLX'};

};

$p += 1 could be $p++

Could also use a range for the for loop:

for (1..$pmm_number){do 'PMM_TILE.PLX'};



*PMM_TILE.PLX*********************************
use Image::Magick;


# 0)Input
# 0.1) counting variable, folders, file

# 0.2) definition of lokal parameters
$rectangle_width_min_Faktor = 0.4;
$rectangle_width_max_Faktor = 0.6;
$rectangle_height_min_Faktor = 0.4;
$rectangle_height_max_Faktor = 0.6;




# 1) definition of PerlMagick objects
$pmm_G = new Image::Magick;


# 2) inilisation of random number generator
srand;


# 3) read source image
$sourcefile_fullname = $sourcefolder_path . $sourcefile_name;
$pmm_G->Read("$sourcefile_fullname");




# 4) local variable calculations: $rectangle_width, $rectangle_height,
$x1, $y1
$image_width = $pmm_G->Get('columns');
$image_height = $pmm_G->Get('rows');
if ($image_width < $image_height) {$image_min = $image_width} else
{$image_min = $image_height};
$rectangle_width_min = $rectangle_width_min_Faktor * $image_min;
$rectangle_width_max = $rectangle_width_max_Faktor * $image_min;
$rectangle_height_min = $rectangle_height_min_Faktor * $image_min;
$rectangle_height_max = $rectangle_height_max_Faktor * $image_min;
$rectangle_width = $rectangle_width_min +
(int(rand($rectangle_width_max - $rectangle_width_min)) + 1);
$rectangle_height = $rectangle_height_min +
(int(rand($rectangle_height_max - $rectangle_height_min)) + 1);
$x_allowed = $image_width - $rectangle_width;
$y_allowed = $image_height - $rectangle_height;
$x1 = int(rand($x_allowed))+1;
$y1 = int(rand($y_allowed))+1;


# 5) generate pmm_G by cropping the source image
$pmm_G->Crop(geometry => "$rectangle_width x
$rectangle_hight+$x1+$y1");

# 6) convert pmm_G.png -flop pmm_G_flop.png
$pmm_G_flop = $pmm_G->Clone();
$pmm_G_flop->Flop();

# 7) convert pmm_G.png pmm_G_flop.png +append pmm_tile_row.png
$q = $pmm_G->Clone();
push(@$q, $pmm_G_flop);
$pmm_tile_row = $q->Append(stack=>'false');

# 8) convert pmm_tile_row.png -flip pmm_tile_row_flip.png
$pmm_tile_row_flip = $pmm_tile_row->Clone();
$pmm_tile_row_flip->Flip();

# 9) convert pmm_tile_row.png pmm_tile_row_flip.png -append
pmm_tile.png
@$q = ();
$q = $pmm_tile_row->Clone();
push(@$q, $pmm_tile_row_flip);
$pmm_tile = $q->Append(stack=>'true');

# 10) generate name of the result image
chop $sourcefile_name;
chop $sourcefile_name;
chop $sourcefile_name;
chop $sourcefile_name;

if ($p <= 9) {$resultimage_name = "pmm_tile_" . $sourcefile_name . "-0"
. $p . ".jpg"}
else {$resultimage_name = "pmm_tile_" . $sourcefile_name . "-"
. $p . ".jpg"};

$resultimagepath_name = $pmm_resultfolder_path . $resultimage_name;


# 11) save result image (pmm_tile)
$pmm_tile->Write(filename=>"$resultimagepath_name",
compression=>'JPEG', quality=>'95');

I think the main problem is using a string as a number (as noted
above).
HTH, Ken

.



Relevant Pages

  • problem with calling subprogram in a nested for-loop
    ... multiple times in a nested for-loop from a program Batch_TILE.PLX. ... The outer loop works but the inner loop runs only one time ... # 5) generate pmm_G by cropping the source image ... chop $sourcefile_name; ...
    (perl.beginners)
  • Re: Find/Replace in csv file - using VB
    ... Note that this code *could* chop off the last line if it ends with a break inside a field, but I'm sort of assuming that won't happen. ... If you want to be sure, then after the For...Next loop, add the following line: ... Rob ...
    (microsoft.public.access.modulesdaovba)
  • Re: variable scope in for loop
    ... IMHO, doing that doesn't hurt so much, but it does hurt to be forced to ... I think it's cleaner to have a loop counter defined in the loop ... some more complex object being used in multiple blocks of code ... > declaring it once and using it multiple times. ...
    (comp.lang.cpp)
  • Re: [PATCH 01/18] flag parameters: helper function
    ... and rf multiple times around the loop. ... Copying them into locals does ...
    (Linux-Kernel)
  • Re: threads, XSUB allocated memory, destructors, destruction
    ... >> Obviously freegets called multiple times, ... cleanup is not specified (different under windows and linux). ... > use a typemap, but I don't think there's any relevance in that. ... find you will not reach the second iteration of the loop. ...
    (comp.lang.perl.misc)