我不知道如何在中绘制像素。我正在绘制矩形,而不是像素,这使得绘制非常慢。
使用以下方法绘制矩形:
canvas.fill_rect在ipy画布中显示图像的代码:
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from PIL import Image
import ipycanvas
from ipycanvas import Canvas
import requests
from io import BytesIO
url = r"https://wallpapercave.com/dwp1x/wp1816238.jpg"
response = requests.get(url)
img = Image.open(BytesIO(response.content))
array = img.tobytes()
canvas = Canvas(width=img.width, height=img.height)
with ipycanvas.hold_canvas():
for i in range(int(len(array)/3)):
r = array[i * 3 + 0] # red
g = array[i * 3 + 1] # green
b = array[i * 3 + 2] # blue
canvas.fill_style = f"#{r:02x}{g:02x}{b:02x}" # setting color
canvas.fill_rect(i%img.width, int(i/img.width), 1, 1) # drawing rectangle
canvas输出:

我正在绘制一个像素的图像,因为我想在图像中应用过滤器。
如何在ipycanvas中绘制像素
发布于 2022-12-01 17:29:14
不确定这是否有用,但考虑到您正在讨论的是过滤,我假设您指的是像卷积这样的东西。Numpy和Scipy帮助了很多人,并提供了各种方法来应用这些方法,并能很好地处理枕头上的图像。
例如:
import requests
from io import BytesIO
from PIL import Image
import numpy as np
from scipy import signal
image_req = requests.get("https://wallpapercave.com/dwp1x/wp1816238.jpg")
image_req.raise_for_status()
image = Image.open(BytesIO(image_req.content))
# create gaussian glur of a given standard deviation
sd = 3
filt = np.outer(*2*[signal.windows.gaussian(int(sd*5)|1, sd)])
filt /= filt.sum()
# interpret image as 3d array
arr = np.array(image)
# apply it to each channel independently, this loop runs in ~0.1 seconds
for chan in range(3):
arr[:,:,chan] = signal.oaconvolve(arr[:,:,chan], filt, mode='same')
# array back into image for display in notebook
Image.fromarray(arr)这样产生的图像如下:

https://stackoverflow.com/questions/74626615
复制相似问题