调整OLED显示器图像尺寸的难度

时间:2018-01-22 11:33:11

标签: python image resize image-resizing

我正在尝试调整图像大小以在我的OLED设备上显示。

我要显示的图像是: enter image description here

目前,我的OLED显示屏仅显示图像的一小部分: enter image description here

根据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

1 个答案:

答案 0 :(得分:1)

好的,我通过以下方式解决了这个问题:

img = Image.open(img_path)  
img = img.resize((128, 128), Image.ANTIALIAS) \
    .transform(device.size, Image.AFFINE, (1, 0, 0, 0, 1, 0), Image.BILINEAR) \
    .convert(device.mode)

在运行转换和转换之前,首先调整图像大小。