LoginSignup
48

More than 5 years have passed since last update.

png画像とjpg画像の取り扱いの注意点

Posted at

画像処理をするときに次元が合わないでエラーが解決できないことが何度かあった。
png画像とjpg画像はただ拡張子を変えればいい訳ではない。
基本的にjpgはRGBのカラー画像でpngは透過を加えたRGBAに勝手に変換されるイメージを持っていたがそうではない。

# -*- coding: utf-8 -*-                                                                                                                                       
import os
import cv2
import sys
import numpy as np
from PIL import Image

path_jpg = "CMP_facade_DB_base/base/cmp_b0116.jpg"
path_png = "CMP_facade_DB_base/base/cmp_b0116.png"

image = np.asarray(Image.open(path_jpg))
print(Image.open(path_jpg))
print(image.shape)
image = np.asarray(Image.open(path_png))
print(Image.open(path_png))
print(image.shape)

結果
<PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=804x1024 at 0x7F5933E60F90>
(1024, 804, 3)
<PIL.PngImagePlugin.PngImageFile image mode=P size=804x1024 at 0x7F5933E60FD0>
(1024, 804)

pillowライブラリのmode一覧

mode
1 (1-bit pixels, black and white, stored with one pixel per byte)
L (8-bit pixels, black and white)
P (8-bit pixels, mapped to any other mode using a color palette)
RGB (3x8-bit pixels, true color)
RGBA (4x8-bit pixels, true color with transparency mask)
CMYK (4x8-bit pixels, color separation)
YCbCr (3x8-bit pixels, color video format)
Note that this refers to the JPEG, and not the ITU-R BT.2020, standard
LAB (3x8-bit pixels, the L*a*b color space)
HSV (3x8-bit pixels, Hue, Saturation, Value color space)
I (32-bit signed integer pixels)
F (32-bit floating point pixels)

で自分の画像で試してみるとpngもjpgも同じrgba modeになってる。

結果
<PIL.PngImagePlugin.PngImageFile image mode=RGBA size=826x1169 at 0x7F0DF1873F50>
(1169, 826, 4)
<PIL.PngImagePlugin.PngImageFile image mode=RGBA size=494x699 at 0x7F0DF1873ED0>
(699, 494, 4)

ということで変換を初めからする。

pngをP modeに変換

path_png = "0003.png"
path_jpg = "0003.jpg"
image = Image.open(path_png).convert('P')
image.save(path_png)

pngからjpgに変換する

rgb_im = Image.open(path_png).convert('RGB')
rgb_im.save(path_jpg)

似た感じの次元になった。

結果
<PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=826x1169 at 0x111215190>
(1169, 826, 3)
<PIL.PngImagePlugin.PngImageFile image mode=P size=826x1169 at 0x111215210>
(1169, 826)
やりたい次元
<PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=804x1024 at 0x7FA58C2E0F90>
(1024, 804, 3)
<PIL.PngImagePlugin.PngImageFile image mode=P size=804x1024 at 0x7FA58C2E0FD0>
(1024, 804)

ソース

# -*- coding: utf-8 -*-
import os
import cv2
import sys
import numpy as np
from PIL import Image

path_png = "0002.png"
path_jpg = "0002.jpg"

image = Image.open(path_png).convert('P')
image.save(path_png)
rgb_im = Image.open(path_png).convert('RGB')
rgb_im.save(path_jpg)

print(image)
print(rgb_im)
print(np.asarray(image).shape)
print(np.asarray(rgb_im).shape)

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
48