Zeroji / st7789v

Python module to control ST7789V-based displays
GNU General Public License v3.0
17 stars 6 forks source link

image stretching when I generate a qr code #6

Closed car-lisle closed 1 year ago

car-lisle commented 1 year ago

Using the examples from this library, I have been trying to generate qr codes to display on this tft. However, the image is stretched and doesn't end up working. However, when I pass images I donwload online, they work just fine.

Here's the test code I used: st7789v-qr-gen-test.txt

The result: IMG_20230609_202613

Zeroji commented 1 year ago

Hi, I tested your code and got the same result. The issue you're getting is because you're sending 198x198 bytes of data (see image.size) into a 240x320 screen, so it's only filling 51% of the screen, and everything is misaligned.

A simple fix is to call display.set_bounds before drawing:

image = qr.make_image(fill_color="red", back_color="white")
data = list(image.get_image().getdata())

with RaspberryPi() as rpi:
    display = Display(rpi)
    display.initialize(rotation=90)  # this is needed for your display, I think
    display.set_bounds(0, 0, image.size[0], image.size[1])  # restrict the draw region to the image size
    display.draw_rgb_bytes(data)

This will correctly draw the QR code in the top-left corner. If you need to draw on the entire screen again, call display.set_bounds(0, 0, display.base_width, display.base_height).

I also tried this:

display.set_bounds((display.base_width - image.size[0]) // 2,
                   (display.base_height - image.size[1]) // 2,
                   (display.base_width + img.size[0]) // 2,
                   (display.base_height + img.size[1]) // 2)

Which centers the QR code quite nicely ;) image

car-lisle commented 1 year ago

THANK YOU SO MUCH!!! I really appreciate your fast response! I have been trying to get this to work for weeks. I am so new to this sort of thing and I tried really hard to search online, but I was getting no results. Thank you once again!!! :)

car-lisle commented 1 year ago

I played around a little bit with the centering code you provided and it's a tad off-center. I had to reduce the qr code image from 198x198 to 145x145 to get it to display to the screen. I'm not really sure what to alter as everything looks fine (you did guess the rotation correctly! :D) IMG_20230610_032416

Zeroji commented 1 year ago

Ah, an interesting issue - there's a logic bug when using base_width with a rotated display, I think width and height are swapped!

Try the centering code with the original 198x198 size, but using base_width instead of base_height and vice-versa. I'll have to fix this one day :)

car-lisle commented 1 year ago

Thank you so much, that fixed it! :)