Python Pip Packages

1/1/1970

Python Pip Packages

.env Environment

Installation

pip install python-dotenv

Create .env File

API_KEY=your_api_key
DB_USER=my_user

Import

import os
from dotenv import load_dotenv

Load .env in Python

# Load Environment Variables from .env File
load_dotenv() ⭐
 
# Access Variables
print(os.getenv('API_KEY')) ⭐

CORS (Cross-Origin Resource Sharing)

CORS allows web browsers to securely request resources from a different origin (domain, protocol, or port) than the one that served the web page.

Flask with CORS

# Installation
pip install flask-cors
# Import
from flask import Flask
from flask_cors import CORS
app = Flask(__name__)
 
CORS(app) # Allow All Origins
# or
CORS(app, resources={r"/api/*": {"origins": "http://example.com"}}, methods=["GET", "POST"]) # Allow Specific Origins & Methods
 
@app.route('/api/data')
def data():
    return {"message": "CORS enabled!"}
 
if __name__ == '__main__':
    app.run()

FastAPI with CORS

# Installation
pip install fastapi uvicorn
# Import
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
app = FastAPI()
 
app.add_middleware( # Allow All Origins
    CORSMiddleware,
    allow_origins=["*"],  # Allow all
    allow_methods=["*"],  # Allow all HTTP methods
    allow_headers=["*"],  # Allow all headers
)
 
@app.get("/api/data")
async def read_data():
    return {"message": "CORS enabled!"}
 
# Run: uvicorn main:app --reload

Django with CORS

# Installation
pip install django-cors-headers
Skipped X

Common CORS Parameters


Pillow (PIL Fork)

Python Imaging Library for image manipulation.

Installation

pip install Pillow

Import

from PIL import Image, ImageFilter, ImageEnhance

Basic Operations

# Open an Image
img = Image.open("image.jpg") ⭐
 
# Save Image
img.save("output.png") ⭐
 
# Resize Image
img_resized = img.resize((300, 300))
 
# Rotate Image
img_rotated = img.rotate(45)
 
# Convert to Grayscale
img_gray = img.convert("L")
 
# Apply Blur Filter
img_blur = img.filter(ImageFilter.BLUR)
 
# Enhance Brightness
enhancer = ImageEnhance.Brightness(img)
img_bright = enhancer.enhance(1.5)
 
# Show Image
img.show() ⭐

cv2 (OpenCV)

Computer Vision library for image/video processing.

Installation

pip install opencv-python

Import

import cv2

Basic Operations

# Read Image
img = cv2.imread('image.jpg') ⭐
 
# Display Image
cv2.imshow('Image', img) ⭐
cv2.waitKey(0)  # Wait for key press
cv2.destroyAllWindows()
 
# Resize Image
img_resized = cv2.resize(img, (300, 300))
 
# Convert to Grayscale
img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) ⭐
 
# Save Image
cv2.imwrite('output.jpg', img_gray) ⭐
 
# Draw a Rectangle
cv2.rectangle(img, (50, 50), (200, 200), (0, 255, 0), 2)
 
# Apply Gaussian Blur
img_blur = cv2.GaussianBlur(img, (5, 5), 0)
 
# Capture from Webcam
cap = cv2.VideoCapture(0)
ret, frame = cap.read()
cv2.imshow("Webcam", frame)
cap.release()
cv2.destroyAllWindows()

🟡 pytesseract (OCR Tool)

Python wrapper for Google’s Tesseract-OCR Engine.

Installation

pip install pytesseract

Ensure Tesseract is installed:

Import

import pytesseract
from PIL import Image

Basic OCR Operations

# Path to Tesseract (Windows Only)
pytesseract.pytesseract.tesseract_cmd = r"C:\Program Files\Tesseract-OCR\tesseract.exe"
 
# OCR on an Image
img = Image.open("image.png")
text = pytesseract.image_to_string(img) ⭐
print(text)
 
# Specify Language (e.g., English + Hindi)
text = pytesseract.image_to_string(img, lang="eng+hin")
 
# Extract Data with Bounding Boxes
data = pytesseract.image_to_data(img) ⭐
print(data)
 
# Extract Digits Only
digits = pytesseract.image_to_string(img, config="--psm 6 digits")
print(digits)