-
Notifications
You must be signed in to change notification settings - Fork 0
/
citra_rotasi.py
47 lines (35 loc) · 1.46 KB
/
citra_rotasi.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
35
36
37
38
39
40
41
42
43
44
45
46
47
from PIL import Image
from math import sin, cos
def rotasi(derajat):
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()
x_tengah = ukuran_horizontal // 2
y_tengah = ukuran_vertikal // 2
for x in range(ukuran_horizontal):
for y in range(ukuran_vertikal):
# theta == radian
theta = derajat * 22/7 / 180
# rumus rotasi dengan pusat rotasi bebas (disini tengah)
# sumber: https://homepages.inf.ed.ac.uk/rbf/HIPR2/rotate.htm
x_baru = (cos(theta) * (x - x_tengah) - sin(theta)
* (y - y_tengah) + x_tengah)
y_baru = (sin(theta) * (x - x_tengah) + cos(theta)
* (y - y_tengah) + y_tengah)
# rumus rotasi dengan pusat rotasi (0, 0)
# dari buku rinaldi munir
# x_baru = x*cos(theta) - y*sin(theta)
# y_baru = x*cos(theta) + y*cos(theta)
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_rotasi_' + str(derajat) + '.jpg'
CITRA_BARU.save(nama_setelah_disave)
rotasi(45)
rotasi(90)
rotasi(180)