Tom J Nowell
Tom J Nowell

Reputation: 9981

Creating arbitrary sized blank pngs/jpegs

I want to create arbitrary sized pngs and jpeg images of a single block colour at a specified x,y size, however I do not want to do this by generating a raw image in memory and encoding/compressing it due to potential memory requirements.

How would this be done?

Upvotes: 0

Views: 317

Answers (3)

leonbloy
leonbloy

Reputation: 75926

If what you want is to create an arbitrarly large PNG image on the fly, writing it to a stream (file, network or whatever) without having it created in memory, you can use -in Java- this PNJG library (disclaimer, I wrote it). For example, this generates a black RGB8 image:

PngWriter png = FileHelper.createPngWriter(
     new File(filename), 
     new ImageInfo(cols, rows,  8, false),false);
ImageLine iline = new ImageLine(png.imgInfo);
for (int j = 0; j < cols ; j++) 
  ImageLineHelper.setPixelRGB8(iline, j, 0);
for (int row=0;row<rows; row++) {
  iline.setRown(row);
  png.writeRow(iline);
}
png.end();

Upvotes: 0

Jeff Paquette
Jeff Paquette

Reputation: 7127

It's unclear what language you're using, or what your use case is, but I use the convert program from the imagemagick suite to do this:

convert -size 300x300 xc:transparent missing_normal.png

If you're creating images for a browser you could just create a single 1x1 image, then use the width and height attributes to let the browser scale it for you.

Upvotes: 1

Will
Will

Reputation: 75635

You could make your own generator. Pseudo-code for a PNG generator is:

write_png_header(width,height);
gzip_scanline(width,colour);
for(y=1; y<height; y++) { // scanline 0 already written
  write_type_copy_previous();
  gzip_zero_delta(width);
}

Upvotes: 1

Related Questions