亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

無法繪制圖像周圍已移除空白的圖像

無法繪制圖像周圍已移除空白的圖像

Cats萌萌 2023-02-15 15:52:22
我有以下生成分形圖像的代碼,import numpy as npimport matplotlib.pyplot as pltdef julia(C):    X = np.arange(-1.5, 1.5, 0.2)    Y = np.arange(-1.5, 1.5, 0.2)    pixel = np.zeros((len(Y), len(X)))    for x_iter, x in enumerate(X):        for y_iter, y in enumerate(Y):            z = x + 1j * y            intensity = np.nan            r = np.empty((100, 100)) # Unused at the moment            for n in range(1, 1024):                if abs(z) > 2:                    intensity = n                    break                z = z**2 + C            pixel[y_iter, x_iter] = intensity            r.fill(intensity) # Unused at the moment    # We return pixel matrix    return pixel# Compute Julia set imagepixel = julia(-0.7 + 0.27015j)# Plottingprint(pixel[:,:])數據(像素)如下:array([[  1.,   2.,   2.,   2.,   2.,   2.,   2.,   2.,   2.,   2.,   2.,          2.,   2.,   2.,   2.],       [  2.,   2.,   2.,   2.,   2.,   2.,   2.,   2.,   2.,   2.,   2.,          2.,   2.,   2.,   2.],       [  2.,   2.,   2.,   2.,   2.,   2.,   2.,   3.,   3.,   3.,   3.,          3.,   2.,   2.,   2.],       [  2.,   2.,   2.,   2.,   3.,   3.,   3.,   4.,   5.,   4.,   4.,          3.,   3.,   3.,   2.],       [  2.,   2.,   3.,   3.,   3.,   4.,   4.,   7., 209.,   6.,   5.,          4.,   4.,   3.,   3.],       [  2.,   3.,   3.,   3.,   4.,   5.,   6.,  37.,  59., 220.,  13.,          7.,  10.,   6.,   4.],繪制數據(像素)后,它看起來像plt.axis('off')plt.imshow(pixel)拿到圖片后我做了plt.savefig(),當我做image.open()的時候,數據變成了下面這樣!我需要擺脫所有這些 255 強度對應的邊界不需要的空白
查看完整描述

1 回答

?
蕪湖不蕪

TA貢獻1796條經驗 獲得超7個贊

您可以使用以下腳本來保存周圍沒有空格的圖像:


# Plotting

from PIL import Image

min_value = np.nanmin(pixel)

max_value = np.nanmax(pixel)

pixel_int = (255*(pixel-min_value)/(max_value-min_value)).astype(np.uint8)

# sample LUT from matplotlib

lut = (plt.cm.viridis(np.arange(256)) * 255).astype(np.uint8) # CHOOSE COLORMAP HERE viridis, jet, rainbow

pixel_rgb = lut[pixel_int]

# changing NaNs to a chosen color

nan_color = [0,0,0,0] # Transparent NaNs

for i,c in enumerate(nan_color):

  pixel_rgb[:,:,i] = np.where(np.isnan(pixel),c,pixel_rgb[:,:,i])

# apply LUT and display

img = Image.fromarray(pixel_rgb, 'RGBA')


# Saving image and matrix

img.save('julia.png')

np.save('julia.npy', pixel)


# Delete data

del(img, pixel)


# Loading image and matrix

img = Image.open('julia.png')

pixel = np.load("julia.npy")


# Show image

img.show()


print(pixel)

print(min_value, max_value)

每個數組值將獲得一個像素。X和軸的 2 個分辨率的輸出Y:


X,Y = (np.arange(-1.5, 1.5, 0.2),)*2: http://img1.sycdn.imooc.com//63ec8f7e000176e101540172.jpg

X,Y = (np.arange(-1.5, 1.5, 0.02),)*2:


查看完整回答
反對 回復 2023-02-15
  • 1 回答
  • 0 關注
  • 121 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號