GVKun编程网logo

Python-将NumPy数组转储到csv文件中(将numpy数组转换为图片)

22

如果您对Python-将NumPy数组转储到csv文件中感兴趣,那么本文将是一篇不错的选择,我们将为您详在本文中,您将会了解到关于Python-将NumPy数组转储到csv文件中的详细内容,我们还将为

如果您对Python-将NumPy数组转储到csv文件中感兴趣,那么本文将是一篇不错的选择,我们将为您详在本文中,您将会了解到关于Python-将NumPy数组转储到csv文件中的详细内容,我们还将为您解答将numpy数组转换为图片的相关问题,并且为您提供关于MySQL转储到CSV文本文件中,列名在顶部?[复制]、python numpy数组的numpy数组、python – 如何将numpy数组流式传输到pyaudio流?、python – 将boolean numpy数组转换为枕头图像的有价值信息。

本文目录一览:

Python-将NumPy数组转储到csv文件中(将numpy数组转换为图片)

Python-将NumPy数组转储到csv文件中(将numpy数组转换为图片)

有没有办法将NumPy数组转储到CSV文件中?我有一个2D NumPy数组,需要以人类可读的格式转储它。

答案1

小编典典

numpy.savetxt 将数组保存到文本文件。

import numpya = numpy.asarray([ [1,2,3], [4,5,6], [7,8,9] ])numpy.savetxt("foo.csv", a, delimiter=",")

MySQL转储到CSV文本文件中,列名在顶部?[复制]

MySQL转储到CSV文本文件中,列名在顶部?[复制]

我使用以下SQL代码段将表转储为CSV文本文件:

SELECT * FROM brand INTO OUTFILE“ e:/brand.csv”字段以’,’结尾,’‘’以’\ n’结尾;

但是,这种方法不会在CSV文件的开头添加列名。我的问题是如何也选择所有列/字段名称,就像phpMyAdmin在导出表并选择“在第一行中放入字段名称”时所做的一样。

python numpy数组的numpy数组

python numpy数组的numpy数组

我在创建numpy数组的numpy数组时遇到问题。我将在一个循环中创建它:

a=np.array([])
while(...):
   ...
   b= //a numpy array generated
   a=np.append(a,b)
   ...

所需结果:

[[1,5,3],[9,10,1],...,[4,8,6]]

实际结果:

[1,3,9,1,... 4,6]

可能吗?我不知道数组的最终尺寸,因此无法使用固定尺寸对其进行初始化。

python – 如何将numpy数组流式传输到pyaudio流?

python – 如何将numpy数组流式传输到pyaudio流?

我正在编写一个代码,根据他的动作给用户提供一些音频输出,我想生成声音,而不是要播放固定数量的wav文件.现在,我正在做的是以numpy格式生成信号,将数据存储在wav文件中,然后将相同的文件读入pyaudio.我认为这是多余的,但是,我找不到办法做到这一点.我的问题是,我可以直接将一个numpy数组(或常规列表)流式传输到我的pyaudio中来播放吗?

解决方法

如果它只是播放而不需要同步到任何东西那么你可以只做以下事情:

# Open stream with correct settings
stream = self.p.open(format=pyaudio.paFloat32,channels=CHANNELS,rate=48000,output=True,output_device_index=1
                         )
# Assuming you have a numpy array called samples
data = samples.astype(np.float32).tostring()
stream.write(data)

我使用这种方法,它对我来说很好.如果您需要同时录制,那么这将无效.

python – 将boolean numpy数组转换为枕头图像

python – 将boolean numpy数组转换为枕头图像

我目前正在使用scikit-image库在 python中处理图像处理.我正在尝试使用索沃拉阈值使用以下代码制作二进制图像:

from PIL import Image
import numpy
from skimage.color import rgb2gray
from skimage.filters import threshold_sauvola

im = Image.open("test.jpg")
pix = numpy.array(im)
img = rgb2gray(pix)

window_size = 25
thresh_sauvola = threshold_sauvola(img,window_size=window_size)
binary_sauvola = img > thresh_sauvola

这给出了以下结果:

enter image description here

输出是一个numpy数组,此图像的数据类型是bool

[[ True  True  True ...  True  True  True]
 [ True  True  True ...  True  True  True]
 [ True  True  True ...  True  True  True]
 ...
 [ True  True  True ...  True  True  True]
 [ True  True  True ...  True  True  True]
 [ True  True  True ...  True  True  True]]

问题是我需要使用以下代码行将此数组转换回PIL图像:

image = Image.fromarray(binary_sauvola)

这使得图像看起来像这样:

enter image description here

我也尝试将数据类型从bool更改为uint8,但之后我将得到以下异常:

AttributeError: 'numpy.ndarray' object has no attribute 'mask'

到目前为止,我还没有找到一个解决方案来获得看起来像阈值结果的PIL图像.

解决方法

PIL的Image.fromarray函数有一个模式’1’图像的错误. This Gist演示了该错误,并显示了一些解决方法.以下是最好的两种解决方法:

import numpy as np
from PIL import Image

# The standard work-around: first convert to greyscale 
def img_grey(data):
    return Image.fromarray(data * 255,mode='L').convert('1')

# Use .frombytes instead of .fromarray. 
# This is >2x faster than img_grey
def img_frombytes(data):
    size = data.shape[::-1]
    databytes = np.packbits(data,axis=1)
    return Image.frombytes(mode='1',size=size,data=databytes)

另见Error Converting PIL B&W images to Numpy Arrays.

今天关于Python-将NumPy数组转储到csv文件中将numpy数组转换为图片的介绍到此结束,谢谢您的阅读,有关MySQL转储到CSV文本文件中,列名在顶部?[复制]、python numpy数组的numpy数组、python – 如何将numpy数组流式传输到pyaudio流?、python – 将boolean numpy数组转换为枕头图像等更多相关知识的信息可以在本站进行查询。

本文标签: