将图片的base64直接从内存中读取,不用存储在本地的方法的python和java实现
import cStringIO
imgdata = cStringIO.StringIO(base64.b64decode(ls_f))
true_image = Image.open(imgdata)
true_image.show()
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
import java.io.*;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
import java.awt.color.ColorSpace;
import java.awt.image.ColorConvertOp;
public static String GenerateImage(String imgStr) {
BASE64Decoder decoder = new BASE64Decoder();
try {
byte[] b = decoder.decodeBuffer(imgStr);
for (int i = 0; i < b.length; ++i) {
if (b[i] < 0) {
b[i] += 256;
}
}
ByteArrayInputStream in = new ByteArrayInputStream(b);
BufferedImage image = ImageIO.read(in);
ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_GRAY);
ColorConvertOp op = new ColorConvertOp(cs, null);
image = op.filter(image, null);
ByteArrayOutputStream out = new ByteArrayOutputStream();
ImageIO.write(image,"JPEG", out);
byte[] data_byte = out.toByteArray();
BASE64Encoder encoder = new BASE64Encoder();
return encoder.encode(data_byte);
} catch (Exception e) {
return "";
}