Image matplotlib:jpg图像特效/转换,就像它在一本打开的书的一页上一样
我希望出现一个图形(和某些文本),就好像它们打印在一本打开的书上一样。是否可以通过编程或在matplotlib中转换jpg图像来产生这样的效果 您可以使用一个背景轴和一本开源书来做类似的事情Image matplotlib:jpg图像特效/转换,就像它在一本打开的书的一页上一样,image,python-2.7,matplotlib,Image,Python 2.7,Matplotlib,我希望出现一个图形(和某些文本),就好像它们打印在一本打开的书上一样。是否可以通过编程或在matplotlib中转换jpg图像来产生这样的效果 您可以使用一个背景轴和一本开源书来做类似的事情 import numpy as np import matplotlib.pyplot as plt fig = plt.figure() ax1 = fig.add_axes([0.1, 0.1, 0.8, 0.8]) ax2 = fig.add_axes([0.2, 0.3, 0.25, 0.3])
import numpy as np
import matplotlib.pyplot as plt
fig = plt.figure()
ax1 = fig.add_axes([0.1, 0.1, 0.8, 0.8])
ax2 = fig.add_axes([0.2, 0.3, 0.25, 0.3])
#Plot page from a book
im = plt.imread("./book_page.jpg")
implot = ax1.imshow(im, origin='lower')
# Plot a graph and set background to transparent
x = np.linspace(0,4.*np.pi,40)
y = np.sin(x)
ax2.plot(x,y,'-ro',alpha=0.5)
ax2.set_ylim([-1.1,1.1])
ax2.patch.set_alpha(0.0)
from matplotlib import rc
rc('text', usetex=True)
margin = im.shape[0]*0.075
ytext = im.shape[1]/2.+10
ax1.text(margin, ytext, "The following text is an example")
ax1.text(margin, 90, "Figure 1. Showing a sine function")
plt.show()
看起来像这样
import numpy as np
import matplotlib.pyplot as plt
fig = plt.figure()
ax1 = fig.add_axes([0.1, 0.1, 0.8, 0.8])
ax2 = fig.add_axes([0.2, 0.3, 0.25, 0.3])
#Plot page from a book
im = plt.imread("./book_page.jpg")
implot = ax1.imshow(im, origin='lower')
# Plot a graph and set background to transparent
x = np.linspace(0,4.*np.pi,40)
y = np.sin(x)
ax2.plot(x,y,'-ro',alpha=0.5)
ax2.set_ylim([-1.1,1.1])
ax2.patch.set_alpha(0.0)
from matplotlib import rc
rc('text', usetex=True)
margin = im.shape[0]*0.075
ytext = im.shape[1]/2.+10
ax1.text(margin, ytext, "The following text is an example")
ax1.text(margin, 90, "Figure 1. Showing a sine function")
plt.show()
我用了下面的书
更新:添加了基于scikit图像扭曲的非仿射变换,但具有麦克斯韦分布。该解决方案将matplotlib线保存为图像,以便应用逐点变换。矢量图形的映射可能是可能的,但我认为这将更加复杂
import numpy as np
import matplotlib.pyplot as plt
def maxwellian_transform_image(image):
from skimage.transform import PiecewiseAffineTransform, warp
rows, cols = image.shape[0], image.shape[1]
src_cols = np.linspace(0, cols, 20)
src_rows = np.linspace(0, rows, 10)
src_rows, src_cols = np.meshgrid(src_rows, src_cols)
src = np.dstack([src_cols.flat, src_rows.flat])[0]
# add maxwellian to row coordinates
x = np.linspace(0, 3., src.shape[0])
dst_rows = src[:, 1] + (np.sqrt(2/np.pi)*x**2 * np.exp(-x**2/2)) * 50
dst_cols = src[:, 0]
dst_rows *= 1.5
dst_rows -= 1.0 * 50
dst = np.vstack([dst_cols, dst_rows]).T
tform = PiecewiseAffineTransform()
tform.estimate(src, dst)
out_rows = image.shape[0] - 1.5 * 50
out_cols = cols
out = warp(image, tform, output_shape=(out_rows, out_cols))
return out
#Create the new figure
fig = plt.figure()
ax = fig.add_axes([0.1, 0.1, 0.8, 0.8])
#Plot page from a book
im = plt.imread("./book_page.jpg")
implot = ax.imshow(im, origin='lower')
# Plot and save graph as image, will need some manipulation of location
temp, at = plt.subplots()
margin = im.shape[0]*0.1
x = np.linspace(margin,im.shape[0]/2.,40)
y = im.shape[1]/3. + 0.1*im.shape[1]*np.sin(12.*np.pi*x/im.shape[0])
at.plot(x,y,'-ro',alpha=0.5)
temp.savefig("lineplot.png",transparent=True)
#Read in plot as an image and apply transform
plot = plt.imread("./lineplot.png")
out = maxwellian_transform_image(plot)
ax.imshow(out, extent=[0,im.shape[1],0,im.shape[0]])
plt.show()
这个数字现在看起来像
@Smith,谢谢你的回答,这更接近了,但我正在努力解决的是——曲线形状与图形。比方说,如果我将曲线页面定义为具有类似于麦克斯韦函数的函数形式,我想缩放/变换图像以跟随该曲线,因此,它看起来像是打印在一个开放的曲线页面上。例如,你所说的是仿射变换。我已经相应地更新了我的答案,虽然我不确定你问的是无关紧要的问题,但这正是我想要的。这有帮助。基于此,现在我可以构造我想要的变换。