ValueError:无法将大小为27648000的数组重塑为形状(24001280,3)

2024-05-23 15:56:08 发布

您现在位置:Python中文网/ 问答频道 /正文

在这里,我有一个小项目,我封锁了几个星期

我有一个显示器是3840x2400单色像素。然而,它像1280(RGB)x2400一样被驱动,而每个RGB子像素映射到一个单色像素

因此,为了显示真实的3840x2400,必须将单色图像的3个连续像素映射到一个伪RGB像素。这将生成一个1280x2400宽的图像,其中每个RGB子像素对应一个真正的单色像素

我尝试在python3.9中使用numpy和PIL来实现这一点

代码如下:

from PIL import Image
import numpy as np

def TransTo1224(SourcePngFileName, DestPngFileName):
  #trans png file from 3840x2400 to 1280X2400(RGB)
  print('~~~~~~~')
  print(SourcePngFileName)
  imgSrc = Image.open(SourcePngFileName)
  dataSrc = np.array(imgSrc)
  dataDest = dataSrc.reshape(2400,1280,3)
  imgDest = Image.fromarray(dataDest, 'RGB')
  imgDest.save(DestPngFileName)


TransTo1224("./source/test1.png","./output/test1.png")

我有一个错误:

dataDest = dataSrc.reshape(2400,1280,3)
ValueError: cannot reshape array of size 27648000 into shape (2400,1280,3)

我不明白我的错误,如果有人能帮助我,提前谢谢你


Tags: from图像imageimportnumpypilpngnp
2条回答

好的,我解决了我的问题,它确实来自我的输入图像,代码适用于一些图像,但不是我想要重新映射的图像。此外,我不明白3的倍数是从哪里来的 3840x2400x3=27648000

我的问题来自RGB图像的模式

这对我来说已经足够了,在我整形之前,我把这个模式转换成“L”,亮度

from PIL import Image
import numpy as np

def TransTo1224(SourcePngFileName, DestPngFileName):
  #trans png file from 3840x2400 to 1280X2400(RGB)
  print('~~~~~~~')
  print(SourcePngFileName)
  imgSrc = Image.open(SourcePngFileName)
  imgSrc = imgSrc.convert('L') # <  -
  dataSrc = np.array(imgSrc)
  dataDest = dataSrc.reshape(2400,1280,3)
  imgDest = Image.fromarray(dataDest, 'RGB')
  imgDest.save(DestPngFileName)

TransTo1224("./source/test1.png","./output/test1.png")

谢谢大家帮助我

试试这个

dataDest = vv.reshape(2400,1280,3,-1)

dataDest = vv.reshape(2400,1280,3,3)

使用dataDest=dataSrc.reformate(24001280,3)它不会工作

相关问题 更多 >