Learn Python Coding
38.7K subscribers
1.06K photos
37 videos
24 files
853 links
Learn Python through simple, practical examples and real coding ideas. Clear explanations, useful snippets, and hands-on learning for anyone starting or improving their programming skills.

Admin: @HusseinSheikho || @Hussein_Sheikho
Download Telegram
Topic: Python OpenCV – Part 1: Introduction, Image Reading, and Basic Operations

---

What is OpenCV?

OpenCV (Open Source Computer Vision Library) is a powerful computer vision and image processing library.

• It supports image and video capture, analysis, object detection, face recognition, and much more.

• Commonly used with Python, C++, and machine learning pipelines.

---

Installing OpenCV

pip install opencv-python


---

1. Reading and Displaying Images

import cv2

# Read the image
image = cv2.imread('image.jpg')

# Display the image in a window
cv2.imshow('My Image', image)
cv2.waitKey(0) # Wait for any key to close the window
cv2.destroyAllWindows()


---

2. Image Shape and Type

print(image.shape)  # (height, width, channels)
print(image.dtype) # uint8 (8-bit integers for each channel)


---

3. Converting Color Spaces

# Convert BGR to Grayscale
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

# Convert BGR to RGB (for matplotlib or image correction)
rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)


---

4. Saving an Image

cv2.imwrite('gray_image.png', gray)


---

5. Drawing Shapes

# Draw a red rectangle
cv2.rectangle(image, (50, 50), (200, 200), (0, 0, 255), 2)

# Draw a filled circle
cv2.circle(image, (150, 150), 40, (255, 0, 0), -1)

# Draw text
cv2.putText(image, 'OpenCV!', (40, 300), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2)

cv2.imshow('Drawn Image', image)
cv2.waitKey(0)
cv2.destroyAllWindows()


---

6. Resize and Flip

# Resize image
resized = cv2.resize(image, (300, 300))

# Flip image horizontally
flipped = cv2.flip(image, 1)


---

Summary

• OpenCV allows you to read, display, modify, and save images easily.

• You can perform basic tasks like drawing, resizing, flipping, and color transformations.

• These operations are the building blocks for image analysis, preprocessing, and machine vision applications.

---

Exercise

• Write a program that:

1. Loads an image.
2. Converts it to grayscale.
3. Draws a blue circle in the center.
4. Saves the new image to disk.

---

#Python #OpenCV #ImageProcessing #ComputerVision #Beginners

https://t.me/DataScience4
2