在这篇文章中,我们将带领您了解将图像保存为文件中的numpy数组,并使用Python从文件中将其作为图像加载回来的全貌,同时,我们还将为您介绍有关"importnumpyasnp"ImportErro
在这篇文章中,我们将带领您了解将图像保存为文件中的 numpy 数组,并使用 Python 从文件中将其作为图像加载回来的全貌,同时,我们还将为您介绍有关"import numpy as np" ImportError: No module named numpy、Difference between import numpy and import numpy as np、HTML-PARSING 并使用 python 从网页中获取链接、Jupyter Notebooks 嵌入 Excel 并使用 Python 替代 VBA 宏的知识,以帮助您更好地理解这个主题。
本文目录一览:- 将图像保存为文件中的 numpy 数组,并使用 Python 从文件中将其作为图像加载回来
- "import numpy as np" ImportError: No module named numpy
- Difference between import numpy and import numpy as np
- HTML-PARSING 并使用 python 从网页中获取链接
- Jupyter Notebooks 嵌入 Excel 并使用 Python 替代 VBA 宏
将图像保存为文件中的 numpy 数组,并使用 Python 从文件中将其作为图像加载回来
如何解决将图像保存为文件中的 numpy 数组,并使用 Python 从文件中将其作为图像加载回来
我正在尝试将图像转换为 numpy 数组并将其另存为文本/csv 文件。然后我尝试将 text/csv 文件的内容加载回图像中。
在整个过程中,维度、数据类型的像素值不能改变,以便准确地重建原始图像(不失真)。
到目前为止我所拥有的-
testim = cv2.imread(''img.jpg'') #reading the input image
numpyimg = np.array(testim) # Saving as a numpy array
# Checking the shape and image
cv2_imshow(numpyimg)
print(numpyimg.shape)
# Trying to save in csv
for i in numpyimg:
np.savetxt(fname="image_array.csv",delimiter=",",X=i)
# Check generated csv file after loading it
image_array = np.loadtxt(
fname="image_array.csv","
)
print("NumPy array: \\n",image_array)
print("Shape: ",image_array.shape)
print("Data Type: ",image_array.dtype.name)
当我打印保存文件的内容时,我看到了什么 -
NumPy array that I Could saved in a file:
[[ 70. 176. 153.]
[ 63. 170. 144.]
[ 57. 167. 139.]
...
[ 69. 118. 80.]
[ 67. 117. 77.]
[ 64. 114. 74.]]
Shape: (1040,3)
原图的数组虽然-
array([[[ 78,120,165],[ 63,105,150],[ 48,91,134],...,[ 22,80,51],[ 35,62],[ 49,76]],[[ 77,122,160],[ 62,109,147],[ 50,95,132],[ 24,84,54],[ 29,87,58],[ 38,96,67]],[[ 73,124,[ 66,143],116,137],[ 28,90,60],[ 26,86,56],[ 27,57]],[ 69,118,80],[ 67,117,77],[ 64,114,74]]],dtype=uint8)
shape: (780,1040,3)
这些看起来不太一样,我不明白出了什么问题。
有没有更简单、更准确的方法来解决这个问题?
我已经坚持了很长时间。任何帮助表示赞赏!
解决方法
这些看起来不太一样,我不明白出了什么问题。
表示彩色图像 OpenCV
使用三维数组。要访问单个值,您必须提供 3:Y 坐标、X 坐标、哪个颜色通道(0
表示 Blue,1
表示 Green >,2
表示 Red,如果我没记错 OpenCV 约定的话。
text/csv
非常适合表示 2D 数据(想想电子表格),但如果您希望有更多维度,则需要在写入之前和读取之后进行特殊处理。
RFC4180 不提供与列内容类型相关的任何功能。
Have you focused on the size of both arrays?
这是 CSV 文件的大小 = (1040,3)。
原始图片的大小 = (780,1040,3).
尝试在任何操作之前将图像从 RGB 转换为灰度:
,
cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)
经过多次反复试验,我找到了解决方案。这就是帮助我解决问题的原因-
from PIL import Image
# Create an empty text file before this step
with open(''image_array.txt'',''w'') as outfile:
for slice_2d in numpyimg:
np.savetxt(outfile,slice_2d)
new_data = np.loadtxt(''image_array.txt'')
new_data=new_data.reshape((780,3))
img = Image.fromarray(new_data.astype(np.uint8),''RGB'')
img.save(''try.jpg'')
img.show()
"import numpy as np" ImportError: No module named numpy
问题:没有安装 numpy
解决方法:
下载文件,安装
numpy-1.8.2-win32-superpack-python2.7
安装运行 import numpy,出现
Traceback (most recent call last):
File "<pyshell#2>", line 1, in <module>
import numpy
File "C:\Python27\lib\site-packages\numpy\__init__.py", line 153, in <module>
from . import add_newdocs
File "C:\Python27\lib\site-packages\numpy\add_newdocs.py", line 13, in <module>
from numpy.lib import add_newdoc
File "C:\Python27\lib\site-packages\numpy\lib\__init__.py", line 8, in <module>
from .type_check import *
File "C:\Python27\lib\site-packages\numpy\lib\type_check.py", line 11, in <module>
import numpy.core.numeric as _nx
File "C:\Python27\lib\site-packages\numpy\core\__init__.py", line 6, in <module>
from . import multiarray
ImportError: DLL load failed: %1 不是有效的 Win32 应用程序。
原因是:python 装的是 64 位的,numpy 装的是 32 位的
重新安装 numpy 为:numpy-1.8.0-win64-py2.7
Difference between import numpy and import numpy as np
Difference between import numpy and import numpy as np
up vote 18 down vote favorite 5 |
I understand that when possible one should use This helps keep away any conflict due to namespaces. But I have noticed that while the command below works the following does not Can someone please explain this? python numpy
|
||||||||
add a comment |
4 Answers
active oldest votes
up vote 13 down vote |
numpy is the top package name, and doing When you do In your above code: Here is the difference between
|
|||
add a comment |
up vote 7 down vote |
The When you import a module via the numpy package is bound to the local variable Thus, is equivalent to, When trying to understand this mechanism, it''s worth remembering that When importing a submodule, you must refer to the full parent module name, since the importing mechanics happen at a higher level than the local variable scope. i.e. I also take issue with your assertion that "where possible one should [import numpy as np]". This is done for historical reasons, mostly because people get tired very quickly of prefixing every operation with Finally, to round out my exposé, here are 2 interesting uses of the 1. long subimports 2. compatible APIs
|
||
add a comment |
up vote 1 down vote |
when you call the statement
|
||
add a comment |
up vote 1 down vote |
This is a language feature. This feature allows:
Notice however that Said that, when you run You receive an
|
||||||||
add a comment |
HTML-PARSING 并使用 python 从网页中获取链接
如何解决HTML-PARSING 并使用 python 从网页中获取链接
我正在尝试从网页中收集所有 url,旨在对每个名称执行 NER 和传记。 我一直在使用 html 解析器。它给了我一些网址的链接,但它失败了,并且没有给其他一些结果,我在网址中发布了一些没有给我结果的网址。
- **https://ctinnovations.com/learn-about-connecticut-innovations/meet-our-team/
- https://foundationcapital.com/our-team/**
我的代码如下:—
!pip install httplib2
import httplib2
from bs4 import BeautifulSoup,SoupStrainer
# EXTRACTING ALL WEB LINKS FROM A PARTIculaR WEBPAGE
j = vmsing_list.index(sourcefile)
print(j)
exturl = url_list[j][0]
print(exturl)
http = httplib2.Http()
response,content = http.request(exturl)
# STORING THE LINKS IN A LIST
links=[]
for link in BeautifulSoup(content).find_all(''a'',href=True):
links.append(link[''href''])
## AVAILING THE LINKS
p = range(len(links))
linklower =[]
for i in p :
site = links[i].lower()
linklower.append(site)
print(linklower[i])
#print(links)
我没有得到任何结果作为上述 URL 的输出。然而,对于许多网址来说,它在其他方面运行良好。
如果有人能告诉我什么应该是更好的代码来为所有文件工作,我很感激。
谢谢
Jupyter Notebooks 嵌入 Excel 并使用 Python 替代 VBA 宏
以前,Excel 和 Python Jupyter Notebook 之间我们只能选择一个。但是现在随着 PyXLL-Jupyter 软件包的推出,可以将两者一起使用。
在本文中,我将向你展示如何设置在 Excel 中运行的 Jupyter Notebook。在这两者之间共享数据,甚至可以从 Excel 工作簿调用 Jupyter 笔记本中编写的 Python 函数!
开始
首先,要在 Excel 中运行 Python 代码,你需要使用 PyXLL 包。PyXLL 使我们可以将 Python 集成到 Excel 中,并使用 Python 代替 VBA。要安装 PyXLL Excel 加载项 “pip install pyxll”,然后使用 PyXLL 命令行工具安装 Excel 的加载项:
>> pip install pyxll
>> pyxll install
安装完 PyXLL Excel 插件,下一步就是安装 PyXLL -jupyter 软件包。该软件包提供了 PyXLL 和 Jupyter 之间的链接,因此我们可以在 Excel 内使用 Jupyter 笔记本。
使用 pip 安装 pyxll-jupyter 包:
>> pip install pyxll-jupyter
一旦安装了 PyXLL Excel 加载项和 PyXLL-Jupyter 软件包后,启动 Excel 将在 PyXLL 选项卡中看到一个新的 “Jupyter” 按钮。
单击此按钮可在 Excel 工作簿的侧面板中打开 Jupyter 笔记本。该面板是 Excel 界面的一部分,可以通过拖放操作取消停靠或停靠在其他位置。
在 Jupyter 面板中,你可以选择一个现有的笔记本或创建一个新的笔记本。要创建一个新的笔记本,请选择 “新建” 按钮,然后选择 “ Python 3”。
如何使用
现在,你已经在 Excel 中运行了完整的 Jupyter 笔记本!但是,这有什么好处呢?这比在 Excel 外部运行笔记本更好?
好了,现在你可以使用 Excel 处理数据,并使用 Python 处理相同的数据。将 Excel 用作用于组织和可视化数据的交互式操作,无缝切换到 Python 以使用更复杂的功能。
将 Jupyter 笔记本用作草稿板,以试用 Python 代码。在 Jupyter 笔记本上完全用 Python 编写 Excel 函数,并进行实时测试。开发完一个有用的可重用函数后,将其添加到 PyXLL Python 项目中。这样你每次使用 Excel 时都可以使用相同的函数。
在本文的其余部分,我将向你展示如何:
使用 Jupyter 笔记本在 Excel 和 Python 之间共享数据
在笔记本上写 Excel 工作表函数 (udf)
脚本 Excel 与 Python 代替 VBA
从 Excel 获取数据到 Python
因为 PyXLL 在与 Excel 相同的进程中运行 Python,所以用 Python 访问 Excel 数据以及在 Python 和 Excel 之间快速调用。
为了使事情尽可能简单,pyxll-jupyter 包附带了一些 IPython “魔法” 函数,可以在你的 Jupyter 笔记本中使用。
% xl_get
excel sheet 与 Pandas DataFrames 同步
使用魔术函数 “%xl_get” 来获取 Python 中当前的 Excel 选择。在 Excel 中创建数据表, 选择左上角(或整个范围),然后在 Jupyter 笔记本中输入 “%xl_get”,瞧!Excel 表现在是 pandas DataFrame。
%xl_get 魔术函数有几个选项:
-c或--cell。传递单元格的地址以获取值,例如%xl_get --cell A1:D5。
-t或--type。指定获取值时要使用的数据类型,例如%xl_get --type numpy_array。
-x或--no-auto-resize。仅获取选定范围或给定范围的数据。不要扩展到包括周围的数据范围。
PyXLL 还有其他与 Excel 交互以将数据读入 Python 的方式。“%xl_get” 魔术功能只是使事情变得更简单!当 Jupyter 笔记本在 Excel 中运行时,所有其他方法(例如,使用 XLCell 类,Excel 的 COM API 甚至 xlwings)仍然可用。
提示:可以为魔术函数的结果分配一个变量!例如,尝试 “df =%xl_get”。
将 Python 中的数据移回 Excel
从 Python 到 Excel 的另一种传输方式也可以正常工作。无论你是使用 Python 加载数据集并将其传输到 Excel 工作簿,还是通过 Excel 处理数据集并希望将结果返回 Excel,从 Python 复制数据到 Excel 都很容易。
%xl_set
魔术函数 “%xl_set” 获取一个 Python 对象并将其写入 Excel。在 Excel 中是否有想要的数据框 “ df”?只需使用 “%xl_set df”,它将被写入 Excel 中的当前选择。
与%xl_get 一样,%xl_set 也具有一系列选项来控制其行为。你甚至可以使用 PyXLL 的单元格格式设置功能在将结果写入 Excel 的同时自动应用格式设置。
-c或--cell。将值写入的单元格地址,例如%xl_set VALUE --cell A1。
-t或--type。将值写入Excel时要使用的数据类型说明符,例如%xl_set VALUE --type dataframe <index = False>。
-f或--formatter。PyXLL单元格格式化程序对象,例如%xl_set VALUE --formatter DataFrameFormatter()。请参阅单元格格式。
-x或--no-auto-resize。不要自动调整范围大小以适合数据。仅将值写入当前选择或指定范围。
与%xl_get 一样,%xl_set 只是一个快捷方式,你可能已与 PyXLL 一起使用的所有其他写回 Excel 的方式仍然可以在 Jupyter 笔记本中使用。
在 Excel 中使用 Python 图(matplotlib /plotly 等)
关于数据处理的一大优点是可用的功能强大的绘图程序包。例如 df.plot ()
PyXLL 集成了所有主要的绘图库,因此你也可以在 Excel 中充分利用它们。这包括 matplotlib(由 pandas 使用),plotly,bokeh 和 altair。
%xl_plot
使用 “%xl_plot” 在 Excel 中绘制任何 Python 图表。从一个受支持的绘图库中向其传递任何图形对象,或使用最后一个 pyplot 图形。使用 pandas plot 的效果也很好,例如。% xl_plot df.plot (kind=''scatter'').
%xl_plot 魔术函数具有一些选项来控制其工作方式:
-n或--name。Excel中图片对象的名称。如果使用已经存在的图片名称,则该图片将被替换。
-c或--cell。用作新图片位置的单元格地址。如果图片已经存在,则无效。
-w或--width。Excel中图片的宽度(以磅为单位)。如果更新现有图片,则无效。
-h或--height。Excel中图片的高度(以磅为单位)。如果更新现有图片,则无效。
%xl_plot 是 pyxll.plot 函数的快捷方式。
从 Excel 调用 Python 函数
你可以直接从 Excel 工作簿中调用 Python 函数,而不是在 Excel 和 Jupyter 之间不断移动数据然后运行一些 Python 代码
PyXLL 的主要用例之一是用 Python 编写自定义 Excel 工作表函数(或 “UDF”)。这用于在使用 Python 函数构建的 Excel 中构建模型,这些函数当然可以使用其他 Python 库(例如 pandas 和 scipy)。
你也可以在 Jupyter 笔记本中编写 Excel 工作表函数。这是在不离开 Excel 即可使用 Python IDE 的情况下尝试想法的绝佳方法。
自己试试吧。编写一个简单的函数,然后将 “pyxll.xl_func” 修饰符添加到你的函数中:
from pyxll import xl_func
@xl_func
def test_func(a, b, c):
# This function can be called from Excel!
return (a * b) + c
输入代码并在 Jupyter 中运行单元格后,即可立即从 Excel 工作簿中调用 Python 函数。
不只是简单的功能。你可以将整个数据范围作为 pandas DataFrames 传递给函数,并返回任何 Python 类型,包括 numpy 数组和 DataFrames!你可以通过给 @xl_func 装饰器一个参数字符串来告诉 PyXLL 期望什么类型。
例如,尝试以下方法:
from pyxll import xl_func
# The "signature" tells PyXLL how to convert the arguments
# and returned value.
@xl_func("dataframe df: dataframe<index=True>", auto_resize=True)
def df_describe(df):
# ''df'' is a pandas DataFrame built from the range passed
# to this function.
desc = df.describe()
# ''desc'' is a new DataFrame, which PyXLL will convert to
# a range of values when returning to Excel.
return desc
现在,你可以编写复杂的 Python 函数来进行数据转换和分析,Excel 中如何调用或排序这些函数。更改输入会导致调用函数,并且计算出的输出会实时更新,这与你期望的一样!
在 Excel 中使用 Python 而不是 VBA 的脚本
你是否知道在 VBA 中可以执行的所有操作也可以在 Python 中完成?编写 VBA 时将使用 Excel 对象模型,但是 Python 也提供相同的 API。
在 Excel 中运行的 Jupyter 笔记本中,可以使用整个 Excel 对象模型,因此你可以使用与 Excel VBA 编辑器中完全相同的方式编写 Excel 脚本。
由于 PyXLL 在 Excel 进程内运行 Python,因此从 Python 调用 Excel 不会对性能造成任何影响。也可以从外部 Python 进程调用 Excel,但这通常要慢得多。在 Excel 中运行 Jupyter 笔记本也使一切变得更加便捷!
使用 PyXLL 的 xl_app 函数获取 “Excel.Application” 对象,该对象等效于 VBA 中的 Application 对象。尝试进行诸如获取当前选择和更改单元格内部颜色之类的操作。弄清楚如何使用 Excel 对象模型进行操作的一种好方法是记录 VBA 宏,然后将该宏转换为 Python!PyXLL 文档页面 Python 作为 VBA 的替代品提供了一些有关如何做到这一点的技巧。
总结
Python 是 VBA 的强大替代品。使用 PyXLL,你可以完全用 Python 编写功能齐全的 Excel 加载项。Excel 是一种出色的交互式计算工具。添加 Python 和 Jupyter 将 Excel 提升到一个全新的水平。
使用 Jupyter 笔记本编写的代码可以轻松地重构为独立的 Python 包,以创建 Excel 工具包来为直观的工作簿和仪表板提供动力。任何 Excel 用户都将能够利用使用 PyXLL 编写的 Python 工具,而无需任何 Python 知识。
最后 PyXLL 的官网地址:https://www.pyxll.com/blog/python-jupyter-notebooks-in-excel/
作者:Tony Roberts
原文地址:https://towardsdatascience.com/python-jupyter-notebooks-in-excel-5ab34fc6439
deephub 翻译组
本文分享自微信公众号 - DeepHub IMBA(deephub-imba)。
如有侵权,请联系 support@oschina.cn 删除。
本文参与 “OSC 源创计划”,欢迎正在阅读的你也加入,一起分享。
关于将图像保存为文件中的 numpy 数组,并使用 Python 从文件中将其作为图像加载回来的问题就给大家分享到这里,感谢你花时间阅读本站内容,更多关于"import numpy as np" ImportError: No module named numpy、Difference between import numpy and import numpy as np、HTML-PARSING 并使用 python 从网页中获取链接、Jupyter Notebooks 嵌入 Excel 并使用 Python 替代 VBA 宏等相关知识的信息别忘了在本站进行查找喔。
本文标签: