0% found this document useful (0 votes)
6 views3 pages

Opencv CSV Cheat Sheet

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views3 pages

Opencv CSV Cheat Sheet

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Python OpenCV and CSV Modules Cheat Sheet

OpenCV (cv2):
OpenCV Cheat Sheet (cv2):

1. Import OpenCV:

import cv2

2. Read an image:

img = cv2.imread('image.jpg')

3. Show an image:

cv2.imshow('Window', img)

cv2.waitKey(0)

cv2.destroyAllWindows()

4. Resize image:

resized = cv2.resize(img, (width, height))

5. Convert color (e.g., BGR to Gray):

gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

6. Save an image:

cv2.imwrite('new_image.jpg', img)

7. Capture from webcam:


cap = cv2.VideoCapture(0)

ret, frame = cap.read()

8. Draw a rectangle:

cv2.rectangle(img, (x1, y1), (x2, y2), (255, 0, 0), thickness)

9. Draw text:

cv2.putText(img, 'Text', (x, y), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 255), 2)

10. Release webcam:

cap.release()

cv2.destroyAllWindows()

CSV Module:
CSV Module Cheat Sheet:

1. Import CSV module:

import csv

2. Write to a CSV file:

with open('file.csv', 'w', newline='') as f:

writer = csv.writer(f)

writer.writerow(['col1', 'col2'])

writer.writerow(['val1', 'val2'])

3. Read from a CSV file:

with open('file.csv', 'r') as f:


reader = csv.reader(f)

for row in reader:

print(row)

4. Append to a CSV file:

with open('file.csv', 'a', newline='') as f:

writer = csv.writer(f)

writer.writerow(['val3', 'val4'])

5. Using DictWriter (write dictionaries):

with open('file.csv', 'w', newline='') as f:

fieldnames = ['col1', 'col2']

writer = csv.DictWriter(f, fieldnames=fieldnames)

writer.writeheader()

writer.writerow({'col1': 'val1', 'col2': 'val2'})

6. Using DictReader (read dictionaries):

with open('file.csv', 'r') as f:

reader = csv.DictReader(f)

for row in reader:

print(row['col1'], row['col2'])

You might also like