0 dní v řadě · 0 bodů

Tahák · Python · Užitečné knihovny

Pillow: úprava obrázků

Ořízneš, zmenšíš, otočíš a složíš obrázky kódem, pochopíš pixely, režimy a formáty a zpracuješ celou složku fotek naráz.

Otevřít celou lekciVšechny taháky
python
from PIL import Image, ImageOps, ImageDraw   # pip install pillow

img = Image.open("foto.jpg")                 # lazy, reads the header
img = Image.new("RGB", (800, 600), "white")
img.size, img.width, img.height, img.mode

img.crop((left, top, right, bottom))         # new image, right/bottom excluded
img.resize((w, h))                           # new image, exact size
img.thumbnail((w, h))                        # IN PLACE, keeps ratio, returns None
img.rotate(90, expand=True)                  # counterclockwise
img.convert("L")                             # RGB, RGBA, L, 1, P
ImageOps.exif_transpose(img)                 # apply phone rotation
ImageOps.fit(img, (w, h))                    # exact size, crops the rest

img.paste(logo, (x, y), logo)                # third argument = transparency mask
ImageDraw.Draw(img).rectangle((x1, y1, x2, y2), fill="red")
img.convert("RGB").save("out.jpg", quality=85)