Objective
Use AIDB's DIMENSIONS function to query and filter images based on their width, height, and aspect ratio. This enables finding images that meet specific size requirements.
Step 1: Create Image Table
Create a table for storing images with dimension metadata.
CREATE TABLE image_repository (
id INTEGER PRIMARY KEY,
filename VARCHAR(255) NOT NULL,
image IMAGE(JPEG),
category VARCHAR(50),
width INTEGER,
height INTEGER,
aspect_ratio VARCHAR(10),
megapixels DECIMAL(5, 2),
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
Step 2: Insert Sample Images
Add sample images with various dimensions.
INSERT INTO image_repository (id, filename, category, width, height) VALUES
(1, 'landscape_wide.jpg', 'landscape', 3840, 2160),
(2, 'portrait_tall.jpg', 'portrait', 1080, 1920),
(3, 'square_post.jpg', 'social', 1080, 1080),
(4, 'banner_ultra.jpg', 'marketing', 2560, 600),
(5, 'thumbnail.jpg', 'web', 150, 150),
(6, 'hd_photo.jpg', 'photography', 1920, 1080),
(7, 'mobile_screen.jpg', 'mobile', 750, 1334),
(8, 'desktop_bg.jpg', 'desktop', 2560, 1440),
(9, 'social_story.jpg', 'social', 1080, 1920),
(10, '4k_image.jpg', 'photography', 4096, 2160);
Step 3: Calculate Aspect Ratios
Update images with calculated aspect ratios.
UPDATE image_repository
SET aspect_ratio = CASE
WHEN width * 9 = height * 16 THEN '16:9'
WHEN height * 9 = width * 16 THEN '9:16'
WHEN width * 3 = height * 4 THEN '4:3'
WHEN height * 3 = width * 4 THEN '3:4'
WHEN width = height THEN '1:1'
WHEN width * 10 = height * 16 THEN '16:10'
ELSE 'custom'
END,
megapixels = (width * height) / 1000000.0;
SELECT filename, width, height, aspect_ratio, megapixels
FROM image_repository
ORDER BY megapixels DESC;
Step 4: Extract Dimensions from Images
Use DIMENSIONS function on actual image data.
SELECT
id,
filename,
DIMENSIONS(image) as image_dimensions
FROM image_repository
WHERE image IS NOT NULL
LIMIT 5;
Step 5: Find Images by Minimum Size
Query images meeting minimum dimension requirements.
-- Find images at least 1920x1080 (Full HD)
SELECT
filename,
width,
height,
category
FROM image_repository
WHERE width >= 1920 AND height >= 1080
ORDER BY width DESC, height DESC;
Step 6: Find Portrait vs Landscape
Separate images by orientation.
-- Portrait images
SELECT filename, width, height
FROM image_repository
WHERE height > width
ORDER BY height DESC;
-- Landscape images
SELECT filename, width, height
FROM image_repository
WHERE width > height
ORDER BY width DESC;
-- Square images
SELECT filename, width, height
FROM image_repository
WHERE width = height;
Step 7: Images by Aspect Ratio
Group and count images by aspect ratio.
SELECT
aspect_ratio,
COUNT(*) as image_count,
AVG(megapixels) as avg_megapixels
FROM image_repository
GROUP BY aspect_ratio
ORDER BY image_count DESC;