Page 1 of 1

How to create an image from scratch?

Posted: 2013-08-08T08:35:11-07:00
by joew
Hello wizards,

I just tried this one:

Code: Select all

#! /usr/bin/perl

use strict;
use warnings;

use Image::Magick;

my $e;
my $DPI = 300; # 600;

my $image = Image::Magick->new(density => $DPI,
                               width=>100, height=>100,
    );

# $e = $image->Read ("labels4.ps");
warn $e if $e;

$e = $image->Draw (fill=>"red",
                   stroke=>"none", strokewidth=>0,
                   primitive=>"roundrectangle",
                   points=>"10,10 90,90, 5,5");
warn $e if $e;

$e = $image->Display(); warn $e if $e;
When I uncomment the line with the Read() method, I get the expected result. But without reading in the file, I get those error messages:

Code: Select all

jw@raven:/m$ ./t.pl
Exception 410: no images defined `Draw' @ error/Magick.xs/XS_Image__Magick_Mogrify/7403 at ./t.pl line 22.
Exception 410: no images defined `Image::Magick' @ error/Magick.xs/XS_Image__Magick_Display/3564 at ./t.pl line 24.
jw@raven:/m$
I guess this is closely related to viewtopic.php?f=7&t=23871 but I just can't find any information on how to handle this.

Re: How to create an image from scratch?

Posted: 2013-08-28T01:53:08-07:00
by hh10k
I was beating my head against the wall with the same error message, and eventually found the answer in the docs. It seems you need to create the image by reading from something, and there's a built-in 'canvas' source for this:

Code: Select all

$image = Image::Magick->new;
$image->Set(size=>'100x100');
$image->ReadImage('canvas:white');
$image->Set('pixel[49,49]'=>'red');
Not at all obvious from the error message, and the Perl API docs are poorly organised to learn about it.

Re: How to create an image from scratch?

Posted: 2015-05-20T13:48:43-07:00
by BrianP007
>> It seems you need to create the image by reading from something, and there's a built-in 'canvas' source for this: ...
>> Not at all obvious from the error message, and the Perl API docs are poorly organised to learn about it.

Confirmed. I can not draw a line unless I execute:

Code: Select all

$image->ReadImage('canvas:white');  # Must READ to make image?
Without reading the canvas, I get this error message.
Exception 410: no images defined `Draw' @ error/Magick.xs/unknown/7586 at c:/bin/bb.pl line 309. << "$res = $image->Draw( ..."

The NEW constructor DEFINES an image: Log file says "$im=Image::Magick=ARRAY(0x6afad80)"

The EXACT same operation with GD works fine:
$gdim = new GD::Image(2048, 30);
$gdim->line(10, 10, 2000, 20, $gdim->colorAllocate(255,0,0) ); << GD found the IMAGE is just NEW'ed. IM can not find any imag at this point
open(I, ">gd.test.png");
binmode I; print I $gdim->png; close I;

Create object, draw line, close, voila.

Needing to read something onto your canvas before you can write to it is non-standard, non-intuitive and highly inefficient. The error message too is totally bogus. What does NEW do?

What is the argument supporting this as the best practical behavior? I am down a few handfuls of hair as a result of this design. :)

Brian

Re: How to create an image from scratch?

Posted: 2018-01-02T21:37:36-07:00
by HocusLocus
I just registered so I could post this.

This is the most frustrating evening in WEEKS. I have spent 4 hours gnashing my teeth.
I just wanted to make an image object in memory and write to it.

$i2=Image::Magick->new;
Creates a AGGRAVATING DYSFUNCTIONAL object you cannot draw on.
$i2=Image::Magick->new(width=>100,height=>100,background=>transparent);
Creates a AGGRAVATING DYSFUNCTIONAL object you cannot draw on.
And the error message 'Exception 410: reference is not my type' is INCORRECT and MISLEADING.
ImageMagick created the object. The object *is* its type.

Then you start searching for examples, and guess what? You only find command line examples that read from files, because that is how the shell commands work. You find perl examples where people create images and then read from files. You start to resent those people, for reading from files. You ask them (silently), will you now build an image from scratch now, please? They never do.

Then you happen to spot someone who does a ReadImage('canvas:blue') and do you think you've found the answer to your problem? NO. You just think someone wants a blue background and that is a way to get one. But you don't want a blue background. You don't realize that their object was DYSFUNCTIONAL too until that canvas statement.

You just think that their ImageMagick somehow works, and yours doesn't.
Until 4 hours later almost by chance because joew (bless him) decided to ask a simple question, "How to create an image from scratch?"

I was beating away at it because in the distant past I remembered writing to objects after 'new'. Now I realize that was with Gd.

///////////////////////////////////////////// YAWP ////////////////////////////////////////////////////////

Re: How to create an image from scratch?

Posted: 2018-01-03T01:56:33-07:00
by HocusLocus
//// YAWN ////
Long day yesterday. Back after a nap to make sure everyone knows I am grateful to the ImageMaick crew and community... like audio took Audacity... despite steep learning curve, these are very sharp tools.

Re: How to create an image from scratch?

Posted: 2018-01-03T06:34:32-07:00
by snibgo
As far as I know, no one who uses IM with Perl documents publicly what they do. And no one who uses IM with Perl regularly visits these forums. We sometimes get people new to IM with Perl not knowing how to start, and we regulars can only point to the scant documentation and shrug our shoulders.

If someone who used IM with Perl would write some documentation, that would help the others.

Re: How to create an image from scratch?

Posted: 2018-01-19T04:05:07-07:00
by rmabry
You can at least create an image from scratch without reading some external image; below the image 'xc:white' is one of some internal types. (This is probably the same as the "canvas" mentioned earlier".) I don't find this onerous, since I usually want to write on some base color. Maybe there's another way to do it.

Code: Select all

$image = Image::Magick->new(size=>"100x100");

$image->ReadImage('xc:white');

$image->Draw(primitive=>'circle', points=>"50,50 50,20", fill=>'blue');
$image->Write("newImageTest.png");
That's a pretty crude example; of course one should always check for warnings and errors...

Code: Select all

...
$notOk = $image->Draw(primitive=>'circle', points=>"50,50 50,20", fill=>'blue');
die "Killing me softly with $notOk" if $notOk;
...
Speaking of frustrating gaps in the PerlMagick doc, I recently shared your experience with image sequences. There are many instances in the doc of what one can do with a sequence of images (modify them, take their average, etc), but how to make a sequence from scratch? It's nowhere in the doc. Finally I found something buried in some sample script that was created when my PerlMagick was built: use push. Start with a new empty image and push more images onto it. Again, there might be a better way, but at least this works for me. I do need to create some first image from scratch, but that's above. If that $image already exists, then below I get a sequence of 10 copies.

Code: Select all

$imageSequence = new Image::Magick;
map {push @$imageSequence, $image->Clone()} (1..10);
(I might be violating Perl etiquette by using map over a bogus 10-element array. Maybe a for loop is better.)

Next frustrations: Blobs and Direct Pixel Access...

Cheers and good luck,

Rick

P.S. Everyone understands how limited the IM team's resources are, so expressing frustrations with the docs should not be construed to be schimpfing or critical. If more time were spent on the docs, fewer IM toys may have been developed to play with in the first place; they are the same folks who do most of this. We get it. On the other hand, it is fair to wonder where the sweet spot is --- with better docs might come better participation, which might lead to a virtuous cycle of more development and better docs...