Question

Question pretty much says it all.

The idea is to generate procedural PNG's. I'm aware of imageMagick's '-fx' operator but it's very slow.

A workaround could be to save my array to .bmp (which is pretty much just saving it) and then just use ImageMagick to convert .bmp to .png but it would be nice to have a more direct approach.

Was it helpful?

Solution

You may be able to exploit ImageMagick's TXT image format for this purpose. I can't tell you anything about the performance, you'll have to test it.

Start by trying the following:

convert  some-small.png  some-small-png.txt
convert  some-small.jpg  some-small-jpg.txt
convert  some-small.tif  some-small-tif.txt
[...]

(This conversion can be slow too.)

Then do the reverse conversions:

convert  some-small-png.txt  some-small-roundtripped.png
convert  some-small-png.txt  some-small-roundtripped.jpg
convert  some-small-png.txt  some-small-roundtripped.tif
[...]

(This conversion should be faster.)

Compare initial PNG, JPEG, TIFF with the roundtripped ones.

Look at the *.txt files to understand their format:

  • It is a formal textual description enumerating, line by line, for each pixel's coordinates and the respective color values.
  • The first line is a header describing the image's dimensions, its depth and the colorspace used.
  • Also be aware that on each line everything after the # (which depicts the hex values of the respective colors) is not required -- you may regard it as a comment. Also, all remaining blanks on each line are optional.

It means you could re-organize your RGBA array in such a way that it matches ImageMagick's TXT image format (as I said, you don't require neither the hex values nor the friendly color names) and then simply call convert (after adding the required header line).


Update

I'll give you an example.

Here is the extremly simple, stripped of blanks, names and #hex values for colors, 2x2 pixels sRGBA mini-image represented by 2x2.txt. It's file size is 129 Bytes.

cat 2x2.txt
 # ImageMagick pixel enumeration: 2,2,65535,srgba
 0,0:(65535,0,0,65535)
 0,1:(0,65535,0,65535)
 1,0:(0,0,65535,65535)
 1,1:(0,0,0,0)

You an convert this into a PNG image:

convert  2x2.txt  2x2.png

2x2.png is 293 Bytes. Since a 2x2 pixel PNG is a bit too small to be recognized on a website, we can convert to something bigger:

convert 2x2.txt  -scale 10000%  200x200.png
convert 2x2.txt  -scale 1000%   20x20.png

These 2 PNGs are recognizable on a web page:

200x200.png   20x20.png

To demonstrate the full format (not stripped of blanks, names or #hex values for colors), let's create the respective 20x20.txt and look at it:

convert  20x20.png  20x20.txt

cat 20x20.txt 
 # ImageMagick pixel enumeration: 20,20,255,srgba
 0,0: (255,  0,  0,255)  #FF0000  red
 1,0: (255,  0,  0,255)  #FF0000  red
 2,0: (255,  0,  0,255)  #FF0000  red
 3,0: (255,  0,  0,255)  #FF0000  red
 4,0: (255,  0,  0,255)  #FF0000  red
 5,0: (255,  0,  0,255)  #FF0000  red
 6,0: (255,  0,  0,255)  #FF0000  red
 7,0: (255,  0,  0,255)  #FF0000  red
 8,0: (255,  0,  0,255)  #FF0000  red
 9,0: (255,  0,  0,255)  #FF0000  red
 10,0: (  0,  0,255,255)  #0000FF  blue
 11,0: (  0,  0,255,255)  #0000FF  blue
     [....]
     [....]
     [....]
 7,19: (  0,255,  0,255)  #00FF00  lime
 8,19: (  0,255,  0,255)  #00FF00  lime
 9,19: (  0,255,  0,255)  #00FF00  lime
 10,19: (  0,  0,  0,  0)  #00000000  none
 11,19: (  0,  0,  0,  0)  #00000000  none
 12,19: (  0,  0,  0,  0)  #00000000  none
 13,19: (  0,  0,  0,  0)  #00000000  none
 14,19: (  0,  0,  0,  0)  #00000000  none
 15,19: (  0,  0,  0,  0)  #00000000  none
 16,19: (  0,  0,  0,  0)  #00000000  none
 17,19: (  0,  0,  0,  0)  #00000000  none
 18,19: (  0,  0,  0,  0)  #00000000  none
 19,19: (  0,  0,  0,  0)  #00000000  none
 
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top