[PYTHON] Vorsichtsmaßnahmen für den Umgang mit PNG- und JPG-Bildern

Es gab mehrere Male, in denen der Fehler nicht behoben werden konnte, da die Abmessungen bei der Bildverarbeitung nicht übereinstimmten. PNG-Bilder und JPG-Bilder müssen nicht nur ihre Erweiterungen ändern. Grundsätzlich war jpg ein RGB-Farbbild und png hatte ein Bild, das automatisch mit Transparenz in RGBA konvertiert wurde, aber das ist nicht der Fall.

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)

Ergebnis


<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)

Liste der Kissenbibliotheksmodi

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)

Wenn ich es mit meinem eigenen Bild versuche, befinden sich sowohl png als auch jpg im selben rgba-Modus.

Ergebnis


<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)

Die Konvertierung erfolgt also von Anfang an.

Konvertieren Sie PNG in den P-Modus

python


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

Konvertieren Sie von PNG nach JPG

python


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

Es wurde eine ähnliche Dimension.

Ergebnis


<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)

Die Dimension, die Sie ausführen möchten


<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)

Quelle

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

Vorsichtsmaßnahmen für den Umgang mit PNG- und JPG-Bildern
[Version 2020] Entwicklungsverfahren für Personal Crawler und seine Vorsichtsmaßnahmen
Einfache Verwaltungs-App für heruntergeladene Bilder und Gesichtsbilder
Vorsichtsmaßnahmen für cv2.cvtcolor
[Hikari-Python] Kapitel 07-01 Ausnahmebehandlung (Fehler und Ausnahmen)
Vorsichtsmaßnahmen beim Umgang mit Luigi
Pandas-Grundlagen für Anfänger ④ Umgang mit Datums- und Zeitangaben