Question:
make a gif image from an array or text file - python or java?
rtshigeta
2008-11-07 08:13:35 UTC
how can i convert an array of integer or byte values to an image in python or java - any libraries you recommend?
Three answers:
Steven V
2008-11-07 09:26:00 UTC
It's all built into Java........ I don't have the exact code, but it should help you find exactly what you need.



--------------------------------------------------------

// need to created a BufferedImage



Color[] colorArray = new Color[]{Color.RED};



BufferedImage bufferedImage = new BufferedImage(getXWidth(), getYWidth(),

BufferedImage.TYPE_INT_RGB);



// Create a graphics contents on the buffered image

Graphics2D g2d = bufferedImage.createGraphics();



for (int x = 0; x < getXWidth(); x++) {

for (int y = 0; y < getYWidth(); y++) {

g2d.setColor(colorArray[x][y]);

g2d.drawLine(x, y, x + 1, y + 1);

}

}



g2d.dispose();





-----------------------------------------------------------------------



Then you need to write the image



/** Creates a new instance of ImageWriter */

public RenderedImageWriter() {

super();

}



private static final String defaultFormat = "png";



private ImageWriter findImageWriter(String format) {

ImageWriter writer = null;

Iterator iter = ImageIO.getImageWritersByFormatName(format);

if (iter.hasNext()) {

writer = (ImageWriter) iter.next();

}



if(writer == null && format.equalsIgnoreCase(defaultFormat) == false) {

findImageWriter(defaultFormat);

}



return writer;



}



/**

*

* the save method

*

* @param rendImage

* @param outputFile

* @param format

* @throws IOException

*/

public void save(RenderedImage rendImage, File outputFile, String format)

throws IOException {

// Find a writer

ImageWriter writer = findImageWriter(format);



// Prepare output file

ImageOutputStream ios = ImageIO.createImageOutputStream(outputFile);

writer.setOutput(ios);



// Set the compression quality

ImageWriteParam iwparam = new MyImageWriteParam();

iwparam.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);

float compressionQuality = 0;

iwparam.setCompressionQuality(compressionQuality);



// Write the image

writer.write(null, new IIOImage(rendImage, null, null), iwparam);



// Cleanup

ios.flush();

writer.dispose();

ios.close();

}



-----------------------------------------------------

You'll need this subclass



import java.util.Locale;



import javax.imageio.plugins.jpeg.JPEGImageWriteParam;



/**

* This class overrides the setCompressionQuality() method to workaround a

* problem in compressing JPEG images using the javax.imageio package.

*/

public class MyImageWriteParam extends JPEGImageWriteParam {

/**

*

* Constructs a new MyImageWriteParam object.

*

*/

public MyImageWriteParam() {

super(Locale.getDefault());

}



/**

* This method accepts quality levels between 0 (lowest) and 1 (highest) and

* simply converts it to a range between 0 and 256; this is not a correct

* conversion algorithm. However, a proper alternative is a lot more

* complicated. This should do until the bug is fixed.

*/

@Override

public void setCompressionQuality(float quality) {

if (quality < 0.0F || quality > 1.0F) {

throw new IllegalArgumentException("Quality out-of-bounds!");

}

this.compressionQuality = 256 - (quality * 256);

}

}



-----------------------------------------------------------------------------
deonejuan
2008-11-07 08:43:45 UTC
I would say Python Imaging Library ( PIL ), but you will need to have python and also the PIL. I love python, but getting everything to link and make a GUI can slow down development. So, python, if you just want to convert.



I've used python to make a binary file that loads as an image in java.



The java has an ongoing project called Java Advance Imaging. Because JAI is not part of javaSE, you will have to find books and online docs on how to use the JAI API, but at least, java comes with buttons.
rooks
2016-11-09 05:28:21 UTC
it fairly is the 'no longer ordinary' thank you to serialize gadgets, the code occasion presented by employing your submit. look on the link below, take be conscious how concise the code is for IO employing Scanner. The link shows the fashionable thank you to r/w a text fabric document. ok, then I many times have an open-end documents shop like ArrayList. I study the text fabric document. If it has text fabric, I placed it into the ArrayList as a String. Loop the ArrayList text fabric now loaded in reminiscence... Then, I take each line of text fabric String[] shapeFLDS = arrayList.get( num ).s.split( " " ); shapeFLDS is momentary, yet i comprehend container 0 is an merchandise, container a million is var call, container 2 is howmany, something fields are dimensions. It looks such as you like 7 training for each geometric. each of those training might have one or 2 constructors. shape is an Interface and it describes all the geometry.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...