Xoay hình ảnh 90 độ Python

Trước tiên chúng ta hãy nhập các thư viện cần thiết và đọc hình ảnh. Hãy để chúng tôi cũng lưu trữ kích thước của hình ảnh bằng cách sử dụng. hàm hình dạng []. Hình ảnh mà chúng tôi đang sử dụng ở đây là hình ảnh được hiển thị bên dưới

Lưu ý– Lưu ý rằng kích thước của hình ảnh được xoay giống với hình ảnh gốc và cách nó cắt các góc của hình ảnh

Bạn có thể loại bỏ phần cắt xén này bằng cách đặt đối số

Input: matrix = [[1,2],[3,4]]
Output: [[3,1],[4,2]]
1 thành
Input: matrix = [[1,2],[3,4]]
Output: [[3,1],[4,2]]
2 khi gọi phương thức
Input: matrix = [[1]]
Output: [[1]]
9. Điều này sẽ tăng kích thước của hình ảnh được xoay để chứa toàn bộ hình ảnh được xoay

Ví dụ

# Rotate the image 45 degrees counter-clockwise and store it in img_rot_expanded
img_rot_expanded = img.rotate[45, expand=True]

print[img.size]
# Outputs- [3264, 2448]
print[img_rot_expanded.size]
# Outputs- [4040, 4040]

img_rot.show[]

Đầu ra của điều này sẽ là-

Ví dụ

# Rotate the image 90 degrees clockwise and store it in img_rot_2
img_rot_2 = img.rotate[-90]

print[img.size]
# Outputs- [3264, 2448]
print[img_rot_2.size]
# Outputs- [3264, 2448]

img_rot_2.show[]

Đầu ra của điều này sẽ là-

Chuyển hình ảnh

Bạn có thể chuyển đổi hình ảnh bằng cách gọi phương thức

Input: matrix = [[1,2],[3,4]]
Output: [[3,1],[4,2]]
4 của Gối trên một đối tượng của lớp Hình ảnh.
Input: matrix = [[1,2],[3,4]]
Output: [[3,1],[4,2]]
4 chấp nhận một trong số các thuộc tính Hình ảnh làm đối số

Bảng bên dưới liệt kê một vài Thuộc tính của

# Rotate the image 45 degrees counter-clockwise and store it in img_rot_expanded
img_rot_expanded = img.rotate[45, expand=True]

print[img.size]
# Outputs- [3264, 2448]
print[img_rot_expanded.size]
# Outputs- [4040, 4040]

img_rot.show[]
1 có thể được chuyển cho
Input: matrix = [[1,2],[3,4]]
Output: [[3,1],[4,2]]
4 cùng với hiệu ứng mà nó tạo ra

Image AttributeEffect
# Rotate the image 45 degrees counter-clockwise and store it in img_rot_expanded
img_rot_expanded = img.rotate[45, expand=True]

print[img.size]
# Outputs- [3264, 2448]
print[img_rot_expanded.size]
# Outputs- [4040, 4040]

img_rot.show[]
3Lật ảnh quanh trục tung.
# Rotate the image 45 degrees counter-clockwise and store it in img_rot_expanded
img_rot_expanded = img.rotate[45, expand=True]

print[img.size]
# Outputs- [3264, 2448]
print[img_rot_expanded.size]
# Outputs- [4040, 4040]

img_rot.show[]
4Lật ảnh quanh trục ngang ______ 65 Xoay ảnh 90° theo hướng ngược chiều kim đồng hồ.
# Rotate the image 45 degrees counter-clockwise and store it in img_rot_expanded
img_rot_expanded = img.rotate[45, expand=True]

print[img.size]
# Outputs- [3264, 2448]
print[img_rot_expanded.size]
# Outputs- [4040, 4040]

img_rot.show[]
6Xoay hình ảnh 180° theo hướng ngược chiều kim đồng hồ.
# Rotate the image 45 degrees counter-clockwise and store it in img_rot_expanded
img_rot_expanded = img.rotate[45, expand=True]

print[img.size]
# Outputs- [3264, 2448]
print[img_rot_expanded.size]
# Outputs- [4040, 4040]

img_rot.show[]
7Xoay hình ảnh 270° theo hướng ngược chiều kim đồng hồ

Ví dụ

Trong ví dụ này, chúng tôi sẽ sử dụng phương pháp

Input: matrix = [[1,2],[3,4]]
Output: [[3,1],[4,2]]
4 để lật hình ảnh theo chiều ngang [về trục dọc]

Input: matrix = [[5,1,9,11],[2,4,8,10],[13,3,6,7],[15,14,12,16]]
Output: [[15,13,2,5],[14,3,4,1],[12,6,8,9],[16,7,10,11]]
4

Đầu ra của điều này sẽ là-

Hình dưới đây so sánh các hình ảnh khi các thuộc tính

# Rotate the image 45 degrees counter-clockwise and store it in img_rot_expanded
img_rot_expanded = img.rotate[45, expand=True]

print[img.size]
# Outputs- [3264, 2448]
print[img_rot_expanded.size]
# Outputs- [4040, 4040]

img_rot.show[]
1 khác nhau được chuyển đến
Input: matrix = [[1,2],[3,4]]
Output: [[3,1],[4,2]]
4

Lưu ý– Sử dụng

# Rotate the image 90 degrees clockwise and store it in img_rot_2
img_rot_2 = img.rotate[-90]

print[img.size]
# Outputs- [3264, 2448]
print[img_rot_2.size]
# Outputs- [3264, 2448]

img_rot_2.show[]
1 cũng giống như sử dụng
# Rotate the image 90 degrees clockwise and store it in img_rot_2
img_rot_2 = img.rotate[-90]

print[img.size]
# Outputs- [3264, 2448]
print[img_rot_2.size]
# Outputs- [3264, 2448]

img_rot_2.show[]
2. Tương tự,
# Rotate the image 90 degrees clockwise and store it in img_rot_2
img_rot_2 = img.rotate[-90]

print[img.size]
# Outputs- [3264, 2448]
print[img_rot_2.size]
# Outputs- [3264, 2448]

img_rot_2.show[]
3 và
# Rotate the image 90 degrees clockwise and store it in img_rot_2
img_rot_2 = img.rotate[-90]

print[img.size]
# Outputs- [3264, 2448]
print[img_rot_2.size]
# Outputs- [3264, 2448]

img_rot_2.show[]
4 tương ứng với
# Rotate the image 90 degrees clockwise and store it in img_rot_2
img_rot_2 = img.rotate[-90]

print[img.size]
# Outputs- [3264, 2448]
print[img_rot_2.size]
# Outputs- [3264, 2448]

img_rot_2.show[]
5 và
# Rotate the image 90 degrees clockwise and store it in img_rot_2
img_rot_2 = img.rotate[-90]

print[img.size]
# Outputs- [3264, 2448]
print[img_rot_2.size]
# Outputs- [3264, 2448]

img_rot_2.show[]
6

Chủ Đề