Untitled
unknown
plain_text
2 years ago
3.0 kB
6
Indexable
from PIL import Image
import numpy as np
import matplotlib.pyplot as plt
#zadanie 1
obraz = Image.open("obraz.jpg").copy()
inicjaly = Image.open("inicjaly.bmp").convert("RGB").copy()
#zadanie 2
#a
def wstaw_inicjaly(obraz, inicjaly, m, n, kolor):
width, height = inicjaly.size
target_x = obraz.width - width
target_y = obraz.height - height
for i in range(width):
for j in range(height):
pixel = inicjaly.getpixel((i, j))
if pixel != (0, 0, 0):
obraz.putpixel((target_x + i + m, target_y + j + n), kolor)
czerwony = (255, 0, 0)
wstaw_inicjaly(obraz, inicjaly, 0, 0, czerwony)
obraz.save("obraz1.png")
#b
def wstaw_inicjaly_maska(obraz, inicjaly, m, n, x, y, z):
obraz_kopia = obraz.copy()
w, h = obraz.size
inicjaly = inicjaly.convert("RGB")
iw, ih = inicjaly.size
start_x = max((w - iw) // 2, 0)
start_y = max((h - ih) // 2, 0)
for i in range(iw):
for j in range(ih):
if start_x + i + m < w and start_y + j + n < h:
p = inicjaly.getpixel((i, j))
if p == (0, 0, 0):
current_pixel = obraz.getpixel((start_x + i + m, start_y + j + n))
new_pixel = tuple(c + offset for c, offset in zip(current_pixel, (x, y, z)))
obraz_kopia.putpixel((start_x + i + m, start_y + j + n), new_pixel)
return obraz_kopia
nowy_obraz = wstaw_inicjaly_maska(obraz, inicjaly, 0, 0, 50, 30, 20)
nowy_obraz.save("obraz2.png")
#zadanie 3
def wstaw_inicjaly_load(obraz, inicjaly, m, n, kolor):
obraz1 = obraz.copy()
width, height = inicjaly.size
w, h = obraz.size
inicjaly = inicjaly.convert("RGB")
target_x = w - width
target_y = h - height
for i in range(width):
for j in range(height):
if i + m < w and j + n < h:
pixel = inicjaly.getpixel((i, j))
if pixel != (0, 0, 0):
obraz1.putpixel((target_x + i + m, target_y + j + n), kolor)
return obraz1
def wstaw_inicjaly_maska(obraz, inicjaly, m, n, x, y, z):
obraz1 = obraz.copy()
width, height = inicjaly.size
w, h = obraz.size
inicjaly = inicjaly.convert("RGB")
target_x = max((w - width) // 2, 0)
target_y = max((h - height) // 2, 0)
for i in range(width):
for j in range(height):
if target_x + i + m < w and target_y + j + n < h:
pixel = inicjaly.getpixel((i, j))
if pixel == (0, 0, 0):
current_pixel = obraz.getpixel((target_x + i + m, target_y + j + n))
new_pixel = tuple(c + offset for c, offset in zip(current_pixel, (x, y, z)))
obraz1.putpixel((target_x + i + m, target_y + j + n), new_pixel)
return obraz1
nowy_obraz = wstaw_inicjaly_load(obraz, inicjaly, 0, 0, (255, 0, 0))
nowy_obraz_maska = wstaw_inicjaly_maska(obraz, inicjaly, 0, 0, 50, 30, 20)Editor is loading...
Leave a Comment