Introducing Unsplash Image Fetcher: A Practical Approach to Image Processing

makaminski1337

Michael Kaminski

Posted on January 14, 2024

Introducing Unsplash Image Fetcher: A Practical Approach to Image Processing

Overview

Welcome to the first iteration of the Unsplash Image Fetcher, a Python script tailored for developers, designers, and content creators. This script efficiently sources and processes images based on your input theme, simplifying a task that often consumes valuable time.

Version 1.0 Features

Theme-Based Image Fetching: Select a theme and the script fetches corresponding images from Unsplash.
Resizing: Automatically resizes images to 64x64 pixels.
Aspect Ratio Selection: Option to fetch images with equal aspect ratios.
Grayscale and Contrast Enhancement: Converts images into high-contrast grayscale versions.
Organized Saving: Saves original and transformed images in separate directories for easy access.

Code Snippet: Fetching and Resizing Images

def fetch_images_from_unsplash(theme, config):
    """
    Fetches images from Unsplash based on the provided theme and configuration.
    If 'match_aspect_ratio' in the configuration is True, only fetches images that have an equal aspect ratio.
    Resizes the fetched images to 64x64 pixels and returns them as a list of PIL Image objects.
    """
    print(f"Fetching images for theme: {theme}")
    response = requests.get(f'https://api.unsplash.com/search/photos?query={theme}&client_id={UNSPLASH_ACCESS_KEY}')

    if response.status_code == 200:
        data = response.json()
        images = []
        for i, result in enumerate(data['results']):
            img_url = result['urls']['small']
            img_response = requests.get(img_url)
            img = Image.open(BytesIO(img_response.content))

            # Check if the image has an equal aspect ratio
            if not config['match_aspect_ratio'] or img.width == img.height:
                # Resize the image to 64x64 pixels
                img = img.resize((64, 64))
                images.append(img)
                print(f"Image {i+1} fetched and resized")
            else:
                print(f"Image {i+1} skipped due to unequal aspect ratio")

        print(f"Finished fetching images for theme: {theme}")
        print(f"Number of images retrieved: {len(images)}")
        if len(images) == 0:
            print(colored('No images were retrieved.', 'red'))
        print('-'*50)
        return images
    else:
        print(f"Failed to get images for theme '{theme}' from Unsplash API. Status code: {response.status_code}")
        return []
Enter fullscreen mode Exit fullscreen mode

Current Limitations and Upcoming Version 2.0

While effective, version 1.0 has its limits. It's a standalone script without a user interface, making it more suitable for those comfortable with command-line tools. Recognizing this, version 2.0 aims to include:

React App Integration: Launching a user-friendly front-end.
Enhanced Customization: More configuration options for image processing.
User Feedback Integration: Implementing features based on user suggestions.

Ideation for Version 2.0 Features

Batch Processing: Allow multiple themes in one go for bulk image handling.
Image Filtering Options: Introduce filters like brightness, contrast, and saturation adjustments.
Download Quality Selection: Choose the resolution for downloaded images.
Interactive UI: A simple and intuitive interface for non-programmers.
Social Media Integration: Directly upload edited images to social platforms.

Code Example: Grayscale Conversion

def convert_to_grayscale_and_contrast(image):
    """
    Converts the provided image to grayscale and applies high contrast.
    Returns the converted image.
    """
    grayscale_image = ImageOps.grayscale(image)
    high_contrast_image = ImageOps.autocontrast(grayscale_image)
    return high_contrast_image
Enter fullscreen mode Exit fullscreen mode

Repository and Collaboration

The script is available on GitHub, inviting collaboration and improvements. You're encouraged to contribute ideas, report bugs, or suggest enhancements. Check out the repo here.

Conclusion

Version 1.0 of the Unsplash Image Fetcher marks the beginning of a journey towards more streamlined image processing. It's a functional tool, but with your feedback and contributions, version 2.

๐Ÿ’– ๐Ÿ’ช ๐Ÿ™… ๐Ÿšฉ
makaminski1337
Michael Kaminski

Posted on January 14, 2024

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related

ยฉ TheLazy.dev

About