【发布时间】:2018-01-22 11:33:10
【问题描述】:
我正在尝试调整图像的大小以在我的 OLED 设备上显示。
基于How to resize image?,我在脚本中添加了new_img = img.resize((128,128)) 以调整图像大小。然而,图像的相同部分出现在屏幕上。当我尝试为 resize 参数 (64x64) 输入较小的图像尺寸时,终端打印:
pi@raspberrypi:~/project $ python colors.py --display ssd1351 --width 128 --height 128 --interface spi --spi-bus-speed 16000000 --gpio-data-command 20
Version: luma.oled 2.3.1 (luma.core 1.3.0)
Display: ssd1351
Interface: spi
Dimensions: 128 x 128
------------------------------------------------------------
Traceback (most recent call last):
File "colors.py", line 87, in <module>
main()
File "colors.py", line 30, in main
device.display(new_img)
File "/usr/local/lib/python2.7/dist-packages/luma/oled/device.py", line 371, in display
assert(image.size == self.size)
AssertionError
关于如何为 OLED 显示器正确调整图像大小的任何想法?
我的完整脚本是:
#!/usr/bin/env python
import math
import time
import random
import os.path
from demo_opts import get_device
from luma.core.render import canvas
from PIL import Image
def main():
img_path = os.path.abspath(os.path.join(
os.path.dirname(__file__), 'plot.jpg'))
img = Image.open(img_path) \
.transform(device.size, Image.AFFINE, (1, 0, 0, 0, 1, 0), Image.BILINEAR) \
.convert(device.mode)
new_img = img.resize((128,128))
while True:
# Image display
device.display(new_img)
time.sleep(5)
if __name__ == "__main__":
try:
device = get_device()
main()
except KeyboardInterrupt:
pass
【问题讨论】:
-
我看到图片和图表,我赞成...
-
您在底部使用
device = get_device(),然后尝试在main()中使用它。为什么那条线不是主要的呢?尽可能避免使用全局变量。 -
@Bailey:我根据 OLED Luma 库 repo 上的演示脚本修改了脚本。我认为将其放在底部的原因是因为原始脚本中还有其他功能,我现在已将其删除。感谢您指出这一点,我稍后可能会解决这部分问题。
-
您的设备显示为 128x128,因此将其调整为 500x500 可能不正确...
-
@马克。感谢您指出了这一点。我现在将其调整为 128x128。我不再收到错误,但只显示原始图像的相同部分。我试图降低,但断言错误返回。
标签: python image resize image-resizing