[英]Mark pixels of a blank image
I Want to mark the pixels,我想标记像素,
Mark=[2, 455, 6, 556, 12, 654, 22, 23, 4,86,.....] in such a way that it will not mark the 1st 2 pixels and then mark next 455 pixels by a color, again for next 6 pixels it will not mark and again mark the next 556 pixels by the same color and so on. Mark=[2, 455, 6, 556, 12, 654, 22, 23, 4,86,.....] 这样它就不会标记第一个 2 个像素,然后用 a 标记接下来的 455 个像素颜色,对于接下来的 6 个像素,它不会标记并再次用相同的颜色标记接下来的 556 个像素,依此类推。 The size of the image is 500x500x3.
图像尺寸为 500x500x3。 How do I calculate these steps?
我如何计算这些步骤?
Img=np.zeros((500,500,3),dtype=np.uint8)
Your algorithm is actually in your question.您的算法实际上在您的问题中。 By 500x500x3 I guess that you mean your image is 500 (width) on 500 (height) with 3 color channel?
500x500x3 我猜你的意思是你的图像是 500(宽度)在 500(高度)上具有 3 个颜色通道?
It could be implemented as follows, without any optimizations:它可以如下实现,无需任何优化:
color = (128, 50, 30)
x, y = 0, 0
for (skip, count) in [Mark[n:n+2] for n in range(len(Mark) // 2)]:
x += skip
y += x // 500 # keep track of the lines, when x > 500,
# it means we are on a new line
x %= 500 # keep the x in bounds
# colorize `count` pixels in the image
for i in range(0, count):
Img[x, y, 0] = color[0]
Img[x, y, 1] = color[1]
Img[x, y, 2] = color[2]
x += 1
y += x // 500
x %= 500 # keep the x in bounds
The zip([a for i, a in enumerate(Mark) if i % 2 == 0], [a for i, a in enumerate(Mark) if i % 2 != 0])
is a just a way to group the pairs (skip, pixel to colorize). zip([a for i, a in enumerate(Mark) if i % 2 == 0], [a for i, a in enumerate(Mark) if i % 2 != 0])
只是一种分组方式对(跳过,像素着色)。 It could definitely be improved though, I'm no Python expert.不过,它肯定可以改进,我不是 Python 专家。
EDIT: modified the zip() to use [Mark[n:n+2] for n in range(len(Mark) // 2)]
as suggested by Peter, much simpler and easier to understand.编辑:修改 zip() 以使用 Peter 建议的
[Mark[n:n+2] for n in range(len(Mark) // 2)]
,更简单,更容易理解。
The easiest way is probably to convert the image to a Numpy array:最简单的方法可能是将图像转换为 Numpy 数组:
import numpy as np
na = np.array(Img)
And then use Numpy ravel() to give you a flattened (1-D) view of the array然后使用 Numpy ravel()为您提供阵列的扁平(1-D)视图
flat = np.ravel(na)
You can now see the shape of your flat view:您现在可以看到平面视图的形状:
print(flat.shape)
Then you can do your colouring by iterating over your array of offsets from your question.然后,您可以通过从您的问题中迭代您的偏移数组来进行着色。 Then the good news is that, because
ravel()
gives you a view
into your original data all the changes you make to the view will be reflected in your original data.那么好消息是,因为
ravel()
为您提供了原始数据的view
,您对视图所做的所有更改都将反映在您的原始数据中。
So, to get back to a PIL Image
, all you need is:因此,要回到
PIL Image
,您只需要:
RecolouredImg = Image.fromarray(na)
Try it out by just colouring the first ten pixels before worrying about your long list.在担心你的长列表之前,只需为前十个像素着色即可尝试一下。
If you like working with Python lists (I don't), you can achieve a similar effect by using PIL getdata()
to get a flattened list of pixels and then process the list against your requirements and putdata()
to put them all back.如果您喜欢使用 Python 列表(我不喜欢),您可以通过使用PIL
getdata()
获取扁平的像素列表,然后根据您的要求处理列表并putdata()
将它们全部放回,从而获得类似的效果. The effect will be the same, just choose a method that fits how your brain works.效果将是相同的,只需选择适合您大脑工作方式的方法即可。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.