julia系列5:文本、图像、其他语言函数互动

1. 文件读写

语法和python很像,读文件使用open-readlines-close,写文件使用open-println(file,data)-close
使用DelimitedFiles包中的readlm()读取csv文件。

1.1 读文件

readline读取一行;readlines读取所有行为一个数组;eachline读取为一个迭代器:
在这里插入图片描述

1.2 写文件

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

1.3 json文件

using ProgressMeter
@showprogress 1 "Retrieving Julian info..." for ...

using JSON
# write to file
userdata = Dict("nodes"=>usernodes, "links"=>userlinks)
open("Julians.json", "w") do f
  JSON.print(f, userdata, 2)
end

2. 与python/c/Fortran交互

通过PyCall包,Julia可以直接调用Python包。例如:

using PyCall
np= pyimport("numpy")
x = np.linspace(1, 10, 10)

数值、布尔、字符串、IO stream、函数、元组、数组或列表、以及包含这些类型的字典等,它们都会自动进行类型的转换(Python函数会被转换或传递为Julia的函数,反之亦然)。其它类型则是通过通用的PyObject提供的。
下面是调用c函数的例子。

ccall((:clock, "libc"), Int32, ()) # 调用C的clock函数

3. 绘图

在这里插入图片描述

3.1 pyplot

既然都装了python了,就用pyplot绘图吧。

using PyPlot
x=1:10
xlabel("x")
y=ones(10)
for i=1:10
    y[i]=-i*i*i
end
ylabel("y")
title("XKCD plot")
plot(x,y)

另一种方式:

在这里插入图片描述
第三种方式:
在这里插入图片描述

3.2 使用Images

using Images
url = "..."
download(url,"test.jpg")
load("test.jpg")

下载图片,并绘制原图

3.3 skimage

pyplot()
@pyimport skimage.io as io
io.imshow("test.jpg") # 也可以直接放链接

3.4 Plots

using Plots
import FileIO
pyplot()
plot(FileIO.load("test.jpg"))

猜你喜欢

转载自blog.csdn.net/kittyzc/article/details/126105227
今日推荐