How to generate an image pixel by pixel

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
ociurana
Posts: 2
Joined: 2014-05-29T10:44:02-07:00
Authentication code: 6789

How to generate an image pixel by pixel

Post by ociurana »

Hello,

I have a bidimensional array with RGB values (ej: "0,0,0") and I'm trying to generate an image drawing it pixel by pixel. It works fine but it takes too long time. The bash script is something like this:

Code: Select all

[...]
convert -size ${size_x}x${size_y} xc:none accppt.png
for ((y=1;y<${size_y};y++)) do
     for ((x=1;x<${size_x};x++)) do
          mogrify -fill "rgb(${array_ACCPPT[${x},${y}]})" -draw "point ${x},${y}" accppt.png
     done
done
[...]
Is there any way to optimize the operation?
Is there any way to paint all the pixels at once?
Array size is about 500x500 pixels.

Thanks in advance.
User avatar
glennrp
Posts: 1147
Joined: 2006-04-01T08:16:32-07:00
Location: Maryland 39.26.30N 76.16.01W

Re: How to generate an image pixel by pixel

Post by glennrp »

I'd write a PPM image (P6 width height 255 followed by width x height RGB pixels) then use
"convert" to convert the PPM to PNG.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: How to generate an image pixel by pixel

Post by fmw42 »

Just to add to glenn's comment, you can find information here http://netpbm.sourceforge.net/doc/ppm.html
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: How to generate an image pixel by pixel

Post by fmw42 »

Here is an example I have used in the past. I created an array of triples, then

Code: Select all

echo "P3 $width $height $max\n ${array[*]}" | convert - image.png
Note that in my case, I only created a 1 row image so height was 1. I have not tested building a (large) 2D image.
ociurana
Posts: 2
Joined: 2014-05-29T10:44:02-07:00
Authentication code: 6789

Re: How to generate an image pixel by pixel

Post by ociurana »

Thanks for the reply, works like a charm!
User avatar
glennrp
Posts: 1147
Joined: 2006-04-01T08:16:32-07:00
Location: Maryland 39.26.30N 76.16.01W

Re: How to generate an image pixel by pixel

Post by glennrp »

fmw42 wrote:Here is an example I have used in the past. I created an array of triples, then

Code: Select all

echo "P3 $width $height $max\n ${array[*]}" | convert - image.png
Note that in my case, I only created a 1 row image so height was 1. I have not tested building a (large) 2D image.
In fact, I generally use "P3" as well, instead of "P6" as I suggested earlier, if I'm working with small images. P3 takes
ASCII text for the RGB values while P6 takes binary triples crammed together without any space between them. So you
can even create a P3 image with a text editor. But if you're using a program to spit out a large image, P6 is just as
simple and occupies a smaller file size.
Post Reply