» »

[Pillow] Kako shranit PNG (mode P) sliko v JPEG?

[Pillow] Kako shranit PNG (mode P) sliko v JPEG?

HotBurek ::

Dobro zgodnje jutro.

Evo, fantje in dekline, nov dan, nov izziv.

Source image je tole:

Zdej me pa zanima, kako konvertat to sliko v jpeg format, in to tako, da tist "transparenten" background okrog konzerve ne postane zelen (ali črn)?

Trenutni sample:

import io;
import PIL.Image;

image = PIL.Image.open("/home/sepultura/arise/minerva_in.png");

print("image mode [1]: " + str(image.mode));
print("image format [1]: " + str(image.format));

plan = "a";

if image.mode in ["1", "8", "L", "P", "CMYK", "RGBA", "LA"]:

    # plan a
    if plan == "a":

        image = image.convert("RGB");

    # plan b
    elif plan == "b":

        if image.format == "PNG":

            image = image.convert("RGBA");

        else:

            image = image.convert("RGB");

    print("image mode [2]: " + str(image.mode));
    print("image format [2]: " + str(image.format));

image.save("/home/sepultura/arise/minerva_out.jpeg", format="JPEG", optimize=True);

image.close();

Če grem plan a, dobim na output-u warning:

UserWarning: Palette images with Transparency expressed in bytes should be converted to RGBA images

Print:

image mode [1]: P
image format [1]: PNG
image mode [2]: RGB
image format [2]: None

In končno sliko:



Če grem plan b, dobim na output-u error:

OSError: cannot write mode RGBA as JPEG

Print:

image mode [1]: P
image format [1]: PNG
image mode [2]: RGBA
image format [2]: None

Že od prej imam link na tole: PIL remove error "UserWarning: Palette images with Transparency expressed in bytes should be converted to RGBA images"

No, zdej moram to nekako rešit. Kako PNG sliko, ki je v mode P, shranit v JPEG format? Pa da bo lepo zgledalo, kot na izvoru.
root@debian:/# iptraf-ng
fatal: This program requires a screen size of at least 80 columns by 24 lines
Please resize your window
  • spremenilo: HotBurek ()

socialec ::

jpeg podpira transparenco?
kolikor vidim, hitri queryji pravijo, da ne...
menda je rešitev, da zmergaš na fiksen background image

Liker ::

if image.format == "PNG":
    image = image.convert("RGBA");
    bg = Image.new('RGBA', image.size, (0, 155, 0, 0))
    final_image = Image.alpha_composite(bg,image)
    ...

No comment ... :(

HotBurek ::

Tvoj sample ne dela, ker:

OSError: cannot write mode RGBA as JPEG

Delujočo rešitev sem našel tule:

How to Covert PNG to JPEG using Pillow while image color is black?

To z "background" rešitvijo že imam za resize. Zdej pa še za PNG P situacijo. Moram dodelat še, da RGBA konverta v RGB, ter LA v L.
root@debian:/# iptraf-ng
fatal: This program requires a screen size of at least 80 columns by 24 lines
Please resize your window

Zgodovina sprememb…

  • spremenilo: HotBurek ()

cekr ::

Uporabljaj raje TIFF.
Sinclair ZX Spectrum [Zilog Z80A - 3.5 MHz, 48kB, dvojni kasetofon,
TV-OUT, radirke, Sinclair-Basic], Sinclair ZX-81 [Z80A, 3.25MHZ, 1kB]

no comment ::

Nekateri formati se uporabljajo z razlogom. Če moraš imeti tranparency oz. alpha layer, izbereš ustrezen format.

Liker ::

*sigh*

Sem prebral, da želiš zeleno ozadje. JPEG ne podpira prosojnosti, saj to veš, ne?
In moja koda ni bila popolna. Če želiš shraniti JPEG, moraš najprej narediti nazaj convert v RGB mode. Na primer:
>>> from PIL import Image
>>> img = Image.open("Minerva-in.png")
>>> img = img.convert("RGBA")
>>> bg = Image.new("RGBA",img.size,(255,255,255,255))
>>> end = Image.alpha_composite(bg,img)
>>> end_jpg = end.convert("RGB")
>>> end_jpg.save("Minerva-out.jpeg")

To naredi JPG z belim ozadjem, kar si si verjetno želel.

Je pa python koda s podpičji res zakon. Thumbs up!

HotBurek ::

Ja ja, to. Da se transparentnost iz PNG zamenja v belo "ozadje" pri shranjevanju v JPEG.

Takole imam sedaj. Sem potestiral in dela, kot je željeno. Verjamem pa, da so še tudi druge/drugačne rešitve.

# UserWarning: Palette images with Transparency expressed in bytes should be converted to RGBA images
# https://stackoverflow.com/questions/70839890/pil-remove-error-userwarning-palette-images-with-transparency-expressed-in-byt
if image.format == "PNG":

    # set new mode for background
    new_mode = "RGB";

    # check if this is LA
    if image.mode == "LA":

        # set new mode to L
        new_mode = "L";

    # logging
    logging(loginfo, "PIL > new_mode: " + str(new_mode));

    # logging
    logging(loginfo, "PIL > Convert image from " + str(image.mode) + " mode to RGBA");

    # convert image from x to RGBA
    image = image.convert("RGBA");

    # make white background
    background = PIL.Image.new(new_mode, (image.width, image.height), (255, 255, 255));

    # put all together
    # https://stackoverflow.com/questions/50739732/how-to-covert-png-to-jpeg-using-pillow-while-image-color-is-black
    background.paste(image, image);

    # save back as image
    image = background;
root@debian:/# iptraf-ng
fatal: This program requires a screen size of at least 80 columns by 24 lines
Please resize your window

Zgodovina sprememb…

  • spremenilo: HotBurek ()

socialec ::

Odlično.


Vredno ogleda ...

TemaSporočilaOglediZadnje sporočilo
TemaSporočilaOglediZadnje sporočilo
»

[Python] PIL.Image resize in slaba kvaliteta

Oddelek: Programiranje
5683 (476) DamijanD
»

word - obrezovanje slike (kvaliteta)

Oddelek: Pomoč in nasveti
71816 (1675) harvey
»

[DirectX] Problem s teksturami

Oddelek: Programiranje
6733 (631) Senitel
»

uporaba C++ unmanaged kode v C#.NET projektu

Oddelek: Programiranje
111973 (1696) krho
»

[java] transparentnost

Oddelek: Programiranje
91265 (1039) jpzoky1

Več podobnih tem