0
0
Computer Visionml~5 mins

Style transfer concept in Computer Vision

Choose your learning style9 modes available
Introduction

Style transfer lets you take the look of one image and apply it to another. It helps create new pictures that mix content and style in a fun way.

You want to make a photo look like a famous painting.
You want to create artistic filters for your photos or videos.
You want to explore creative image editing without drawing skills.
You want to learn how computers understand and mix images.
You want to build apps that transform images in unique styles.
Syntax
Computer Vision
content_image = load_image('content.jpg')
style_image = load_image('style.jpg')

stylized_image = style_transfer(content_image, style_image)

save_image(stylized_image, 'output.jpg')

content_image is the picture you want to keep the main shapes of.

style_image is the picture with the colors and brush strokes you want to copy.

Examples
Basic call to create a new image that combines content and style.
Computer Vision
stylized = style_transfer(content, style)
Run the style transfer for more steps to get a clearer style effect.
Computer Vision
stylized = style_transfer(content, style, iterations=500)
Adjust how much the output looks like the content or the style.
Computer Vision
stylized = style_transfer(content, style, content_weight=1.0, style_weight=100.0)
Sample Model

This program loads two images, uses a ready-made style transfer model, and saves the new stylized image.

Computer Vision
import tensorflow as tf
import tensorflow_hub as hub
import numpy as np
import PIL.Image

def load_image(path, max_dim=512):
    img = PIL.Image.open(path)
    img = img.convert('RGB')
    size = max(img.size)
    scale = max_dim / size
    new_size = tuple([int(dim * scale) for dim in img.size])
    img = img.resize(new_size, PIL.Image.LANCZOS)
    img = np.array(img)/255.0
    img = img[tf.newaxis, ...]
    return img

def tensor_to_image(tensor):
    tensor = tensor*255
    tensor = np.array(tensor.numpy(), dtype=np.uint8)
    if np.ndim(tensor)>3:
        assert tensor.shape[0] == 1
        tensor = tensor[0]
    return PIL.Image.fromarray(tensor)

# Load content and style images
content_image = load_image('content.jpg')
style_image = load_image('style.jpg')

# Load pre-trained style transfer model from TensorFlow Hub
hub_model = hub.load('https://tfhub.dev/google/magenta/arbitrary-image-stylization-v1-256/2')

# Apply style transfer
stylized_image = hub_model(tf.constant(content_image), tf.constant(style_image))[0]

# Convert tensor to image and save
output_image = tensor_to_image(stylized_image)
output_image.save('stylized_output.jpg')

print('Style transfer completed and image saved as stylized_output.jpg')
OutputSuccess
Important Notes

Style transfer models usually need both content and style images to work.

Running style transfer can take time depending on your computer power.

You can adjust style and content weights to get different artistic effects.

Summary

Style transfer mixes the look of one image with the shapes of another.

It is useful for creative image editing and artistic effects.

Pre-trained models make it easy to try style transfer without deep knowledge.