8000 [Bug]: savefig with bbox_inches does not save image in the figure · Issue #25116 · matplotlib/matplotlib · GitHub
[go: up one dir, main page]

Skip to content

[Bug]: savefig with bbox_inches does not save image in the figure #25116

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
chappet opened this issue Jan 31, 2023 · 7 comments
Closed

[Bug]: savefig with bbox_inches does not save image in the figure #25116

chappet opened this issue Jan 31, 2023 · 7 comments

Comments

@chappet
Copy link
chappet commented Jan 31, 2023

Bug summary

The image added in the text bbox is not saved with savefig if setting a bbox.
savefig without option do save the image

Code for reproduction

def GrabTextWithImage():
    
    fig = plt.figure()
    plt.subplot(111)

    #Display Text
    txt = plt.gcf().text(0.10, 0.5, "      ................\n      ................\n      ................"
                      ,bbox={'edgecolor':'black','alpha' : 0.2,'facecolor':'white', 'pad':10, 'linewidth' : 2},
                      verticalalignment='center', horizontalalignment='left', color='black', fontsize=12,zorder = 3)
    

    #Add an image
    an_array = numpy.random.random((20,20)) * 255    
    fig.figimage(an_array,70,240, zorder = 1)

    #Grab around text and return image
    ax = plt.gca()
    r = fig.canvas.get_renderer()
    bb = txt.get_window_extent(r)
    
    transf = ax.transData.inverted()    
    bb_datacoords = bb.transformed(transf)  
    bbox = bb_datacoords.transformed(ax.transData).transformed(fig.dpi_scale_trans.inverted())
    
    plt.axis('off')

    fp = tempfile.TemporaryFile()  
    plt.savefig(fp, bbox_inches = bbox) #BUG : image is not displayed
#     plt.savefig(fp) #Image is displayed
    
    im = Image.open(fp)
    return im

if __name__ == "__main__":
    import numpy
    print (mpl.__version__)

    im = GrabTextWithImage()
    plt.show()
    im.show()

Actual outcome

image

Expected outcome

image

Additional information

No response

Operating system

Win, Python 3.8, Matplotlib 3.6.2

Matplotlib Version

3.6.2

Matplotlib Backend

QtAgg

Python version

3.8.2

Jupyter version

No response

Installation

pip

@jklymak
Copy link
Member
jklymak commented Jan 31, 2023

I would expect this is much the same as #23399. So far as I can tell figimage doesn't handle dpi changes properly. I would personally recommend not using figimage, but we probably should fix.

@jklymak
Copy link
Member
jklymak commented Jan 31, 2023

@chappet if you want help doing what you want, suggest a discussion over at https://discourse.matplotlib.org. Your bug report has many other issues with it that make the results confounding.

@jalajk24
Copy link

Can I work on this issue? I am new to this but interested to work on it

@jklymak
Copy link
Member
jklymak commented Jan 31, 2023

Anyone can work on any issue, but I don't think we have isolated a problem yet.

@tacaswell
Copy link
Member

The bug is very likely a miss-matched assumption between

def make_image(self, renderer, magnification=1.0, unsampled=False):
# docstring inherited
fac = renderer.dpi/self.figure.dpi
# fac here is to account for pdf, eps, svg backends where
# figure.dpi is set to 72. This means we need to scale the
# image (using magnification) and offset it appropriately.
bbox = Bbox([[self.ox/fac, self.oy/fac],
[(self.ox/fac + self._A.shape[1]),
(self.oy/fac + self._A.shape[0])]])
width, height = self.figure.get_size_inches()
width *= renderer.dpi
height *= renderer.dpi
clip = Bbox([[0, 0], [width, height]])
return self._make_image(
self._A, bbox, bbox, clip, magnification=magnification / fac,
unsampled=unsampled, round_to_pixel_border=False)
and what the bbox_inches code is doing that is causing the image to be drawn "off screen".

Given the example images, BBoxImage (https://matplotlib.org/stable/gallery/images_contours_and_fields/demo_bboximage.html#bboximage-demo) may be a better option for the @chappet anyway....

@chappet
Copy link
Author
chappet commented Feb 2, 2023

Thanks for your advice.
Therefore, I use Image.crop from the PIL library to do the job.

@jklymak
Copy link
Member
jklymak commented Feb 9, 2023

I'm closing as a duplicate of #23399, but we probably should fix figimage for modern hiDPI...

@jklymak jklymak closed this as not planned Won't fix, can't repro, duplicate, stale Feb 9, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants
0