Page 1 of 1

How to generate an image pixel by pixel

Posted: 2014-05-29T11:00:45-07:00
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.

Re: How to generate an image pixel by pixel

Posted: 2014-05-29T11:07:27-07:00
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.

Re: How to generate an image pixel by pixel

Posted: 2014-05-29T11:20:36-07:00
by fmw42
Just to add to glenn's comment, you can find information here http://netpbm.sourceforge.net/doc/ppm.html

Re: How to generate an image pixel by pixel

Posted: 2014-05-29T11:26:12-07:00
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.

Re: How to generate an image pixel by pixel

Posted: 2014-05-29T11:35:37-07:00
by ociurana
Thanks for the reply, works like a charm!

Re: How to generate an image pixel by pixel

Posted: 2014-05-29T11:40:58-07:00
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.