回答此问题可获得 20 贡献值,回答如果被采纳可获得 50 分。
<p>我需要一种快速的方法来创建表示图像像素坐标的元组列表<em>(<strong>X</strong>,<strong>Y</strong>)</em></p>
<p>其中,<strong>X</strong>是从<code>0</code>到<code>size</code>,<strong>Y</strong>是从<code>0</code>到<code>size</code></p>
<p>步长值<code>1</code>会导致<strong>X</strong>和<strong>Y</strong>值(0,1,2,3…)过多的元组。使用大于<code>1</code>的步长值将减少处理时间。例如,如果步长值为<code>2</code>,则值将为(0、2、4、6…)。如果步长值为<code>4</code>,则值为(0、4、8、12…)</p>
<p>在纯python中,可以使用<code>range</code>命令。但是,默认情况下,<code>NumPy</code>安装在我的Linux发行版中。在NumPy中,可能会使用<code>arrange</code>命令,但我很难理解NumPy数组语法</p>
<p>PS:创建元组列表后,它将被随机洗牌,然后在循环中读取</p>
<hr/>
<h2>编辑1</h2>
<p>使用下面的<a href="https://stackoverflow.com/a/65971039/6929343">this answer</a>:</p>
<p><a href="https://i.stack.imgur.com/0X7vo.gif" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/0X7vo.gif" alt="fade in answer 1.gif"/></a></p>
<p>它不是在图像中淡入淡出,而是从左到右进行某种奇怪的擦拭。使用答案中的代码,稍作修改:</p>
<pre><code> step = 4
size = self.play_rotated_art.size[0] - step
self.xy_list = [
(x, y)
for x in range(0, size - step, step)
for y in range(0, size - step, step)
]
</code></pre>
<h3>错误更新</h3>
<p>我的代码中有一个错误,现在工作正常:</p>
<p><a href="https://i.stack.imgur.com/rNy8j.gif" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/rNy8j.gif" alt="fade in answer 1 fix.gif"/></a></p>
<p>更新后的代码为:</p>
<pre><code> self.step = 4
size = self.play_rotated_art.size[0] - self.step
self.xy_list = [
(x, y)
for x in range(0, size - self.step, self.step)
for y in range(0, size - self.step, self.step)
]
shuffle(self.xy_list)
# Convert numpy array into python list & calculate chunk size
self.current_chunk = 0
self.chunk_size = int(len(self.xy_list) / 100)
# Where we stop copying pixels for current 1% chunck
end = self.current_chunk + self.chunk_size
if end > len(self.xy_list) - 1:
end = len(self.xy_list) - 1
while self.current_chunk < end:
x0, y0 = self.xy_list[self.current_chunk]
x1 = x0 + self.step
y1 = y0 + self.step
box = (x0, y0, x1, y1)
region = self.play_rotated_art.crop(box)
self.fade.paste(region, box)
self.current_chunk += 1
self.play_artfade_count += 1
return self.fade
</code></pre>
<hr/>
<h2>TL;博士</h2>
<p>我已经有了步骤值为<code>1</code>的代码,但是这段代码过于复杂,请求修改效率低下。上述一般性问题如果得到回答,将对其他人有更大帮助,对我也有帮助</p>
<h2>步骤值为<code>1</code>的现有代码:</h2>
<pre><code>def play_artfade2(self):
''' PILLOW VERSION:
Fade in artwork in 100 chunks leaving loop after chunk and
reentering after Tkinter updates screen and pauses.
'''
if self.play_artfade_count == 100:
# We'have completed a full cycle. Force graphical effects exit
self.play_artfade_count = 0 # Reset art fade count
self.play_rotated_value = -361 # Force Spin Art
return None
# Initialize numpy arrays first time through
if self.play_artfade_count == 0:
# Create black image to fade into
self.fade = Image.new('RGBA', self.play_rotated_art.size, \
color='black')
# Generate a randomly shuffled array of the coordinates
im = np.array(self.play_rotated_art)
X,Y = np.where(im[...,0]>=0)
coords = np.column_stack((X,Y))
np.random.shuffle(coords)
# Convert numpy array into python list & calculate chunk size
self.xy_list = list(coords)
self.current_chunk = 0
self.chunk_size = int(len(self.xy_list) / 100)
# Where we stop copying pixels for current 1% chunck
end = self.current_chunk + self.chunk_size
if end > len(self.xy_list) - 1:
end = len(self.xy_list) - 1
while self.current_chunk < end:
x0, y0 = self.xy_list[self.current_chunk]
x1 = x0 + 1
y1 = y0 + 1
box = (x0, y0, x1, y1)
region = self.play_rotated_art.crop(box)
self.fade.paste(region, box)
self.current_chunk += 1
self.play_artfade_count += 1
return self.fade
</code></pre>
<p>使用Pillow的<code>Image.crop()</code>和<code>Image.paste()</code>对于单个像素来说是过分的,但是最初的<em><strong>工作</strong></em>设计在图像从200x200调整到333x333到512x512等时,未来的重点是利用“超级像素”,框大小为2x2、3x3、5x5等</p>