Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8687685
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T23:10:45+00:00 2026-06-12T23:10:45+00:00

I have a 2D numpy array containing the individual data from each pixel of

  • 0

I have a 2D numpy array containing the individual data from each pixel of a sensor. The image is displayed in a GUI with a live feed from the camera. I want to be able to draw a rectangle over the image in order to distinguish an area of the screen. It seems pretty simple to draw a rectangle which is parallel to the side of the image but I eventually want to be able to rotate the rectangle. How will I know which pixels the rectangle covers when it is rotated?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T23:10:46+00:00Added an answer on June 12, 2026 at 11:10 pm

    You can use the Python Imaging Library, if you don’t mind the dependency. Given a 2D numpy array data, and an array poly of polygon coordinates (with shape (n, 2)), this will draw a polygon filled with the value 0 in the array:

    img = Image.fromarray(data)
    draw = ImageDraw.Draw(img)
    draw.polygon([tuple(p) for p in poly], fill=0)
    new_data = np.asarray(img)
    

    Here’s a self-contained demo:

    import numpy as np
    import matplotlib.pyplot as plt
    
    # Python Imaging Library imports
    import Image
    import ImageDraw
    
    
    def get_rect(x, y, width, height, angle):
        rect = np.array([(0, 0), (width, 0), (width, height), (0, height), (0, 0)])
        theta = (np.pi / 180.0) * angle
        R = np.array([[np.cos(theta), -np.sin(theta)],
                      [np.sin(theta), np.cos(theta)]])
        offset = np.array([x, y])
        transformed_rect = np.dot(rect, R) + offset
        return transformed_rect
    
    
    def get_data():
        """Make an array for the demonstration."""
        X, Y = np.meshgrid(np.linspace(0, np.pi, 512), np.linspace(0, 2, 512))
        z = (np.sin(X) + np.cos(Y)) ** 2 + 0.25
        data = (255 * (z / z.max())).astype(int)
        return data
    
    
    if __name__ == "__main__":
        data = get_data()
    
        # Convert the numpy array to an Image object.
        img = Image.fromarray(data)
    
        # Draw a rotated rectangle on the image.
        draw = ImageDraw.Draw(img)
        rect = get_rect(x=120, y=80, width=100, height=40, angle=30.0)
        draw.polygon([tuple(p) for p in rect], fill=0)
        # Convert the Image data to a numpy array.
        new_data = np.asarray(img)
    
        # Display the result using matplotlib.  (`img.show()` could also be used.)
        plt.imshow(new_data, cmap=plt.cm.gray)
        plt.show()
    

    This script generates this plot:

    enter image description here

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a 2D array of Numpy data read from a .csv file. Each
I have a 48x365 element numpy array where each element is a list containing
I have a numpy array of pixel data that I want to draw at
I have a numpy array of shape (height, width, 3) loaded from an image.
I have an numpy array which I save to a image using savefig(). Then
I am creating an object in python. I have a numpy array from an
I have a very large numpy array (containing up to a million elements) like
I have a 1D numpy array containing complex numbers (eg. numpy.complex64). I need to
i have an image (saved as numpy-array) and i want to transform it with
I have a numpy array which came from a cv2.imread and so has dtype

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.