2014-01-06 9 views
0

ImageIO로 jpg를 읽은 다음 이미지의 크기를 조정 한 후 PNG로 쓰려고합니다.PNG-8881 대신 PNG-8888을 작성하는 방법은 무엇입니까?

BufferedImage img = ImageIO.read(new FileInputStream("/u/my.jpg")); 
    BufferedImage out = new BufferedImage(img.getWidth(), img.getHeight(), BufferedImage.TYPE_INT_ARGB); 
    Graphics2D g = out.createGraphics(); 
    g.drawImage(img, 0, 0, img.getWidth()/2, img.getHeight()/2, null); 
    g.dispose(); 
    ImageIO.write(out, "png", new FileOutputStream("/u/my2.png")); 

내가 가진 문제는 이것이 PNG-8881 (바이너리/비트 마스크 알파 레이어) 결과이다 :

여기 내 코드입니다. 알파 용으로 1 비트 대신 알파 용으로 8 비트로 된 PNG-8888로 끝내고 싶습니다.

Image: /u/my2.png 
Format: PNG (Portable Network Graphics) 
Class: DirectClass 
Geometry: 2848x4288+0+0 
Units: Undefined 
Type: TrueColorAlpha 
Endianess: Undefined 
Colorspace: sRGB 
Depth: 8-bit 
Channel depth: 
    red: 8-bit 
    green: 8-bit 
    blue: 8-bit 
    alpha: 1-bit 
Channel statistics: 
    Red: 
    min: 0 (0) 
    max: 252 (0.988235) 
    mean: 42.2154 (0.165551) 
    standard deviation: 84.6142 (0.33182) 
    kurtosis: 1.41395 
    skewness: 1.77533 
    Green: 
    min: 0 (0) 
    max: 252 (0.988235) 
    mean: 45.1979 (0.177247) 
    standard deviation: 86.0758 (0.337552) 
    kurtosis: 0.935575 
    skewness: 1.62013 
    Blue: 
    min: 0 (0) 
    max: 255 (1) 
    mean: 46.4455 (0.182139) 
    standard deviation: 87.2088 (0.341995) 
    kurtosis: 0.742603 
    skewness: 1.56331 
    Alpha: 
    min: 0 (0) 
    max: 255 (1) 
    mean: 63.75 (0.25) 
    standard deviation: 110.418 (0.433013) 
    kurtosis: -0.666667 
    skewness: -1.1547 
Image statistics: 
    Overall: 
    min: 0 (0) 
    max: 255 (1) 
    mean: 81.2772 (0.318734) 
    standard deviation: 92.6906 (0.363492) 
    kurtosis: 0.642375 
    skewness: 1.36843 
Alpha: none #00000000 
Rendering intent: Perceptual 
Gamma: 0.454545 
Chromaticity: 
    red primary: (0.64,0.33) 
    green primary: (0.3,0.6) 
    blue primary: (0.15,0.06) 
    white point: (0.3127,0.329) 
Background color: white 
Border color: srgba(223,223,223,1) 
Matte color: grey74 
Transparent color: none 
Interlace: None 
Intensity: Undefined 
Compose: Over 
Page geometry: 2848x4288+0+0 
Dispose: Undefined 
Iterations: 0 
Compression: Zip 
Orientation: Undefined 
Properties: 
    date:create: 2014-01-06T12:15:38-06:00 
    date:modify: 2014-01-06T12:15:38-06:00 
    png:IHDR.bit-depth-orig: 8 
    png:IHDR.bit_depth: 8 
    png:IHDR.color-type-orig: 6 
    png:IHDR.color_type: 6 (RGBA) 
    png:IHDR.interlace_method: 0 (Not interlaced) 
    png:IHDR.width,height: 2848, 4288 
    png:sRGB: intent=0 (Perceptual Intent) 
    signature: f1cd61ef11890cb9981e30787ee13f9a27e0836b4b634fa7b9daf2f6bb14de66 
Artifacts: 
    filename: /u/my2.png 
    verbose: true 
Tainted: False 
Filesize: 3.321MB 
Number pixels: 12.21M 
Pixels per second: 55.51MB 
User time: 0.210u 
Elapsed time: 0:01.219 
Version: ImageMagick 6.8.6-6 2013-10-12 Q16 http://www.imagemagick.org 

내가 PNG 전체 8 비트 알파 32 비트 PNG 파일이어야 할 필요가 : 여기

은 이미지 마법 (magick) 출력을 확인합니다. 어떻게해야합니까?

감사합니다.

그래, @haraldK의 의견 중 하나를 읽은 후.

업데이트 코드 :

public static void main(String[] args) throws IOException{ 

    BufferedImage img = ImageIO.read(new FileInputStream("/u/my.jpg")); 
    BufferedImage out = new BufferedImage(img.getWidth()/2, img.getHeight()/2, BufferedImage.TYPE_INT_ARGB); 
    Graphics2D g2D = out.createGraphics(); 
    g2D.drawImage(img, 0, 0, img.getWidth()/2, img.getHeight()/2, null); 

    int w = out.getWidth(); 
    int h = out.getHeight(); 

    g2D.dispose(); 

    WritableRaster ras = out.getColorModel().createCompatibleWritableRaster(out.getWidth(), out.getHeight()); 
    BufferedImage out2 = new BufferedImage(out.getColorModel(), ras, false, null); 

    ColorModel model = out.getColorModel(); 

    for (int i = 0; i < h; i++) { 
     for (int j = 0; j < w; j++) { 
      int data = out.getRGB(j, i); 
      int pixel = model.getRGB(data); 

      int a = pixel >> 24 & 0xff; 
      int r = pixel >> 16 & 0xff; 
      int g = pixel >> 8 & 0xff; 
      int b = pixel & 0xff; 

      Color color = new Color(r, g, b, a); 
      if(i==25 && j==25){ 
       out2.setRGB(j, i, new Color(111,111,111,126).getRGB()); 
      }else{ 
       out2.setRGB(j, i, pixel); 
      } 

     } 
    } 

    ImageIO.write(out2, "png", new FileOutputStream("/u/my126.png")); 

} 

그리고 이미지 파일에 식별 실행 한 새로운 출력 :

Image: /u/my126.png 
Format: PNG (Portable Network Graphics) 
Class: DirectClass 
Geometry: 1424x2144+0+0 
Units: Undefined 
Type: TrueColorAlpha 
Endianess: Undefined 
Colorspace: sRGB 
Depth: 8-bit 
Channel depth: 
    red: 8-bit 
    green: 8-bit 
    blue: 8-bit 
    alpha: 8-bit 
Channel statistics: 
    Red: 
    min: 34 (0.133333) 
    max: 252 (0.988235) 
    mean: 168.862 (0.662203) 
    standard deviation: 85.1618 (0.333968) 
    kurtosis: -1.63932 
    skewness: -0.419596 
    Green: 
    min: 42 (0.164706) 
    max: 252 (0.988235) 
    mean: 180.791 (0.708986) 
    standard deviation: 71.5681 (0.280659) 
    kurtosis: -1.51545 
    skewness: -0.454524 
    Blue: 
    min: 44 (0.172549) 
    max: 255 (1) 
    mean: 185.782 (0.728557) 
    standard deviation: 67.3447 (0.264097) 
    kurtosis: -1.44714 
    skewness: -0.500635 
    Alpha: 
    min: 126 (0.494118) 
    max: 255 (1) 
    mean: 255 (1) 
    standard deviation: 0.0738282 (0.000289522) 
    kurtosis: 3.05305e+06 
    skewness: 1747.3 
Image statistics: 
    Overall: 
    min: 0 (0) 
    max: 255 (1) 
    mean: 133.859 (0.524936) 
    standard deviation: 65.0189 (0.254976) 
    kurtosis: 5.00426 
    skewness: -0.456343 
Rendering intent: Perceptual 
Gamma: 0.454545 
Chromaticity: 
    red primary: (0.64,0.33) 
    green primary: (0.3,0.6) 
    blue primary: (0.15,0.06) 
    white point: (0.3127,0.329) 
Background color: white 
Border color: srgba(223,223,223,1) 
Matte color: grey74 
Transparent color: none 
Interlace: None 
Intensity: Undefined 
Compose: Over 
Page geometry: 1424x2144+0+0 
Dispose: Undefined 
Iterations: 0 
Compression: Zip 
Orientation: Undefined 
Properties: 
    date:create: 2014-01-06T17:08:21-06:00 
    date:modify: 2014-01-06T17:08:21-06:00 
    png:IHDR.bit-depth-orig: 8 
    png:IHDR.bit_depth: 8 
    png:IHDR.color-type-orig: 6 
    png:IHDR.color_type: 6 (RGBA) 
    png:IHDR.interlace_method: 0 (Not interlaced) 
    png:IHDR.width,height: 1424, 2144 
    png:sRGB: intent=0 (Perceptual Intent) 
    signature: 8bb6e37dbde980af0296dfacebff27b217360aac8994dfad8e90efeecac3bbf2 
Artifacts: 
    filename: /u/my126.png 
    verbose: true 
Tainted: False 
Filesize: 3.01MB 
Number pixels: 3.053M 
Pixels per second: 20.35MB 
User time: 0.090u 
Elapsed time: 0:01.150 
Version: ImageMagick 6.8.6-6 2013-10-12 Q16 http://www.imagemagick.org 

기본적으로이 일어나는 것입니다. 식별 할 때 모든 알파 값에 대해 255 만 읽으면 이미지 파일 8881의 이름이 잘못 지정되지만 한 픽셀 값을 임의의 투명도 그라데이션으로 변경하면 8888로 PNG 파일을 올바르게 인식합니다.

+0

해당 이미지를 게시 할 수 있습니까? 그것은 '신원 확인'이 잘못되었다고 보이며 알파는 실제로 8 비트입니다 ("채널 통계"를 보면 Alpha max는 255이며 8 비트 값이어야 함). – haraldK

+0

JPG는 투명도를 지원하지 않습니다. 투명도를 지원할 수 있습니까? 기본적으로 존재하지 않는 정보는 얻을 수 없습니다. 나는 그것이 8 비트로 패딩 될 수 있다고 가정하지만 파일 크기는 더 커질 것입니다. 다시 말하지만, 처음부터 JPEG를 PNG로 변환하는데도 동일한 방법을 적용 할 수 있다고 생각합니다. – turbo

+0

결과 이미지의 모든 단일 픽셀의 알파 값이 255이기 때문에 식별 결과가 올바르지 않으므로이 값이 2 진 값이 아닙니다. 감사. –

답변

0

identify 출력에 약간의 불일치가있을 수 있습니다. 이것은 identify이 잘못되었다거나 적어도 우리가 기대하는 것을 표시하지 않는다고 믿습니다. 이미지의 알파 채널은 실제로 8 비트입니다 ("채널 통계"를 보면 알파 최대는 255입니다. , 255는 8 비트 값이어야합니다).

이것은 또한 추가 디버깅과 일치하는 것으로 보입니다. 결론적으로, 이미지는 정확히 8 비트 알파를 가진 32 비트 PNG입니다. :-)