-
Notifications
You must be signed in to change notification settings - Fork 0
/
citra_translasi.py
34 lines (26 loc) · 935 Bytes
/
citra_translasi.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
from PIL import Image
def translasi(m, n):
CITRA = Image.open('gambar.jpg')
PIXEL = CITRA.load()
ukuran_horizontal = CITRA.size[0]
ukuran_vertikal = CITRA.size[1]
CITRA_BARU = Image.new("RGB", (ukuran_horizontal, ukuran_vertikal))
PIXEL_BARU = CITRA_BARU.load()
start_m = m
start_n = n
if m < 0:
start_m = 0
if n < 0:
start_n = 0
for x in range(start_m, ukuran_horizontal):
for y in range(start_n, ukuran_vertikal):
x_baru = x - m
y_baru = y - n
if (x_baru >= ukuran_horizontal or y_baru >= ukuran_vertikal or x_baru < 0 or y_baru < 0):
PIXEL_BARU[x, y] = (0, 0, 0)
else:
PIXEL_BARU[x, y]= PIXEL[x_baru, y_baru]
nama_setelah_disave = 'gambar_translasi_' + str(m) + '_' + str(n) +'.jpg'
CITRA_BARU.save(nama_setelah_disave)
translasi(200, 500)
translasi(-200, -500)