Python Forum
Displaying image on PC using Arduino and Python
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Displaying image on PC using Arduino and Python
#1
First of all, I am new to python.
I have arduino connected to my PC. I have a few button interfaced with my arduino. When a button is pressed I want an image to be displayed on my windows PC. Here's my arduino code. At the moment I am only testing button on pin 9, as you can see in code.

#define data0 5
#define data1 6
#define data2 7
#define data3 8
#define data4 9

int data[5];



void setup() {
// put your setup code here, to run once:
pinMode(data0, INPUT);
pinMode(data1, INPUT);
pinMode(data2, INPUT);
pinMode(data3, INPUT);
pinMode(data4, INPUT);
Serial.begin(9600);
}

void loop() {
// put your main code here, to run repeatedly:

data[0] = digitalRead(data0);
data[1] = digitalRead(data1);
data[2] = digitalRead(data2);
data[3] = digitalRead(data3);
data[4] = digitalRead(data4);
if(data[4]==1){
Serial.println(1);
}
delay(100);
}

And here's my python code
import serial
import time

ser = serial.Serial('COM6', baudrate = 9600, timeout =1)

def showPIL(pilImage):
    root = tkinter.Tk()
    w, h = root.winfo_screenwidth(), root.winfo_screenheight()
    root.overrideredirect(1)
    root.geometry("%dx%d+0+0" % (w, h))
    root.focus_set()    
    root.bind("<Escape>", lambda e: (e.widget.withdraw(), e.widget.quit()))
    canvas = tkinter.Canvas(root,width=w,height=h)
    canvas.pack()
    canvas.configure(background='black')
    imgWidth, imgHeight = pilImage.size
    if imgWidth > w or imgHeight > h:
        ratio = min(w/imgWidth, h/imgHeight)
        imgWidth = int(imgWidth*ratio)
        imgHeight = int(imgHeight*ratio)
        pilImage = pilImage.resize((imgWidth,imgHeight), Image.ANTIALIAS)
    image = ImageTk.PhotoImage(pilImage)
    imagesprite = canvas.create_image(w/2,h/2,image=image)
    root.mainloop()


while 1:
    arduinoData = ser.readline().decode('ascii')
    if arduinoData == 49:
        pilImage = Image.open("sample.jpg")
        showPIL(pilImage)
Above python function to display the image works, I have tested it. The code to receive the arduino data also works fine, because when I do
print(arduinoData)
I can see the data. But when I run the above code and press the button, nothing happens.
Reply
#2
Is arduinoData equal to 49?
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Python error on mentioned Arduino port name dghosal 5 848 Aug-22-2023, 04:54 PM
Last Post: deanhystad
  Trying to Get Arduino sensor data over to excel using Python. eh5713 1 1,702 Dec-01-2022, 01:52 PM
Last Post: deanhystad
  [Solved]Help Displaying Emails properly via Python Extra 5 1,170 Sep-28-2022, 09:28 PM
Last Post: deanhystad
  Upload image to Instagram using python/selenium using image URL failed, need help greenpine 5 5,450 Feb-22-2022, 09:42 PM
Last Post: snippsat
  PIL Image / python-resize-image AttributeError sallyjc81 1 4,989 Aug-02-2020, 12:06 AM
Last Post: scidam
  Can't transmit serial fast Python to Arduino pyserial mRKlean 0 2,355 Mar-29-2020, 08:12 PM
Last Post: mRKlean
  About Arduino and Python usb webcam tuts Simurg 1 2,154 Mar-20-2020, 07:25 PM
Last Post: Larz60+
  Help Graphing Arduino Data Real Time in Python nschulz 0 2,532 Mar-12-2020, 06:15 PM
Last Post: nschulz
  Problems displaying data sent from Arduino to Pi VostokRising 2 2,316 May-18-2019, 08:28 PM
Last Post: VostokRising
  Arduino / Python Environment Setup - PIP stevealbright 0 2,250 Feb-24-2019, 10:48 PM
Last Post: stevealbright

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020