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

  • SEARCH
  • Home
  • 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 6974391
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T17:13:53+00:00 2026-05-27T17:13:53+00:00

I’m trying to compare a generated image in Python with an image/photo in a

  • 0

I’m trying to compare a generated image in Python with an image/photo in a file.

The best way I got to make this so far is by generating a figure in Matplotlib and then convert it to a numpy array and compare the values with the values I get from my image.

I got the following code to convert a Matplotlib figure to a 3D numpy array with RGB channels:

def fig2data ( fig ):
    """
    @brief Convert a Matplotlib figure to a 3D numpy array with RGB channels and return it
    @param fig a matplotlib figure
    @return a numpy 3D array of RGB values
    """
    # draw the renderer
    fig.canvas.draw ( )

    # Get the RGBA buffer from the figure
    w,h = fig.canvas.get_width_height()
    buf = numpy.fromstring ( fig.canvas.tostring_rgb(), dtype=numpy.uint8 )
    buf.shape = ( w, h, 3 )

    return buf

One of the problems – the one I’m trying to figure out so far – is that this converted image doesn’t come croped. For instance, if I draw a square ocupying the all canvas, Matplotlib put’s this anoying frame arround and that is converted and mixes all my results.

How do I get only the numeric values – without any frame or axis – of the figure I made?

Or even better, if there is a much easier way to compare the figure and the image in NumPy/Matplotlib that I don’t know about, please do let me know.

  • 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-05-27T17:13:54+00:00Added an answer on May 27, 2026 at 5:13 pm

    Well, it’s not really an answer to the problem at hand using Matplotlib, but I gave up on this lib for this job and just used PIL.

    It’s quite easy, altough it’s also quite slow (but I don’t know if it’s slower than Matplotlib).

    The code is the following:

    def makeImage (triangle, largura, altura):
        """
        triangle: receives a tuple in the form: x1, y1, x2, y2, x3, y3, R, G, B, A
        largura: image weight
        altura: image height
    
        returns: numPy array of the triangle composed final image
        """
        back = Image.new('RGBA', (largura,altura), (0,0,0,0))
        poly = Image.new('RGBA', (largura,altura))
        pdraw = ImageDraw.Draw(poly)
    
        pdraw.polygon([1,2,3,4,5,6], fill=(255,0,0,127))
        back.paste(poly,mask=poly)
    
        back = back.convert('RGB')
        backArr = asarray(back)
        #back.show()
    
        return backArr
    

    If you know of a way to speed up this process, please do let me know.

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

Sidebar

Related Questions

I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,
I've got a string that has curly quotes in it. I'd like to replace
I want use html5's new tag to play a wav file (currently only supported

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.