发国外的文章要求图片是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;

}

python怎么将png转为tif_png转tif相关推荐

  1. python中object转str_python的id()函数介绍 python怎么将objectid转为str

    python id函数的输出值问题 Python中id函数为什么会对不同对象返回相同值如果那两个对象指向的是相同的内存地址,也就是说内部指向的其实是一个对象,那他们的ID返回值就是一样的. pytho ...

  2. Python:pmml格式文件的简介、安装、使用方法(利用python将机器学习模型转为Java常用的pmml格式文件)之详细攻略

    Python:pmml格式文件的简介.安装.使用方法(利用python将机器学习模型转为Java常用的pmml格式文件)之详细攻略 目录 pmml格式文件的简介 1.PMML结构 pmml安装 pmm ...

  3. python将txt文件转为excel格式以及写入excel超过65536行报错问题解决方法

    参考链接: https://blog.csdn.net/levy_cui/article/details/82252183 https://blog.csdn.net/levy_cui/article ...

  4. python怎么将txt转为excel_使用matlab或python将txt文件转为excel表格

    假设txt文件为: 一.matlab代码 data=importdata('data.txt'); xlswrite('data.xls',data); 二.python代码 利用pandas的Dat ...

  5. Python将txt文件转为json文件

    python将txt文件转为json文件 txt文件中内容: [*]www.xiaoyang.1 [*]www.xiaoyang.12 [*]www.xiaoyang.135 [*]www.xiaoy ...

  6. python小工具—图片转为字符txt

    python小工具-图片转为字符txt 图片转为字符txt python小工具-图片转为字符txt 效果展示 转换图片信息 图片信息转字符 完整代码 效果展示 转换图片信息 将图片的rgb色彩信息转为 ...

  7. Python Gdal 栅格数据处理之hgt转tif数据

    Python Gdal 栅格数据处理之.hgt转.tif数据 1 介绍 2 Python代码 3 结果展示 1 介绍   SRTM是地形数据,其存储形式为.hgt格式,根据精度的不同可以分为两种:SR ...

  8. Python将日期月份转为英文和英文转为数字月份

    Python将日期月份转为英文和英文转为数字月份 import calendar date_list = [1,2,3,4,5,6,7,8,9,10,11,12]#数字转为月份简写 for i in ...

  9. python列表怎么转成数字,Python中列表元素转为数字的方法分析

    本文实例讲述了Python中列表元素转为数字的方法.分享给大家供大家参考,具体如下: 有一个数字字符的列表: numbers = ['1', '5', '10', '8'] 想要把每个元素转换为数字: ...

最新文章

  1. 20165206 2017-2018-2 《Java程序设计》第三周学习总结
  2. 专家解读 | 数据中心,从“电老虎”走向“数字经济发动机”
  3. Mybatis怎么能看是否执行了sql语句
  4. c语言程序设计基础的考试题,c语言程序设计基础的考试题.doc
  5. GARFIELD@10-21-2004
  6. MySQL 5.5.31 procedure 的语法规则细节
  7. Spring Security Oauth2系列(一)
  8. 数据库管理(事务、ACID、并发、封锁、可串行化、隔离)
  9. boolean到底占几个字节?
  10. 数据库事务及四大特性
  11. Django模板语言DTL中的变量和标签
  12. 楚留香冰最新服务器,一梦江湖:各门派冰雪外观极寒之刃上线,冰晶透亮玩家直呼绝了!...
  13. 微信公众号与小程序(二十——关于我如何莫名其妙建了一个电影搜索机器人这件事)
  14. wtc java 代码 tpcall(servicename_[转载]Dorado+Spring+Wtc+Tuxedo开发
  15. docker php 环境实操
  16. 面试宝典(一)之程序员必备面试软技能
  17. 和讯网债券数据Python爬取保存成CSV文件之一
  18. 金蝶K3 WISE 15.1金蝶K3 15.1金蝶K3 V15.1 金蝶K3 WISE 15.0金蝶K3 15.0金蝶K3 V15.0 14.3/14.2/14.1/14.0/13.1/13.0 12
  19. 【CJY学习笔记】Linux防火墙基本操作(基于centos7)
  20. 【四轴飞行器】【电机部分】PWM驱动空心杯转速

热门文章

  1. 【NPOI】.NET EXCEL导入导出开发包
  2. 控制好你的 Wordpress 侧边栏
  3. arch模型的思路_ARCH模型
  4. java中date类型如何赋值_一文读懂java中的Reference和引用类型
  5. 查看登录oracle信息,记录Oracle用户的登录信息
  6. excel填充序列_excel如何快速填充数据
  7. SpringMVC跳转页面默认类型和转发、重定向的使用
  8. php 验证url,php过滤器filter验证邮箱、url和ip地址等
  9. php5.6 交叉编译,Cross-compile - 龙芯开源社区
  10. mysql 查询语句执行顺序_MySQL 查询语句执行过程