使用matplotlib将图像设置在图形中心Figure.figimage

cotxawn7  于 2023-03-09  发布在  其他
关注(0)|答案(2)|浏览(213)

我试着设置一个图像,一个水印,在matplotlib图形的中心。我希望图像的中心在图形的中心。问题似乎是图形大小和10”x 10”和960 x 960像素的dpi与保存的图像(像素大小为610 x 623)之间的差异。当我通过图形添加图像时。figimage在960/2,960/2,这是完成的图像偏心。我的代码和示例图像如下。
创建绘图:

  1. f, ax = plt.subplots(1, figsize=(10,10))
  2. f.set_facecolor((1,1,1))
  3. ax.set_title('{} Reflection Integrated Intensity vs Max Intensity of {}'.format(refl, rcmap.sample_number), size=14)
  4. f = add_logo(f, '/home/zeppelin_d/Desktop/company.com_logo.png',x_frac=0.5, y_frac=0.5, scale=0.5, alpha=0.15)
  5. ax = sns.regplot(x='z', y='int_int', data=df, scatter_kws={'alpha':0.4})
  6. ax.set_xlabel('{} Max Intensity'.format(refl), size=14)
  7. ax.set_ylabel('{} Integrated Intensity'.format(refl, size=14))
  8. ax.set_xlim([0,max(df['z']) * 1.05])
  9. ax.set_ylim([0,max(df['int_int']) * 1.05])

添加图像:

  1. def add_logo(f, path, x_frac=0.5, y_frac=0.5, scale=1, alpha=1):
  2. """
  3. Add an image to the figure (not the axes)
  4. f: a matplotlib figure instance.
  5. path: the string path to the image to add to the figure.
  6. x_frac: the fraction of the x dimension of the figure to set the offset to.
  7. Must be a float.
  8. y_frac: the fraction of the y dimension of the figure to set the offset to.
  9. Must be a float.
  10. scale: the float scale by which to multiply to the image pixel dimensions.
  11. alpha: the alpha to set the inserted image to
  12. Set the figure dpi to the same as screen dpi.
  13. Use this like:
  14. f = add_logo(f, 'mah_business_logo.png',x_frac=0.5, y_frac=0.5, scale=0.5, alpha=0.15)
  15. for setting a watermark. This should put the center of the image in the center of the
  16. figure at half it's original size.
  17. """
  18. assert type(x_frac) == float and type(y_frac) == float, "x_frac and y_frac must be floats."
  19. im = Image.open(path)
  20. f.set_dpi(96)
  21. im.thumbnail((int(im.size[0] * scale), int(im.size[1] * scale)), Image.ANTIALIAS)
  22. img_x, img_y = im.size[0], im.size[1]
  23. x_offset = int((f.bbox.xmax * x_frac - img_x/2))
  24. y_offset = int((f.bbox.ymax * y_frac - img_y/2))
  25. f.figimage(im, xo=x_offset, yo=y_offset, origin='upper', zorder=10, alpha=alpha)
  26. return f

x1c 0d1x我怎样才能知道保存的图像在任何比例下的尺寸?或者为什么完成的图像实际上不是10 x10”和960 x960 pixels?谢谢。

ct2axkht

ct2axkht1#

为了从10“x10”图中得到960x960像素的图形,您还必须将plt.savefig()中的dpi关键字设置为96 dpi。

  1. plt.savefig('im.png', dpi=96)

输出正确:

而对于例如72 dpi,结果是:

kgqe7b3p

kgqe7b3p2#

对你的功能做些小小的调整对我很有效...

  1. def add_watermark(figure, source_file, max_scale=0.6, alpha=0.15):
  2. """ add a watermark to an image
  3. Parameters
  4. ----------
  5. figure : matplotlib.figure
  6. figure object
  7. source_file : Path / str
  8. source file
  9. max_scale : float
  10. maximum scale of watermark vs whole figure
  11. alpha : float
  12. transparency of watermark
  13. """
  14. wm = Image.open(source_file)
  15. wm_size = np.array(wm.size)
  16. max_size = figure.get_size_inches() * figure.dpi * max_scale
  17. scaling_factor = min(max_size / wm_size)
  18. img_size = (wm_size * scaling_factor).astype(int)
  19. wmr = wm.resize(img_size, Resampling.LANCZOS)
  20. x_offset = int((figure.bbox.xmax - img_size[0]) * 0.5)
  21. y_offset = int((figure.bbox.ymax - img_size[1]) * 0.5)
  22. figure.figimage(wmr, xo=x_offset, yo=y_offset, origin='upper', alpha=alpha)
展开查看全部

相关问题