You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用列表推导式正确提取图像数据?JPEG分割异常求助

Hey there! Let's work through why all your extracted sub-images are showing up as the number 9. This is a common slicing issue, so let's break it down step by step.

Problem Diagnosis & Fixes

1. The Most Likely Culprit: Incorrect Slicing Step Size

Your list comprehension uses range(10), which generates integers from 0 to 9. When you slice image_1[:, k:k + 28] with these values, you're actually extracting overlapping slices of the first 28 columns (e.g., 0-28, 1-29, ..., 9-37) — all of which fall within the first digit (9) of your image. That's why every sub-image looks like 9!

Fix: Adjust the Step in range()

You need to increment k by 28 each time to grab non-overlapping 28-column-wide chunks:

image_parts = [image_1[:, k:k + 28] for k in range(0, 280, 28)]

range(0, 280, 28) produces 0, 28, 56, ..., 252 — exactly the starting positions for each of the 10 28×28 sub-images.

2. Check if Image Dimensions Are Reversed

Sometimes image libraries (like PIL) return images in (width, height) order instead of (height, width). If your image_1 has a shape of (280, 28) instead of (28, 280), your column slicing will actually cut rows instead of columns.

Verify the shape first:

print(image_1.shape)

If it's (280, 28), adjust your slicing to target rows instead:

image_parts = [image_1[k:k + 28, :] for k in range(0, 280, 28)]

3. Confirm Your Image Layout Assumption

Double-check the original image to make sure digits are indeed arranged left-to-right. Open it in a simple viewer (like Paint or Preview) and confirm the width is 280 pixels, with each digit taking up exactly 28 pixels horizontally. If the digits are arranged vertically instead, you'll need to adjust your slicing logic accordingly.

4. Validate the Fix with Your Test Code

After adjusting the slicing, test with your code again to confirm:

import matplotlib.pyplot as plt

test_img = np.array(image_parts[5]).reshape(28, 28)
plt.imshow(test_img, cmap='gray')
plt.show()

This should display the 6th digit (since Python uses 0-indexing) instead of 9.


内容的提问来源于stack exchange,提问作者Morkus

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.08 11:52:30