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



Hello Ken,

thank you for answering. I have tried to use a number
instead of a string ($pmm_number = 10) but the behavior is
the same: the inner loop runs one time with $p=1.

If I declare all variables as local the subprogram did not
get the values of $sourcefolder_path, $sourcefile_name,
$p and $pmm_resultfolder_path. I do not know how
PMM_TILE.PLX will get this values instead.


best regards
Günter




kens schrieb:

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

  • Re: Programmers unpaid overtime.
    ... > that most programs read multi-GB files into one enormous string, ... > call Split on that string, perhaps multiple times, within loops, ... > a string concatenation within its inner loop. ... > Micro$haft's typical QuickSort implementations? ...
    (comp.programming)
  • Re: coding an anagram function
    ... Exiting the inner loop after the condition is first satisfied, ... not found in the second string and the two are not anagrams. ... Dim I As Long ...
    (microsoft.public.excel.programming)
  • Re: Deleting substrings
    ... Chris Dollin wrote: ... We start at the beginning of the string. ... no changes are made in the inner loop rather than repeatedly ...
    (comp.programming)