Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added home_task/img/small-image.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
21 changes: 21 additions & 0 deletions home_task/task1_flip.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#task1_flip.py

# flip by X

from PIL import Image

img = Image.open("img/small-image.jpg")
ps = img.load()
w, h = img.size


last_pixel_index = w-1
for i in range(w//2):
for j in range(h):
p1 = ps[i, j]
p2 = ps[last_pixel_index-i, j]
ps[last_pixel_index-i, j] = p1
ps[i, j] = p2


img.show()
36 changes: 36 additions & 0 deletions home_task/task2_chessboard.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#task2_chessboard.py

# шахматная доска

from PIL import Image

# длина/ширина кубика
BLOCK_LENGTH = 50



img = Image.open("img/small-image.jpg")
ps = img.load()
w, h = img.size


def is_white(x, y):
width_even = (x // BLOCK_LENGTH) % 2 == 0
height_even = (y // BLOCK_LENGTH) % 2 == 0
# if both are even or both are odd
return width_even == height_even


for i in range(w):
for j in range(h):
p = ps[i, j]
# здесь можно поиграться с цветами, например,
# чтобы кубики были рандомного (или определённого) цвета, если они "белые" и т. п.
if (is_white(i, j)):
p = p[0]//2, p[1]//2, p[2]//2
else:
p = p[0]*2, p[1]*2, p[2]*2
ps[i, j] = p


img.show()
56 changes: 56 additions & 0 deletions home_task/task3.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#task3.py

from PIL import Image
from math import sin, cos


turns = int(input('Введите градус скручивания (+-): ')) # обороты (окружности (незамкнутые))
if turns == 0: raise ValueError('Cannot be zero')
ROTATE_DEGREE = turns * 360
WEIGHT = 3 # толщина заливки

class SimplePen:
def __init__(self, weight, draw_point):
self.weight = weight // 2
self.draw_point = draw_point
def draw(self, x, y):
for i in range(x - self.weight, x + self.weight + 1):
for j in range(y - self.weight, y + self.weight + 1):
self.draw_point(i, j)



img = Image.open("img/small-image.jpg")
ps = img.load()
w, h = img.size


centerX, centerY = w//2, h//2
diagonal = int((w*w + h*h)**.5 // 2) # диагональ от центра до угла

# рост радиуса (фикс. зн.) и сам радиус (переменная (на матем. языке))
r_inc = diagonal / ROTATE_DEGREE
r = r_inc

last_w = w-1
last_h = h-1
ensure_x = lambda x: 0 if x < 0 else last_w if x > last_w else x
ensure_y = lambda y: 0 if y < 0 else last_h if y > last_h else y


def draw(x, y): ps[ensure_x(x), ensure_y(y)] = 0, 0, 0 # color
def draw_negate(x, y):
x, y = ensure_x(x), ensure_y(y)
color = ps[x, y]
ps[x, y] = 255 - color[0], 255 - color[1], 255 - color[2]

pen = SimplePen(WEIGHT, draw)

for deg in range(0, ROTATE_DEGREE, 1 if ROTATE_DEGREE > 0 else -1):
x = centerX + int(r * cos(deg))
y = centerY + int(r * sin(deg))
pen.draw(x, y)
r += r_inc


img.show()