发国外的文章要求图片是tif,cmyk色彩空间的。

大小尺寸还有要求。

比如

网上大神多,找到了一段代码,感谢!

https://www.jianshu.com/p/ec2af4311f56

https://github.com/KevinZc007/image2Tif

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

import javax.imageio.ImageIO;
import javax.imageio.stream.ImageOutputStream;
import javax.media.jai.JAI;
import javax.media.jai.PlanarImage;

//import com.sun.media.imageio.plugins.tiff.TIFFField;
import com.sun.media.imageio.plugins.tiff.TIFFTag;
import com.sun.media.jai.codec.FileSeekableStream;
import com.sun.media.jai.codec.TIFFEncodeParam;
import com.sun.media.jai.codecimpl.TIFFImageEncoder;
import com.sun.media.jai.codec.TIFFField;

public class Png2TifConvert {
/**
*
* 功能描述: 图片转tif格式
*
* @param: [fileAbsolutePath]
* @return: java.lang.String
* @auther: KevinZc  <- 就是他
* @date: 2018/9/8 22:14
*/
public static String image2Tif(String fileAbsolutePath){
OutputStream outputStream = null;
String filterFilePath = null;
String tifFilePath = null;
ImageOutputStream ios = null;
try {
// 解决位深度太小 start ====注意:8位深度的图片会出现文件损坏问题
File picture = new File(fileAbsolutePath);
BufferedImage img = ImageIO.read(picture);
int colorSpaceType = img.getColorModel().getColorSpace().getType();
System.out.print(colorSpaceType);
// 统一进行一次过滤 转换成24位深度
filterFilePath = fileAbsolutePath.substring(0, fileAbsolutePath.lastIndexOf("."))+".png";
tifFilePath = filterFilePath.substring(0, filterFilePath.lastIndexOf("."))+".tif";
ios = ImageIO.createImageOutputStream(new File(filterFilePath));
ImageIO.write(ImageIO.read(picture),"png", ios);
// 解决位深度太小 end
FileSeekableStream stream = new FileSeekableStream(filterFilePath);
PlanarImage in = JAI.create("stream", stream);
OutputStream os = null;
os = new FileOutputStream(tifFilePath);
// 设置dpi为300
TIFFEncodeParam param = new TIFFEncodeParam();
param.setCompression(TIFFEncodeParam.COMPRESSION_NONE);
TIFFField[] extras = new TIFFField[2];
extras[0] = new TIFFField(282, TIFFTag.TIFF_RATIONAL, 1, (Object) new long[][]{{(long) 300, 1}, {0, 0}});
// extras[0] = new TIFFField(282, TIFFTag.TIFF_RATIONAL, 1, (Object) new long[][]{{(long) dpi, 1}, {0, 0}});
extras[1] = new TIFFField(283, TIFFTag.TIFF_RATIONAL, 1, (Object) new long[][]{{(long) 300, 1}, {0, 0}});
param.setExtraFields(extras);

TIFFImageEncoder enc = new TIFFImageEncoder(os, param);
try {
enc.encode(in);
os.flush();
os.close();
stream.close();
} catch (Exception e) {
// logger.error("{}",e );
throw new RuntimeException(e);
}
return tifFilePath;
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (outputStream != null) {
outputStream.close();
}
if (ios != null) {
ios.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}

public static void main(String[] args) {
// TODO Auto-generated method stub
image2Tif("1.png");
System.out.print("done");
}

}

cmyk色彩空间,拿ps转比较好,不过没有ps,黑白图是否会影响?不清楚。。

不过java可以实现RGB转CMYK色彩空间。首先看下图片是否是CMYK空间,通常不是,输出是6 ,CMYK是9

BufferedImage img = ImageIO.read(picture);
int colorSpaceType = img.getColorModel().getColorSpace().getType();
System.out.print(colorSpaceType);

cmyk和rgb应该也存在一定的转换关系,但是转换完了是否还能在电脑里显示?因为电脑是rgb的色彩空间吧、

参考另外一个大神的方法,下面是转载的link,谢谢分享!

https://blog.csdn.net/ybn187/article/details/52185269

public static String readImage(String filename) throws IOException {
File file = new File(filename);
ImageInputStream input = ImageIO.createImageInputStream(file);
Iterator readers = ImageIO.getImageReaders(input);
if(readers == null || !readers.hasNext()) {
throw new RuntimeException("1 No ImageReaders found");
}
ImageReader reader = (ImageReader) readers.next();
reader.setInput(input);
String format = reader.getFormatName() ;
BufferedImage image;

if ( "JPEG".equalsIgnoreCase(format) ||"JPG".equalsIgnoreCase(format) ) {
try {
// 尝试读取图片 (包括颜色的转换).
image = reader.read(0); //RGB
} catch (IIOException e) {
// 读取Raster (没有颜色的转换).
Raster raster = reader.readRaster(0, null);//CMYK
image = createJPEG4(raster);
}
image.getGraphics().drawImage(image, 0, 0, null);
String dstfilename = filename.substring(0,filename.lastIndexOf("."))+"_rgb"+filename.substring(filename.lastIndexOf("."));
String newfilename = filename;
File newFile = new File(dstfilename);
FileOutputStream out = new FileOutputStream(newFile);
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
encoder.encode(image);
out.flush();
out.close();
return dstfilename;
}
return null;
}

private static BufferedImage createJPEG4(Raster raster) {
int w = raster.getWidth();
int h = raster.getHeight();
byte[] rgb = new byte[w * h * 3];
//彩色空间转换
float[] Y = raster.getSamples(0, 0, w, h, 0, (float[]) null);
float[] Cb = raster.getSamples(0, 0, w, h, 1, (float[]) null);
float[] Cr = raster.getSamples(0, 0, w, h, 2, (float[]) null);
float[] K = raster.getSamples(0, 0, w, h, 3, (float[]) null);
for (int i = 0, imax = Y.length, base = 0; i < imax; i++, base += 3) {
float k = 220 - K[i], y = 255 - Y[i], cb = 255 - Cb[i],
cr = 255 - Cr[i];

double val = y + 1.402 * (cr - 128) - k;
val = (val - 128) * .65f + 128;
rgb[base] = val < 0.0 ? (byte) 0 : val > 255.0 ? (byte) 0xff
: (byte) (val + 0.5);

val = y - 0.34414 * (cb - 128) - 0.71414 * (cr - 128) - k;
val = (val - 128) * .65f + 128;
rgb[base + 1] = val < 0.0 ? (byte) 0 : val > 255.0 ? (byte) 0xff
: (byte) (val + 0.5);

val = y + 1.772 * (cb - 128) - k;
val = (val - 128) * .65f + 128;
rgb[base + 2] = val < 0.0 ? (byte) 0 : val > 255.0 ? (byte) 0xff
: (byte) (val + 0.5);
}
raster = Raster.createInterleavedRaster(new DataBufferByte(rgb, rgb.length), w, h, w * 3, 3, new int[]{0, 1, 2}, null);
ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_sRGB);
ColorModel cm = new ComponentColorModel(cs, false, true, Transparency.OPAQUE, DataBuffer.TYPE_BYTE);
return new BufferedImage(cm, (WritableRaster) raster, true, null);
}

public static String TestImg(String src) {
File imgsrc = new File(src);
try {
ImageIO.read(imgsrc);
} catch (IOException e) {
// TODO Auto-generated catch block

String msg = e.getMessage();
System.out.println("msg:"+msg);
if (msg.indexOf("Unsupported Image Type") == 0) {
try {
return readImage(src);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
} else {
e.printStackTrace();
return null;
}
}
return src;

}

png转tif的更多相关文章

  1. h5 hdf5 文件转 tif 流程

    由于需要对h5(hdf5)格式的dem数据进行拼接,但是arcgis不能识别h5的地理参考信息,所以先将h5文件转为带地理参考的tif文件,然后再进行拼接. 工具:arcgis+envi 1.用arc ...

  2. 为TIF、JPG图片添加地理坐标/平面直角坐标

    图片分辨率.(X方向像素数numX,Y方向像素数numY) 步骤: (1)在放图片的目录下新建TXT文本文档,将文件名改为与图片相同,扩展名改为jgw(JPG图片),(TIF要改为tfw). (2)用 ...

  3. andriod arcgis加载影像TIF

    private static final String TAG = "MainActivity"; private MapView mapView = null; @Overrid ...

  4. TIF、JPG图片手动添加地理坐标的方法(转载)

    题目:为TIF.JPG图片添加地理坐标/平面直角坐标. 图片来源:GOOGLE EARTH.(当然也可以是其他知道四角点坐标的图片) 截图工具:GEtscreen(此软件截图时可以自动生成图片四角点坐 ...

  5. Difference Between TIF and TIFF

    TIF vs TIFF Many people are confused with similar file extensions that only differ by a single lette ...

  6. tiff或tif文件的读取

    以下是VC下读取TIFF文件的代码 char* szFileName = "K:\\地图\\fujian-DEM\\fujian1.tif"; TIFF* tiff = TIFFO ...

  7. gdalwarp切割tif参数

    可以去gdal官网查询gdalwarp工具的参数,但是具体的还是不知道怎么写,例如内置数据类型-ot 和压缩-co参数. 这里有一个经过雁阵更可以使用的参数 gdalwarp -te lon1 lat ...

  8. libtiff 生成48位色tif图片

    BOOL CTifImage_48Bits::BitmapConvertTo48BitsTif(CString strImagePath, int nWidth, int nHeight, int n ...

  9. tif图片编辑利器

    http://www.onlinedown.net/soft/99112.htmTIF编辑器 0.4 http://www.zjda07.cn/软件类别:国产软件/图像处理软件大小:1089KB软件授 ...

  10. IE点击tif,tiff文件,提示打开而不是查找

    IE点击tif或者tiff后缀的文件,提示窗口没有显示打开,而是现实查找.而下载到本地后,又能用acdsee之类的软件双击打开.在tif文件右键-属性中选择了打开程序,在IE中还是依然. 搜索网络资料 ...

随机推荐

  1. MSCKF_VIO:MSCKF的双目版本

    论文:MSCKF的双目版本 Robust Stereo Visual Inertial Odometry for Fast Autonomous Flight 下载地址:点击 源码地址:https:/ ...

  2. hash 位运算 练习

    hash  位运算 [以下代码仅做位运算的练习,算法本身不合理  php转译python] 从头到尾彻底解析Hash表算法_知识库_博客园 https://kb.cnblogs.com/page/18 ...

  3. 将lits集合转化为树状结构

    一,bean的类型: public class DeptListRES { /** * 子节点 */ private List<DeptListRES> children; private ...

  4. linux之tail和head的使用

    tail 基本介绍 用于显示文件的结尾的内容.在默认情况下,taild命令显示文件的后10行内容 表达式 tail [options] [filenames] 常用参数 -c:输出最后N个字节 -f: ...

  5. python练习题-day8

    1.有如下文件,a1.txt,里面的内容为: 老男孩是最好的培训机构, 全心全意为学生服务, 只为学生未来,不为牟利. 我说的都是真的.哈哈 分别完成以下的功能: a,将原文件全部读出来并打印. wi ...

  6. spring声明式事务管理方式( 基于tx和aop名字空间的xml配置+@Transactional注解)

    1. 声明式事务管理分类 声明式事务管理也有两种常用的方式, 一种是基于tx和aop名字空间的xml配置文件,另一种就是基于@Transactional注解. 显然基于注解的方式更简单易用,更清爽. ...

  7. __call__方法和Flask中 first_or_404

    1.__call__方法: 在一个类的实例中,函数一般都是可调用的对象: __call__方法时魔法方法,该方法允许程序员创建可调用的对象(实例),默认情况下是不会触发,也就是说,大多数实例是不可被调 ...

  8. 20189203《Linux内核原理与分析》第一周作业

    实验一 Linux 系统简介 我在这一课中主要学习了Linux是什么,Linux的产生和发展历史,Linux发展中的重要人物以及Linux和Windows在是否收费.软件与支持.安全性等方面存在的一些 ...

  9. [macOS] error when brew updating

    I want to update the brew, then run brew update but unluckly, i got these error /usr/local/Library/b ...

  10. Kubernetes持久化存储2——探究实验

    目录贴:Kubernetes学习系列 一.简介 本文在“创建PV,创建PVC挂载PV,创建POD挂载PVC”这个环境的基础上,进行各种删除实验,并记录.分析各资源的状态. 二.实验脚本 实验创建了一个 ...