Hướng dẫn rotate image python - xoay hình ảnh python

Xem thảo luận

Nội dung chính

  • Giới thiệu
  • Phương pháp 1: Sử dụng Thư viện hình ảnh Python (PIL)
  • Thông số
  • Ví dụ để xoay một hình ảnh trong Python với PIL
  • Phương pháp 2: Sử dụng CV mở để xoay hình ảnh trong Python
  • Ví dụ để xoay một hình ảnh với Open-CV
  • Phương pháp 3: Sử dụng Numpy xoay hình ảnh
  • Phương pháp 4: Xoay hình ảnh matplotlib sử dụng scipy trong Python
  • Sự kết luận
  • Câu hỏi thường gặp
  • 1. Làm thế nào để bạn xoay một ma trận trong Python?
  • 2. Làm thế nào để bạn kiểm tra hướng của một hình ảnh trong Python?
  • Làm thế nào để bạn xoay một hình ảnh với một góc trong Python?
  • Làm cách nào để xoay hình ảnh 90 độ trong opencv?
  • Làm thế nào để bạn xoay một con số trong Python?

Cải thiện bài viết

Lưu bài viết

  • Đọc
  • Bàn luận
  • Xem thảo luận

    Cải thiện bài viết

    Lưu bài viết

    Đọc

    Bàn luận

    Trong bài viết này, hãy để Lừa xem cách xoay hình ảnh bằng Python. Bằng cách xoay hình ảnh, hình ảnh được xoay về trung tâm của nó bằng một số độ được chỉ định. Xoay của một hình ảnh là một phép biến đổi hình học. Nó có thể được thực hiện bằng cách chuyển đổi chuyển đổi (hoặc) chuyển đổi nghịch đảo. Using Image Processing Library Pillow Using Image Processing Library Pillow

    Python3

    Ở đây thư viện xử lý hình ảnh với gối sử dụng chuyển đổi nghịch đảo. Nếu số độ được chỉ định cho xoay hình ảnh không phải là bội số của 90 độ, thì một số giá trị pixel vượt ra ngoài ranh giới hình ảnh, tức là các giá trị pixel nằm ngoài kích thước của hình ảnh. Các giá trị như vậy sẽ không được hiển thị trong hình ảnh đầu ra. & NBSP;

    Phương pháp: 1 Sử dụng gối thư viện xử lý hình ảnh

    #import Image from PIL
    from PIL import Image
    
    #read the image
    input_image = Image.open("E:\Python-Pool-Home.jpg")
    #show image 
    input_image.show()
    
    #rotate image
    angle = 180
    output = input_image.rotate(angle)
    output.show()
    
    angle1 = 90
    output1 = input_image.rotate(angle1)
    output1.show()
    
    9
    #import Image from PIL
    from PIL import Image
    
    #read the image
    input_image = Image.open("E:\Python-Pool-Home.jpg")
    #show image 
    input_image.show()
    
    #rotate image
    angle = 180
    output = input_image.rotate(angle)
    output.show()
    
    angle1 = 90
    output1 = input_image.rotate(angle1)
    output1.show()
    
    0
    #import Image from PIL
    from PIL import Image
    
    #read the image
    input_image = Image.open("E:\Python-Pool-Home.jpg")
    #show image 
    input_image.show()
    
    #rotate image
    angle = 180
    output = input_image.rotate(angle)
    output.show()
    
    angle1 = 90
    output1 = input_image.rotate(angle1)
    output1.show()
    
    1

    #import Image from PIL
    from PIL import Image
    
    #read the image
    input_image = Image.open("E:\Python-Pool-Home.jpg")
    #show image 
    input_image.show()
    
    #rotate image
    angle = 180
    output = input_image.rotate(angle)
    output.show()
    
    angle1 = 90
    output1 = input_image.rotate(angle1)
    output1.show()
    
    0
    #import Image from PIL
    from PIL import Image
    
    #read the image
    input_image = Image.open("E:\Python-Pool-Home.jpg")
    #show image 
    input_image.show()
    
    #rotate image
    angle = 180
    output = input_image.rotate(angle)
    output.show()
    
    angle1 = 90
    output1 = input_image.rotate(angle1)
    output1.show()
    
    1
    #import Image from PIL
    from PIL import Image
    
    #read the image
    input_image = Image.open("E:\Python-Pool-Home.jpg")
    #show image 
    input_image.show()
    
    #rotate image
    angle = 180
    output = input_image.rotate(angle)
    output.show()
    
    angle1 = 90
    output1 = input_image.rotate(angle1)
    output1.show()
    
    2
    #import Image from PIL
    from PIL import Image
    
    #read the image
    input_image = Image.open("E:\Python-Pool-Home.jpg")
    #show image 
    input_image.show()
    
    #rotate image
    angle = 180
    output = input_image.rotate(angle)
    output.show()
    
    angle1 = 90
    output1 = input_image.rotate(angle1)
    output1.show()
    
    3
    #import Image from PIL
    from PIL import Image
    
    #read the image
    input_image = Image.open("E:\Python-Pool-Home.jpg")
    #show image 
    input_image.show()
    
    #rotate image
    angle = 180
    output = input_image.rotate(angle)
    output.show()
    
    angle1 = 90
    output1 = input_image.rotate(angle1)
    output1.show()
    
    4
    #import Image from PIL
    from PIL import Image
    
    #read the image
    input_image = Image.open("E:\Python-Pool-Home.jpg")
    #show image 
    input_image.show()
    
    #rotate image
    angle = 180
    output = input_image.rotate(angle)
    output.show()
    
    angle1 = 90
    output1 = input_image.rotate(angle1)
    output1.show()
    
    5
    #import Image from PIL
    from PIL import Image
    
    #read the image
    input_image = Image.open("E:\Python-Pool-Home.jpg")
    #show image 
    input_image.show()
    
    #rotate image
    angle = 180
    output = input_image.rotate(angle)
    output.show()
    
    angle1 = 90
    output1 = input_image.rotate(angle1)
    output1.show()
    
    6
    #import Image from PIL
    from PIL import Image
    
    #read the image
    input_image = Image.open("E:\Python-Pool-Home.jpg")
    #show image 
    input_image.show()
    
    #rotate image
    angle = 180
    output = input_image.rotate(angle)
    output.show()
    
    angle1 = 90
    output1 = input_image.rotate(angle1)
    output1.show()
    
    7
    #import Image from PIL
    from PIL import Image
    
    #read the image
    input_image = Image.open("E:\Python-Pool-Home.jpg")
    #show image 
    input_image.show()
    
    #rotate image
    angle = 180
    output = input_image.rotate(angle)
    output.show()
    
    angle1 = 90
    output1 = input_image.rotate(angle1)
    output1.show()
    
    8
    #import Image from PIL
    from PIL import Image
    
    #read the image
    input_image = Image.open("E:\Python-Pool-Home.jpg")
    #show image 
    input_image.show()
    
    #rotate image
    angle = 180
    output = input_image.rotate(angle)
    output.show()
    
    angle1 = 90
    output1 = input_image.rotate(angle1)
    output1.show()
    
    2
    #import Image from PIL
    from PIL import Image
    
    #read the image
    input_image = Image.open("E:\Python-Pool-Home.jpg")
    #show image 
    input_image.show()
    
    #rotate image
    angle = 180
    output = input_image.rotate(angle)
    output.show()
    
    angle1 = 90
    output1 = input_image.rotate(angle1)
    output1.show()
    
    00
    #import Image from PIL
    from PIL import Image
    
    #read the image
    input_image = Image.open("E:\Python-Pool-Home.jpg")
    #show image 
    input_image.show()
    
    #rotate image
    angle = 180
    output = input_image.rotate(angle)
    output.show()
    
    angle1 = 90
    output1 = input_image.rotate(angle1)
    output1.show()
    
    01
    #import Image from PIL
    from PIL import Image
    
    #read the image
    input_image = Image.open("E:\Python-Pool-Home.jpg")
    #show image 
    input_image.show()
    
    #rotate image
    angle = 180
    output = input_image.rotate(angle)
    output.show()
    
    angle1 = 90
    output1 = input_image.rotate(angle1)
    output1.show()
    
    7
    #import Image from PIL
    from PIL import Image
    
    #read the image
    input_image = Image.open("E:\Python-Pool-Home.jpg")
    #show image 
    input_image.show()
    
    #rotate image
    angle = 180
    output = input_image.rotate(angle)
    output.show()
    
    angle1 = 90
    output1 = input_image.rotate(angle1)
    output1.show()
    
    31
    #import Image from PIL
    from PIL import Image
    
    #read the image
    input_image = Image.open("E:\Python-Pool-Home.jpg")
    #show image 
    input_image.show()
    
    #rotate image
    angle = 180
    output = input_image.rotate(angle)
    output.show()
    
    angle1 = 90
    output1 = input_image.rotate(angle1)
    output1.show()
    
    32
    #import Image from PIL
    from PIL import Image
    
    #read the image
    input_image = Image.open("E:\Python-Pool-Home.jpg")
    #show image 
    input_image.show()
    
    #rotate image
    angle = 180
    output = input_image.rotate(angle)
    output.show()
    
    angle1 = 90
    output1 = input_image.rotate(angle1)
    output1.show()
    
    33

    Output:

    Hướng dẫn rotate image python - xoay hình ảnh python

    #import Image from PIL
    from PIL import Image
    
    #read the image
    input_image = Image.open("E:\Python-Pool-Home.jpg")
    #show image 
    input_image.show()
    
    #rotate image
    angle = 180
    output = input_image.rotate(angle)
    output.show()
    
    angle1 = 90
    output1 = input_image.rotate(angle1)
    output1.show()
    
    03
    #import Image from PIL
    from PIL import Image
    
    #read the image
    input_image = Image.open("E:\Python-Pool-Home.jpg")
    #show image 
    input_image.show()
    
    #rotate image
    angle = 180
    output = input_image.rotate(angle)
    output.show()
    
    angle1 = 90
    output1 = input_image.rotate(angle1)
    output1.show()
    
    2
    #import Image from PIL
    from PIL import Image
    
    #read the image
    input_image = Image.open("E:\Python-Pool-Home.jpg")
    #show image 
    input_image.show()
    
    #rotate image
    angle = 180
    output = input_image.rotate(angle)
    output.show()
    
    angle1 = 90
    output1 = input_image.rotate(angle1)
    output1.show()
    
    05
    #import Image from PIL
    from PIL import Image
    
    #read the image
    input_image = Image.open("E:\Python-Pool-Home.jpg")
    #show image 
    input_image.show()
    
    #rotate image
    angle = 180
    output = input_image.rotate(angle)
    output.show()
    
    angle1 = 90
    output1 = input_image.rotate(angle1)
    output1.show()
    
    06
    #import Image from PIL
    from PIL import Image
    
    #read the image
    input_image = Image.open("E:\Python-Pool-Home.jpg")
    #show image 
    input_image.show()
    
    #rotate image
    angle = 180
    output = input_image.rotate(angle)
    output.show()
    
    angle1 = 90
    output1 = input_image.rotate(angle1)
    output1.show()
    
    2
    #import Image from PIL
    from PIL import Image
    
    #read the image
    input_image = Image.open("E:\Python-Pool-Home.jpg")
    #show image 
    input_image.show()
    
    #rotate image
    angle = 180
    output = input_image.rotate(angle)
    output.show()
    
    angle1 = 90
    output1 = input_image.rotate(angle1)
    output1.show()
    
    00
    #import Image from PIL
    from PIL import Image
    
    #read the image
    input_image = Image.open("E:\Python-Pool-Home.jpg")
    #show image 
    input_image.show()
    
    #rotate image
    angle = 180
    output = input_image.rotate(angle)
    output.show()
    
    angle1 = 90
    output1 = input_image.rotate(angle1)
    output1.show()
    
    09
    #import Image from PIL
    from PIL import Image
    
    #read the image
    input_image = Image.open("E:\Python-Pool-Home.jpg")
    #show image 
    input_image.show()
    
    #rotate image
    angle = 180
    output = input_image.rotate(angle)
    output.show()
    
    angle1 = 90
    output1 = input_image.rotate(angle1)
    output1.show()
    
    7

    Đây là hình ảnh được xoay 180 độ

    Hình ảnh này được quay bởi 60 độrotate() method of Python Image Processing Library Pillow Takes the number of degrees as a parameter and rotates the image in Counter Clockwise Direction to the number of degrees specified.rotate() method of Python Image Processing Library Pillow Takes the number of degrees as a parameter and rotates the image in Counter Clockwise Direction to the number of degrees specified.

    Hình ảnh này được xoay 90 độ Using Open-CV to rotate an image by an angle in Python Using Open-CV to rotate an image by an angle in Python

    Phương pháp xoay () của Gối thư viện xử lý hình ảnh Python lấy số độ làm tham số và xoay hình ảnh theo hướng ngược chiều kim đồng hồ theo số độ được chỉ định.imutilswhich deals with images. The imutils.rotate() function is used to rotate an image by an angle in Python.imutilswhich deals with images. The imutils.rotate() function is used to rotate an image by an angle in Python.

    Python3

    Phương pháp 2: Sử dụng CV mở để xoay hình ảnh bằng một góc trong Python

    Điều này là phổ biến mà mọi người đều biết rằng Python Open-CV là một mô-đun sẽ xử lý các ứng dụng thời gian thực liên quan đến tầm nhìn máy tính. Open-CV hoạt động với thư viện xử lý hình ảnh Imutilswhich xử lý hình ảnh. Hàm iMutils.Rotate () được sử dụng để xoay hình ảnh bằng một góc trong Python.

    #import Image from PIL
    from PIL import Image
    
    #read the image
    input_image = Image.open("E:\Python-Pool-Home.jpg")
    #show image 
    input_image.show()
    
    #rotate image
    angle = 180
    output = input_image.rotate(angle)
    output.show()
    
    angle1 = 90
    output1 = input_image.rotate(angle1)
    output1.show()
    
    1

    #import Image from PIL
    from PIL import Image
    
    #read the image
    input_image = Image.open("E:\Python-Pool-Home.jpg")
    #show image 
    input_image.show()
    
    #rotate image
    angle = 180
    output = input_image.rotate(angle)
    output.show()
    
    angle1 = 90
    output1 = input_image.rotate(angle1)
    output1.show()
    
    35

    #import Image from PIL
    from PIL import Image
    
    #read the image
    input_image = Image.open("E:\Python-Pool-Home.jpg")
    #show image 
    input_image.show()
    
    #rotate image
    angle = 180
    output = input_image.rotate(angle)
    output.show()
    
    angle1 = 90
    output1 = input_image.rotate(angle1)
    output1.show()
    
    1

    #import Image from PIL
    from PIL import Image
    
    #read the image
    input_image = Image.open("E:\Python-Pool-Home.jpg")
    #show image 
    input_image.show()
    
    #rotate image
    angle = 180
    output = input_image.rotate(angle)
    output.show()
    
    angle1 = 90
    output1 = input_image.rotate(angle1)
    output1.show()
    
    37
    #import Image from PIL
    from PIL import Image
    
    #read the image
    input_image = Image.open("E:\Python-Pool-Home.jpg")
    #show image 
    input_image.show()
    
    #rotate image
    angle = 180
    output = input_image.rotate(angle)
    output.show()
    
    angle1 = 90
    output1 = input_image.rotate(angle1)
    output1.show()
    
    38
    #import Image from PIL
    from PIL import Image
    
    #read the image
    input_image = Image.open("E:\Python-Pool-Home.jpg")
    #show image 
    input_image.show()
    
    #rotate image
    angle = 180
    output = input_image.rotate(angle)
    output.show()
    
    angle1 = 90
    output1 = input_image.rotate(angle1)
    output1.show()
    
    2
    #import Image from PIL
    from PIL import Image
    
    #read the image
    input_image = Image.open("E:\Python-Pool-Home.jpg")
    #show image 
    input_image.show()
    
    #rotate image
    angle = 180
    output = input_image.rotate(angle)
    output.show()
    
    angle1 = 90
    output1 = input_image.rotate(angle1)
    output1.show()
    
    80
    #import Image from PIL
    from PIL import Image
    
    #read the image
    input_image = Image.open("E:\Python-Pool-Home.jpg")
    #show image 
    input_image.show()
    
    #rotate image
    angle = 180
    output = input_image.rotate(angle)
    output.show()
    
    angle1 = 90
    output1 = input_image.rotate(angle1)
    output1.show()
    
    81
    #import Image from PIL
    from PIL import Image
    
    #read the image
    input_image = Image.open("E:\Python-Pool-Home.jpg")
    #show image 
    input_image.show()
    
    #rotate image
    angle = 180
    output = input_image.rotate(angle)
    output.show()
    
    angle1 = 90
    output1 = input_image.rotate(angle1)
    output1.show()
    
    7
    #import Image from PIL
    from PIL import Image
    
    #read the image
    input_image = Image.open("E:\Python-Pool-Home.jpg")
    #show image 
    input_image.show()
    
    #rotate image
    angle = 180
    output = input_image.rotate(angle)
    output.show()
    
    angle1 = 90
    output1 = input_image.rotate(angle1)
    output1.show()
    
    15
    #import Image from PIL
    from PIL import Image
    
    #read the image
    input_image = Image.open("E:\Python-Pool-Home.jpg")
    #show image 
    input_image.show()
    
    #rotate image
    angle = 180
    output = input_image.rotate(angle)
    output.show()
    
    angle1 = 90
    output1 = input_image.rotate(angle1)
    output1.show()
    
    16
    #import Image from PIL
    from PIL import Image
    
    #read the image
    input_image = Image.open("E:\Python-Pool-Home.jpg")
    #show image 
    input_image.show()
    
    #rotate image
    angle = 180
    output = input_image.rotate(angle)
    output.show()
    
    angle1 = 90
    output1 = input_image.rotate(angle1)
    output1.show()
    
    17
    #import Image from PIL
    from PIL import Image
    
    #read the image
    input_image = Image.open("E:\Python-Pool-Home.jpg")
    #show image 
    input_image.show()
    
    #rotate image
    angle = 180
    output = input_image.rotate(angle)
    output.show()
    
    angle1 = 90
    output1 = input_image.rotate(angle1)
    output1.show()
    
    15
    #import Image from PIL
    from PIL import Image
    
    #read the image
    input_image = Image.open("E:\Python-Pool-Home.jpg")
    #show image 
    input_image.show()
    
    #rotate image
    angle = 180
    output = input_image.rotate(angle)
    output.show()
    
    angle1 = 90
    output1 = input_image.rotate(angle1)
    output1.show()
    
    16
    #import Image from PIL
    from PIL import Image
    
    #read the image
    input_image = Image.open("E:\Python-Pool-Home.jpg")
    #show image 
    input_image.show()
    
    #rotate image
    angle = 180
    output = input_image.rotate(angle)
    output.show()
    
    angle1 = 90
    output1 = input_image.rotate(angle1)
    output1.show()
    
    60
    #import Image from PIL
    from PIL import Image
    
    #read the image
    input_image = Image.open("E:\Python-Pool-Home.jpg")
    #show image 
    input_image.show()
    
    #rotate image
    angle = 180
    output = input_image.rotate(angle)
    output.show()
    
    angle1 = 90
    output1 = input_image.rotate(angle1)
    output1.show()
    
    61
    #import Image from PIL
    from PIL import Image
    
    #read the image
    input_image = Image.open("E:\Python-Pool-Home.jpg")
    #show image 
    input_image.show()
    
    #rotate image
    angle = 180
    output = input_image.rotate(angle)
    output.show()
    
    angle1 = 90
    output1 = input_image.rotate(angle1)
    output1.show()
    
    62
    #import Image from PIL
    from PIL import Image
    
    #read the image
    input_image = Image.open("E:\Python-Pool-Home.jpg")
    #show image 
    input_image.show()
    
    #rotate image
    angle = 180
    output = input_image.rotate(angle)
    output.show()
    
    angle1 = 90
    output1 = input_image.rotate(angle1)
    output1.show()
    
    7

    Output:

    #import Image from PIL
    from PIL import Image
    
    #read the image
    input_image = Image.open("E:\Python-Pool-Home.jpg")
    #show image 
    input_image.show()
    
    #rotate image
    angle = 180
    output = input_image.rotate(angle)
    output.show()
    
    angle1 = 90
    output1 = input_image.rotate(angle1)
    output1.show()
    
    83
    #import Image from PIL
    from PIL import Image
    
    #read the image
    input_image = Image.open("E:\Python-Pool-Home.jpg")
    #show image 
    input_image.show()
    
    #rotate image
    angle = 180
    output = input_image.rotate(angle)
    output.show()
    
    angle1 = 90
    output1 = input_image.rotate(angle1)
    output1.show()
    
    2
    #import Image from PIL
    from PIL import Image
    
    #read the image
    input_image = Image.open("E:\Python-Pool-Home.jpg")
    #show image 
    input_image.show()
    
    #rotate image
    angle = 180
    output = input_image.rotate(angle)
    output.show()
    
    angle1 = 90
    output1 = input_image.rotate(angle1)
    output1.show()
    
    85
    #import Image from PIL
    from PIL import Image
    
    #read the image
    input_image = Image.open("E:\Python-Pool-Home.jpg")
    #show image 
    input_image.show()
    
    #rotate image
    angle = 180
    output = input_image.rotate(angle)
    output.show()
    
    angle1 = 90
    output1 = input_image.rotate(angle1)
    output1.show()
    
    2
    #import Image from PIL
    from PIL import Image
    
    #read the image
    input_image = Image.open("E:\Python-Pool-Home.jpg")
    #show image 
    input_image.show()
    
    #rotate image
    angle = 180
    output = input_image.rotate(angle)
    output.show()
    
    angle1 = 90
    output1 = input_image.rotate(angle1)
    output1.show()
    
    87
    #import Image from PIL
    from PIL import Image
    
    #read the image
    input_image = Image.open("E:\Python-Pool-Home.jpg")
    #show image 
    input_image.show()
    
    #rotate image
    angle = 180
    output = input_image.rotate(angle)
    output.show()
    
    angle1 = 90
    output1 = input_image.rotate(angle1)
    output1.show()
    
    7
    #import Image from PIL
    from PIL import Image
    
    #read the image
    input_image = Image.open("E:\Python-Pool-Home.jpg")
    #show image 
    input_image.show()
    
    #rotate image
    angle = 180
    output = input_image.rotate(angle)
    output.show()
    
    angle1 = 90
    output1 = input_image.rotate(angle1)
    output1.show()
    
    89
    #import Image from PIL
    from PIL import Image
    
    #read the image
    input_image = Image.open("E:\Python-Pool-Home.jpg")
    #show image 
    input_image.show()
    
    #rotate image
    angle = 180
    output = input_image.rotate(angle)
    output.show()
    
    angle1 = 90
    output1 = input_image.rotate(angle1)
    output1.show()
    
    2
    #import Image from PIL
    from PIL import Image
    
    #read the image
    input_image = Image.open("E:\Python-Pool-Home.jpg")
    #show image 
    input_image.show()
    
    #rotate image
    angle = 180
    output = input_image.rotate(angle)
    output.show()
    
    angle1 = 90
    output1 = input_image.rotate(angle1)
    output1.show()
    
    85
    #import Image from PIL
    from PIL import Image
    
    #read the image
    input_image = Image.open("E:\Python-Pool-Home.jpg")
    #show image 
    input_image.show()
    
    #rotate image
    angle = 180
    output = input_image.rotate(angle)
    output.show()
    
    angle1 = 90
    output1 = input_image.rotate(angle1)
    output1.show()
    
    2
    #import Image from PIL
    from PIL import Image
    
    #read the image
    input_image = Image.open("E:\Python-Pool-Home.jpg")
    #show image 
    input_image.show()
    
    #rotate image
    angle = 180
    output = input_image.rotate(angle)
    output.show()
    
    angle1 = 90
    output1 = input_image.rotate(angle1)
    output1.show()
    
    13
    #import Image from PIL
    from PIL import Image
    
    #read the image
    input_image = Image.open("E:\Python-Pool-Home.jpg")
    #show image 
    input_image.show()
    
    #rotate image
    angle = 180
    output = input_image.rotate(angle)
    output.show()
    
    angle1 = 90
    output1 = input_image.rotate(angle1)
    output1.show()
    
    7

    Hình ảnh được xoay bằng cách sử dụng CV mở ở 45 độCounter Clockwise direction to the number of degrees specifiedCounter Clockwise direction to the number of degrees specified


    • Giới thiệu
    • Phương pháp 1: Sử dụng Thư viện hình ảnh Python (PIL)
      • Cú pháp
      • Thông số
      • Ví dụ để xoay một hình ảnh trong Python với PIL
    • Phương pháp 2: Sử dụng CV mở để xoay hình ảnh trong Python
      • Cú pháp
      • Thông số
    • Ví dụ để xoay một hình ảnh trong Python với PIL
    • Phương pháp 2: Sử dụng CV mở để xoay hình ảnh trong Python
    • Ví dụ để xoay một hình ảnh với Open-CV
    • Phương pháp 3: Sử dụng Numpy xoay hình ảnh
      • Phương pháp 4: Xoay hình ảnh matplotlib sử dụng scipy trong Python
      • Sự kết luận

    Giới thiệu

    Phương pháp 1: Sử dụng Thư viện hình ảnh Python (PIL)

    Phương pháp 1: Sử dụng Thư viện hình ảnh Python (PIL)

    Câu hỏi thường gặp The function uses an Inverse transformation. If the Number Of Degrees that we have Specified for Image Rotation is not an Integer Multiple of 90 Degrees, then some Pixel Values Beyond Image Boundaries, i.e., Pixel values lying outside the Dimension of the image. Such Values are not displayed in the output image. 

    Thông số

    Cú pháp

    #import Image from PIL
    from PIL import Image
    
    #read the image
    input_image = Image.open("E:\Python-Pool-Home.jpg")
    #show image 
    input_image.show()
    
    #rotate image
    angle = 180
    output = input_image.rotate(angle)
    output.show()
    
    angle1 = 90
    output1 = input_image.rotate(angle1)
    output1.show()
    
    2

    Thông số

    • Cú pháp The function uses an Inverse transformation. If the Number Of Degrees that we have Specified for Image Rotation is not an Integer Multiple of 90 Degrees, then some Pixel Values Beyond Image Boundaries, i.e., Pixel values lying outside the Dimension of the image. Such Values are not displayed in the output image.  It is the angle in degrees counterclockwise.
    • Ví dụ để xoay một hình ảnh trong Python với PIL It is the angle in degrees counterclockwise. It is an optional input. It can be one of PIL.Image.NEAREST (use the nearest neighbor), PIL.Image.BILINEAR (linear interpolation in a 2×2 environment), or PIL.Image.BICUBIC (cubic spline interpolation in a 4×4 environment). If omitted, or if the image has mode “1” or “P,” it is set PIL.Image.NEAREST. See Filters.
    • Ví dụ để xoay một hình ảnh với Open-CV It is an optional input. If set to True, the images expand and become large enough to hold the entire rotated image. If set to False, the output image is of the same size as of input image. It is an optional input. If set to True, the images expand and become large enough to hold the entire rotated image. If set to False, the output image is of the same size as of input image.
    • Phương pháp 3: Sử dụng Numpy xoay hình ảnh It is an optional input. It set the center of image rotation. By default, it is the center of the image. It is an optional input. It set the center of image rotation. By default, it is the center of the image.
    • Phương pháp 4: Xoay hình ảnh matplotlib sử dụng scipy trong Python It is an optional input. It is a post-rotate translation. It is an optional input. It is a post-rotate translation.
    • Sự kết luận It is an optional input. It is a color for an area outside the rotated image. It is an optional input. It is a color for an area outside the rotated image.

    Ví dụ để xoay một hình ảnh trong Python với PIL

    Phương pháp 2: Sử dụng CV mở để xoay hình ảnh trong Python

    #import Image from PIL
    from PIL import Image
    
    #read the image
    input_image = Image.open("E:\Python-Pool-Home.jpg")
    #show image 
    input_image.show()
    
    #rotate image
    angle = 180
    output = input_image.rotate(angle)
    output.show()
    
    angle1 = 90
    output1 = input_image.rotate(angle1)
    output1.show()
    

    Output:

    Ví dụ để xoay một hình ảnh với Open-CV

    Rotated 180 degree image

    Rotated 90 degree image

    Explanation:

    • Phương pháp 3: Sử dụng Numpy xoay hình ảnh
    • Phương pháp 4: Xoay hình ảnh matplotlib sử dụng scipy trong Python
    • Sự kết luận
    • Câu hỏi thường gặp
    • 1. Làm thế nào để bạn xoay một ma trận trong Python?angle of rotation of the image.angle of rotation of the image.
    • 2. Làm thế nào để bạn kiểm tra hướng của một hình ảnh trong Python?
    • Trong Python, chúng tôi đã thảo luận về nhiều khái niệm và chuyển đổi. Nhưng đôi khi, chúng ta đi đến một tình huống mà chúng ta cần xoay một hình ảnh với một góc cụ thể. Trong hướng dẫn này, chúng tôi sẽ thảo luận về 4 cách khác nhau để xoay hình ảnh với một góc cụ thể, tức là, sử dụng thư viện hình ảnh Python và sử dụng CV & NBSP mở; để xoay hình ảnh theo một góc trong Python. Xoay của một hình ảnh là một phép biến đổi hình học. Nó có thể được thực hiện bằng cách chuyển đổi chuyển đổi (hoặc) chuyển đổi nghịch đảo.
    • Thư viện hình ảnh Python (PIL) là một mô-đun chứa các chức năng được xây dựng trong Python để thao tác và làm việc với hình ảnh như một đầu vào cho các chức năng. Hàm sử dụng một phép biến đổi nghịch đảo. Nếu số độ mà chúng tôi đã chỉ định cho xoay hình ảnh không phải là bội số của 90 độ, thì một số giá trị pixel vượt ra ngoài ranh giới hình ảnh, tức là, các giá trị pixel nằm ngoài kích thước của hình ảnh. Các giá trị như vậy không được hiển thị trong hình ảnh đầu ra. & NBSP;

    Phương pháp 2: Sử dụng CV mở để xoay hình ảnh trong Python

    Ví dụ để xoay một hình ảnh với Open-CVimutils.rotate() function, we can rotate an image by an angle in Python. Even Open-CV Rotates the image in Counter Clockwise direction to the number of degrees specified.

    Rotated 180 degree image

    #import Image from PIL
    from PIL import Image
    
    #read the image
    input_image = Image.open("E:\Python-Pool-Home.jpg")
    #show image 
    input_image.show()
    
    #rotate image
    angle = 180
    output = input_image.rotate(angle)
    output.show()
    
    angle1 = 90
    output1 = input_image.rotate(angle1)
    output1.show()
    
    0

    Ví dụ để xoay một hình ảnh với Open-CV

    Rotated 180 degree imagecv2.imshow(). Then, we will apply the imutils.rotate() function to rotate an image for a particular angle. Then, we will print the images after rotation. Let us look at the example for understanding the concept in detail.

    #import Image from PIL
    from PIL import Image
    
    #read the image
    input_image = Image.open("E:\Python-Pool-Home.jpg")
    #show image 
    input_image.show()
    
    #rotate image
    angle = 180
    output = input_image.rotate(angle)
    output.show()
    
    angle1 = 90
    output1 = input_image.rotate(angle1)
    output1.show()
    
    3

    Output:

    Explanation:

    • Rotated 90 degree image
    • Phương pháp 3: Sử dụng Numpy xoay hình ảnh
    • Phương pháp 4: Xoay hình ảnh matplotlib sử dụng scipy trong Python
    • Sự kết luận
    • Câu hỏi thường gặp
    • 1. Làm thế nào để bạn xoay một ma trận trong Python?angle of rotation of the image.

    Phương pháp 3: Sử dụng Numpy xoay hình ảnh

    Phương pháp 4: Xoay hình ảnh matplotlib sử dụng scipy trong Python

    #import Image from PIL
    from PIL import Image
    
    #read the image
    input_image = Image.open("E:\Python-Pool-Home.jpg")
    #show image 
    input_image.show()
    
    #rotate image
    angle = 180
    output = input_image.rotate(angle)
    output.show()
    
    angle1 = 90
    output1 = input_image.rotate(angle1)
    output1.show()
    
    8

    Output:

    Explanation:

    • Sự kết luận
    • Câu hỏi thường gặp
    • 1. Làm thế nào để bạn xoay một ma trận trong Python?angle of rotation of the image.
    • 2. Làm thế nào để bạn kiểm tra hướng của một hình ảnh trong Python?
    • Trong Python, chúng tôi đã thảo luận về nhiều khái niệm và chuyển đổi. Nhưng đôi khi, chúng ta đi đến một tình huống mà chúng ta cần xoay một hình ảnh với một góc cụ thể. Trong hướng dẫn này, chúng tôi sẽ thảo luận về 4 cách khác nhau để xoay hình ảnh với một góc cụ thể, tức là, sử dụng thư viện hình ảnh Python và sử dụng CV & NBSP mở; để xoay hình ảnh theo một góc trong Python. Xoay của một hình ảnh là một phép biến đổi hình học. Nó có thể được thực hiện bằng cách chuyển đổi chuyển đổi (hoặc) chuyển đổi nghịch đảo.
    • Do đó, bạn có thể thấy hình ảnh xoay ở vị trí đã cho.

    Phương pháp 4: Xoay hình ảnh matplotlib sử dụng scipy trong Python

    Trong ví dụ này, chúng tôi sẽ sử dụng thư viện matplotlib và scipy để xoay hình ảnh. Sau đó, chúng tôi sẽ áp dụng hàm Misc.Face () từ Thư viện SCIPY. Sau đó, chúng tôi sẽ xoay một hình ảnh và xem đầu ra. Chúng ta hãy nhìn vào ví dụ để hiểu chi tiết khái niệm.

    #import Image from PIL
    from PIL import Image
    
    #read the image
    input_image = Image.open("E:\Python-Pool-Home.jpg")
    #show image 
    input_image.show()
    
    #rotate image
    angle = 180
    output = input_image.rotate(angle)
    output.show()
    
    angle1 = 90
    output1 = input_image.rotate(angle1)
    output1.show()
    
    1

    Output:

    Explanation:

    • Đầu tiên, chúng tôi sẽ nhập NDIMAGE và MISC từ Thư viện SCIPY.
    • Sau đó, chúng tôi sẽ nhập thư viện Pyplot từ mô -đun Matplotlib.
    • Sau đó, chúng tôi sẽ áp dụng hàm misc.face ().
    • Sau đó, chúng tôi sẽ áp dụng hàm ndimage.rotate () để xoay hình ảnh.
    • Cuối cùng, chúng tôi sẽ áp dụng imshow () và show () để hiển thị hình ảnh.

    Sự kết luận

    Trong hướng dẫn này, chúng tôi đã tìm hiểu về cách xoay hình ảnh trong Python với sự trợ giúp của hai phương pháp, tức là, sử dụng CV mở và sử dụng Thư viện được cài đặt Python (PIL). Cả hai phương pháp đều được giải thích chi tiết với cú pháp của chúng và mã mẫu làm ví dụ. Tất cả các ví dụ sẽ giúp bạn hiểu phương pháp sâu sắc hơn.

    Tuy nhiên, nếu bạn có bất kỳ nghi ngờ hoặc câu hỏi nào, hãy cho tôi biết trong phần bình luận bên dưới. Tôi sẽ cố gắng giúp bạn càng sớm càng tốt.

    Câu hỏi thường gặp

    1. Làm thế nào để bạn xoay một ma trận trong Python?

    Chúng ta có thể xoay một ma trận trong Python với sự trợ giúp của mã sau:

    #import Image from PIL
    from PIL import Image
    
    #read the image
    input_image = Image.open("E:\Python-Pool-Home.jpg")
    #show image 
    input_image.show()
    
    #rotate image
    angle = 180
    output = input_image.rotate(angle)
    output.show()
    
    angle1 = 90
    output1 = input_image.rotate(angle1)
    output1.show()
    
    6

    Output:

    2. Làm thế nào để bạn kiểm tra hướng của một hình ảnh trong Python?

    Khi không có hiệu chỉnh được áp dụng, & nbsp; định hướng & nbsp; của một bức ảnh được xác định bởi & nbsp; xoay & nbsp; của máy ảnh tại thời điểm & nbsp; hình ảnh & nbsp; đã được chụp. Mặc dù có nhiều góc có thể, xoay theo bội số 90 ° là phổ biến nhất. Chúng cũng đơn giản để sửa chữa sau khi được phát hiện. Chúng ta có thể kiểm tra định hướng của một hình ảnh trong Python bằng các tính năng dựa trên LBP và hồi quy logistic.

    Làm thế nào để bạn xoay một hình ảnh với một góc trong Python?

    2 cách để xoay một hình ảnh bằng một góc trong Python...

    Image.show () ....

    QUẢNG CÁO.....

    Hình ảnh đầu vào.....

    125 độ - xoay.....

    imutils.rotate (hình ảnh, góc = góc) ....

    CV2.IMREAD (R "Đường dẫn hình ảnh/URL") ....

    cv2.imshow("output--msg",image).

    Làm cách nào để xoay hình ảnh 90 độ trong opencv?

    Bước 2: Xoay hình ảnh...

    CV2.ROTATE_90_CLOCKWITY: Xoay hình ảnh theo chiều kim đồng hồ 90 độ ..

    CV2.ROTATE_90_COUNECLOCKWITY: Xoay hình ảnh theo hướng ngược chiều kim đồng hồ 90 độ ..

    CV2.ROTATE_180: Xoay hình ảnh theo chiều kim đồng hồ bằng 180 độ ..

    Làm thế nào để bạn xoay một con số trong Python?

    Chức năng xoay () được sử dụng để xoay hình ảnh bằng một góc trong Python. is used to rotate an image by an angle in Python. is used to rotate an image by an angle in Python.