Page 1 of 1

[SOLVED] Creating a new() image from scratch, WITH attributes

Posted: 2019-09-21T09:00:59-07:00
by E. Fudd Wabbitwy
According to my understanding of the documentation (references in the codeblock), it should be possible to create an image from scratch, using all of the attributes named under SetAttribute and to read them back, along with all other attributes under GetAttribute.

However, in the following, only 'size' returns what was set in new(): depth returns incorrectly as 16, not 8, and the rest of the attributes return EMPTY.

For a time I wondered if, because $image can contain multiple images, I should be "dereferencing" it in order to Get() attributes (for example, like $image->[0]->Get()), but I found examples where an $image with a single member did not resort to the index.

I only recently installed v7.0.8 from source and although it passed 'make check', I suppose it could be broken.

Can you suggest anything?

Thank you.

[EDIT: here's what's returned]
Size:100x1 Depth:16 Magick: W: H: Matte: Alpha: Fill:

Code: Select all

#!/usr/bin/perl
use strict;
use Image::Magick;

my @pixel=(0.5,0.5,0.5,1);

# from: https://imagemagick.org/script/perl-magick.php#overview
# "The new() method takes the same parameters as SetAttribute."
my $image = Image::Magick->new(
  size=>'100x1',
  depth=>8,
  magick=>'PNG',
  alpha=>'Transparent',
  matte=>'True',
  fill=>\@pixel
);
#$image->Set(depth=>8);
$image->Set('depth'=>8); # quoted or not, the result is '16' (??)

# from: https://imagemagick.org/script/perl-magick.php#set-attribute
# "...here is a list of all the image attributes you can set:"
# from: https://imagemagick.org/script/perl-magick.php#get-attribute
# "In addition to all the attributes listed in Set an Image Attribute,
#    you can get ... additional attributes:"
# NOTE: Get() arguments are quoted because Perl complains about them being "barewords" under 'use strict'
my $z=$image->Get('size'); # NOTE: even though 'size' is quoted here, it was unquoted in new() but it's still found correctly
my $d=$image->Get('depth'); NOTE: depth was defined as 8 in new() and Set() to 8 as well, but Get()s as 16 (??)
my $g=$image->Get('magick'); # EMPTY
my $c=$image->Get('columns'); # EMPTY
my $r=$image->Get('rows'); # EMPTY
my $t=$image->Get('matte'); # EMPTY
my $a=$image->Get('alpha'); # EMPTY
my @f=$image->Get('fill'); # EMPTY

print "Size:$z Depth:$d Magick:$g W:$c H:$r Matte:$t Alpha:$a Fill:@f\n";

Re: Creating a new() image from scratch, WITH attributes

Posted: 2019-09-23T19:04:34-07:00
by E. Fudd Wabbitwy
The sticking point is non-intuitive.

It is Not Enough to instantiate a new Image::Magick (which happens to be a list of images); it is also NECESSARY to READ at least one Image!

And, creating a new Image from scratch is also Non-Intuitively performed by "Reading" something that's non-existent!

Add this line, after the ->new() declaration:

Code: Select all

$image->ReadImage('xc:red');
and, bazinga, suddenly a (real-life) image exists to be queried, correctly this time.

Re: [SOLVED] Creating a new() image from scratch, WITH attributes

Posted: 2019-09-23T19:09:22-07:00
by E. Fudd Wabbitwy
There are other image-strings that may be used to create a new image (that is, other than 'xc:red') but I've not found a definitive list of all of them. I have had to rely on running across them in examples.

One of the weirder ones I experimented with that worked was:

Code: Select all

$image->ReadImage('gradient:0xffff00-0xffff00');
If the two colors are different, the new image will be filled with a gradient between them, but if they're the same color, the image is filled with that.

Re: [SOLVED] Creating a new() image from scratch, WITH attributes

Posted: 2019-09-23T19:51:09-07:00
by fmw42

Re: [SOLVED] Creating a new() image from scratch, WITH attributes

Posted: 2019-09-24T06:07:18-07:00
by E. Fudd Wabbitwy
Thanks, Fred. That info is possibly going to be a game-changer.

I'm trying to
  • open a source PNG file, containing transparent and non-transparent pixels,
  • open an "empty" PNG file as a target (say a dummy file of the same size, filled with 'white')
  • copy/overwrite transparent pixels from the source, according to one rule, to the target
  • and copy/overwrite the non-transparent pixels, according to a different rule
(I'm using the transparent pixels to "flank" the pixels to be processed.)

As I've been struggling to do this, it feels like I'm stepping from cowpie to cowpie as cross the pasture. :)

Re: [SOLVED] Creating a new() image from scratch, WITH attributes

Posted: 2019-09-24T06:24:53-07:00
by snibgo
E. Fudd Wabbitwy wrote:open an empty PNG file as a target
Sorry, what does that mean? How can an image file be "empty"? Perhaps you mean it has a certain number of pixels but they are all transparent. But I don't think you mean that.

Re: [SOLVED] Creating a new() image from scratch, WITH attributes

Posted: 2019-09-24T06:52:33-07:00
by E. Fudd Wabbitwy
I meant it in a sense that the target would be "empty" of "meaningful" pixels, which would be overwritten later. (I'll edit.)