Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

image stretching when I generate a qr code #6

Closed
car-lisle opened this issue Jun 10, 2023 · 5 comments
Closed

image stretching when I generate a qr code #6

car-lisle opened this issue Jun 10, 2023 · 5 comments

Comments

@car-lisle
Copy link

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
Copy link
Owner

Zeroji commented Jun 10, 2023

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
Copy link
Author

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
Copy link
Author

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
Copy link
Owner

Zeroji commented Jun 10, 2023

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
Copy link
Author

Thank you so much, that fixed it! :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants