日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python怎么将png转为tif_png转tif

發布時間:2024/10/6 python 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python怎么将png转为tif_png转tif 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

發國外的文章要求圖片是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的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。