How to create an image from scratch?

PerlMagick is an object-oriented Perl interface to ImageMagick. Use this forum to discuss, make suggestions about, or report bugs concerning PerlMagick.
Post Reply
joew
Posts: 42
Joined: 2012-08-23T01:19:56-07:00
Authentication code: 67789

How to create an image from scratch?

Post by joew » 2013-08-08T08:35:11-07:00

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.

hh10k
Posts: 1
Joined: 2013-08-28T01:45:19-07:00
Authentication code: 6789

Re: How to create an image from scratch?

Post by hh10k » 2013-08-28T01:53:08-07:00

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.

BrianP007
Posts: 48
Joined: 2013-12-13T09:54:14-07:00
Authentication code: 6789

Re: How to create an image from scratch?

Post by BrianP007 » 2015-05-20T13:48:43-07:00

>> 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

HocusLocus
Posts: 2
Joined: 2018-01-02T21:06:41-07:00
Authentication code: 1152

Re: How to create an image from scratch?

Post by HocusLocus » 2018-01-02T21:37:36-07:00

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 ////////////////////////////////////////////////////////

HocusLocus
Posts: 2
Joined: 2018-01-02T21:06:41-07:00
Authentication code: 1152

Re: How to create an image from scratch?

Post by HocusLocus » 2018-01-03T01:56:33-07:00

//// 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.

snibgo
Posts: 9581
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: How to create an image from scratch?

Post by snibgo » 2018-01-03T06:34:32-07:00

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.
snibgo's IM pages: im.snibgo.com

Post Reply