相关疑难解决方法(0)

指定并保存具有精确大小(以像素为单位)的图形

说我的图像大小为3841 x 7195像素.我想将图中的内容保存到磁盘,从而产生一个我指定的确切大小的图像(以像素为单位).

没有轴,没有标题.只是图像.我个人并不关心DPI,因为我只想指定图像在磁盘屏幕中所占的大小(以像素为单位).

我已经阅读了其他 主题,他们似乎都转换为英寸,然后以英寸为单位指定数字的尺寸,并以某种方式调整dpi.我想避免处理像素到英寸转换可能导致的精度损失.

我尝试过:

w = 7195
h = 3841
fig = plt.figure(frameon=False)
fig.set_size_inches(w,h)
ax = plt.Axes(fig, [0., 0., 1., 1.])
ax.set_axis_off()
fig.add_axes(ax)
ax.imshow(im_np, aspect='normal')
fig.savefig(some_path, dpi=1)
Run Code Online (Sandbox Code Playgroud)

没有运气(Python抱怨宽度和高度必须都低于32768(?))

一切从我所看到的,matplotlib需要到指定的数字大小inchesdpi,但我只关心像素的数字发生在磁盘上.我怎样才能做到这一点?

澄清:我正在寻找一种方法matplotlib,而不是与其他图像保存库.

python matplotlib scipy

127
推荐指数
4
解决办法
13万
查看次数

在 Python 中将频谱图存储为图像

我想将音频的 STFT 频谱图存储为图像。下面的代码向我显示了一个频谱图作为输出,但是当保存为图像时,我得到了不同的图像。

import numpy as np
import matplotlib.pyplot as plt
import pandas as pd

audio_name = '---.au'
hop_length = 512
window_size = 1024

import librosa
y, sr = librosa.load(audio_name)
window = np.hanning(window_size)
out  = librosa.core.spectrum.stft(y, n_fft = window_size, hop_length = hop_length, 
       window=window)
out = 2 * np.abs(out) / np.sum(window)

import librosa.display
librosa.display.specshow(librosa.amplitude_to_db(out,ref=np.max),
               y_axis='log', x_axis='time')
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

from PIL import Image
img = Image.fromarray(out)    
if img.mode != 'RGBA':
    img = img.convert('RGBA')
img.save('output.png')
Run Code Online (Sandbox Code Playgroud)

但是当我保存它时,输出文件是一个黑色图像。在此处输入图片说明

我想保存光谱的确切图像。

audio python-imaging-library python-3.x librosa

6
推荐指数
1
解决办法
1万
查看次数