There were several times when the error could not be resolved because the dimensions did not match when performing image processing. png and jpg images don't just have different extensions. Basically, jpg is an RGB color image and png has an image that is automatically converted to RGBA with transparency, but that is not the case.
python
# -*- 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)
result
<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)
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)
When I try it with my own image, both png and jpg are in the same rgba mode.
result
<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)
So the conversion is done from the beginning.
Convert png to P mode
python
path_png = "0003.png "
path_jpg = "0003.jpg "
image = Image.open(path_png).convert('P')
image.save(path_png)
Convert from png to jpg
python
rgb_im = Image.open(path_png).convert('RGB')
rgb_im.save(path_jpg)
It became a similar dimension.
result
<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)
The dimension you want to do
<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)
python
# -*- 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)
Recommended Posts