Transforming Images Using AI Without Leaving Your React App

Rate this content
Bookmark
Transforming images with AI in a React app is a powerful way to optimize and modify images automatically. Using AI image transformation, developers can remove backgrounds, crop images, and upscale pixelated images. Image CDN simplifies this process by handling optimizations and transformations. Integrating AI-powered image transformation in React apps is straightforward with tools like Unpick Image. Image optimization is crucial for web applications to enhance loading times and improve bandwidth usage. Implementing best practices like lazy loading and maintaining aspect ratios ensures better user experience. Utilizing image transformation on the edge offers faster processing and easy integration.

From Author:

Image optimization libraries abstract away the hard parts of working with images so that you can focus on shipping. But these tools don’t let you do complex transformations to your existing images. Adding text, removing distracting backgrounds, or upscaling images to higher resolution all require you to download, transform, and re-upload your images. This talk will explore using AI image APIs and unpic-image to handle complex image transformations inside our React applications. We’ll remove and replace image backgrounds and use context-aware facial cropping to create responsive layouts.

This talk has been presented at React Summit US 2023, check out the latest edition of this React Conference.

Watch video on a separate page

FAQ

Image optimization is crucial in web applications as it enhances loading times and improves bandwidth usage, which in turn affects user experience and satisfaction. Optimizing images ensures that your application delivers content efficiently without compromising on quality.

Best practices for handling images in web development include using source sizes, maintaining aspect ratios, preventing layout shifts, and implementing lazy loading. These practices help in improving page load times and overall user experience.

Image transformation can be integrated into React applications by using AI-powered image CDNs like Unpick Image, which automate the process of applying transformations such as background removal, facial recognition cropping, and upscaling directly in the CDN.

Using image transformation on the edge offers faster processing times, easy integration, and up-to-date transformations. It allows developers to deploy new functions quickly without worrying about extensive maintenance of tooling.

Using an image CDN simplifies image management by taking care of heavy lifting such as cache invalidation and asset syncing. It also provides zero-config integration with features like automatic scaling, quality adjustment, and format selection, making it easier to adopt image best practices.

AI plays a critical role in image transformation by automating complex tasks such as background removal, facial recognition cropping, and image upscaling. These AI-driven transformations help ensure that images are optimized for both quality and performance in web applications.

A practical example involves a Contentful Next JS app using an image CDN with AI capabilities. For instance, to remove a background from an image, simply append the background removal parameter provided by the CDN to the image URL, and the CDN will automatically process and serve the modified image.

Luis H. Ball Jr.
Luis H. Ball Jr.
5 min
13 Nov, 2023

Comments

Sign in or register to post your comment.

Video Transcription

1. Introduction to Image Transformation

Short description:

Today we're talking about transforming images with AI in your React app. It's important not to forget about images as they are the connective tissue to your sites. Adopting image best practices takes time, so we can explore image transformation on the edge as a faster and easier alternative.

Alright, so my name is Luis Paul. I'm a senior software engineer with ImageX. Today we're going to be talking about transforming images with AI without leaving your React app. But I'm going to go fast because there's a lot of content so excuse me, I'll try to slow down.

So first off, a little story time. As developers, we love to ship product. We're laser focused on features, customer experience and satisfaction as we should be. That's our job. But that makes it easy to forget about the parts that we don't code, about things like images. So you might find yourself in this situation where you shipped a viral product, people love it, but they're also DMing you saying, hey, it's fantastic but you're shipping 100 megabytes of images. Let's not do that. And you got to run and fix it. So this is why it's important not to forget about images. They are the connective tissue to your sites. Images are the way the customers experience your product for the first time and they matter more than we tend to realize.

So what should we do about it? We should adopt image best practices. There's a bunch of them, right? You got source sizes, aspect ratio, layout shifts, lazyloading, etc. And ensuring you hit all of them takes time. So it requires you to do things like generate variants for all of your images. How do we make this easier? So we gravitated to doing things like image transformation as a build step. But it's not great. Transforming at build time can take forever. The feature set is more limited in terms of transformations you can achieve and you have to do some careful maintenance of your tooling to do it right long-term. The DX in short just isn't great.

So what else can we do? We can do things like image transformation on the edge. This is fast, easy to integrate into any application. It's always up-to-date. You can spin up a new replicate endpoint pretty easily and it stays up-to-date for you. But it comes with some overhead. You gotta spin up new functions and every time you need a new transformation, and you have to do things like maintaining and invalidating caches, syncing the generated asset back to your bucket where your assets live.

2. Image Transformation with CDN and AI

Short description:

With an image CDN, you can take care of all the heavy lifting and progressively adopt best practices. In our Contentful Next JS app, we remove backgrounds, crop images using AI, and upscale pixelated images. We leverage the image CDN and AI parameters to ensure our images always look their best. That's transforming images using AI without leaving your React app.

And so it can get a little complicated which is why people gravitate to image CDNs. With an image CDN, you can take care of all that heavy lifting. And then you can progressively adopt it using something like unpick image which will take care of hitting all these best practices for you. It's fantastic because it's zero config with most of the big CDNs out there. You just drop your image URL that lives in the CDN. It knows what to do. You just give it a width and a height if you need it. And that's pretty much it.

Long story short, you don't need to reinvent the wheel to achieve best practices. So let's take a look at how you could actually use this in your applications. We're gonna have a Contentful Next JS app, it's gonna use an image optimization library. It'll use an image CDN to remove all that overhead and will leverage the CDN's AI parameters to do some fun transformations.

So, first off, we're going to remove backgrounds using AI. We wanna do some consistency enforcement on our site, make sure that things match our designs. So here we have our Contentful Next JS application. It's got a hero image. We wanna remove the background. All we need to do, using in CDN, is use its background removal parameter, save, and by appending that to the URL, we'll have a removed background. That was quick, easy, painless, thanks to using Unpick Image in the CDN. Let's go ahead and keep going. We got some AI facial cropping we wanna do. We wanna make sure the images are always focused on what's actually highly relevant and centered. So let's take a look at how we can do that. We've got an image here, and we have somebody walking down the hallway, but we can't really see this person's face, and we'd like to, so, why don't we go ahead and crop it around this person's face? The way we do that is we add some parameters to the URL. We'll set Frick crop, we'll add some width and height dimensions. And once we've done that, now we also wanna make sure that we use some facial recognition to automatically center the subject's face in the image. Easy. And now, last thing we wanna do, do some upscaling. This is my personal favorite, it's like a magic trick. You can add some definition to the pixelated images, you're going to increase the pixel density, and you're gonna do some things like, you know, enhance lightning and contrast. So, if we're thinking about our Contentful NxJS application, we can have an image component, that all our images use, but maybe we have a lot of user-generated content, it gets a little hairy, right? Not everybody has the latest and greatest stuff, they have some old content, we wanna upscale it. What we have to do in this case is add the upscale parameter, save that, and so now, with the upscale equals true in our image URL, our image gets automatically upscaled and that carousel looks great. So just recapping, what did we do? We talked a lot about a lot of stuff. We did a Contentful NxJS application, we used an image optimization library called Unpick Image, and then we used an image CDN to remove all that nasty overhead of having to invalidate caches or store images in the cloud and finally, we leveraged that CDN to use some AI parameters like facial recognition cropping, like background removal, like upscaling and make sure our images always looked their best when they were being displayed. That's transforming images using AI without leaving your React app and I am sufficiently out of breath. Thank you all so much.

Check out more articles and videos

We constantly think of articles and videos that might spark Git people interest / skill us up or help building a stellar career

Building a Voice-Enabled AI Assistant With Javascript
JSNation 2023JSNation 2023
21 min
Building a Voice-Enabled AI Assistant With Javascript
Top Content
This Talk discusses building a voice-activated AI assistant using web APIs and JavaScript. It covers using the Web Speech API for speech recognition and the speech synthesis API for text to speech. The speaker demonstrates how to communicate with the Open AI API and handle the response. The Talk also explores enabling speech recognition and addressing the user. The speaker concludes by mentioning the possibility of creating a product out of the project and using Tauri for native desktop-like experiences.
AI and Web Development: Hype or Reality
JSNation 2023JSNation 2023
24 min
AI and Web Development: Hype or Reality
Top Content
This talk explores the use of AI in web development, including tools like GitHub Copilot and Fig for CLI commands. AI can generate boilerplate code, provide context-aware solutions, and generate dummy data. It can also assist with CSS selectors and regexes, and be integrated into applications. AI is used to enhance the podcast experience by transcribing episodes and providing JSON data. The talk also discusses formatting AI output, crafting requests, and analyzing embeddings for similarity.
The Rise of the AI Engineer
React Summit US 2023React Summit US 2023
30 min
The Rise of the AI Engineer
Watch video: The Rise of the AI Engineer
The rise of AI engineers is driven by the demand for AI and the emergence of ML research and engineering organizations. Start-ups are leveraging AI through APIs, resulting in a time-to-market advantage. The future of AI engineering holds promising results, with a focus on AI UX and the role of AI agents. Equity in AI and the central problems of AI engineering require collective efforts to address. The day-to-day life of an AI engineer involves working on products or infrastructure and dealing with specialties and tools specific to the field.
Web Apps of the Future With Web AI
JSNation 2024JSNation 2024
32 min
Web Apps of the Future With Web AI
Web AI in JavaScript allows for running machine learning models client-side in a web browser, offering advantages such as privacy, offline capabilities, low latency, and cost savings. Various AI models can be used for tasks like background blur, text toxicity detection, 3D data extraction, face mesh recognition, hand tracking, pose detection, and body segmentation. JavaScript libraries like MediaPipe LLM inference API and Visual Blocks facilitate the use of AI models. Web AI is in its early stages but has the potential to revolutionize web experiences and improve accessibility.
Building the AI for Athena Crisis
JS GameDev Summit 2023JS GameDev Summit 2023
37 min
Building the AI for Athena Crisis
Join Christoph from Nakazawa Tech in building the AI for Athena Crisis, a game where the AI performs actions just like a player. Learn about the importance of abstractions, primitives, and search algorithms in building an AI for a video game. Explore the architecture of Athena Crisis, which uses immutable persistent data structures and optimistic updates. Discover how to implement AI behaviors and create a class for the AI. Find out how to analyze units, assign weights, and prioritize actions based on the game state. Consider the next steps in building the AI and explore the possibility of building an AI for a real-time strategy game.
Code coverage with AI
TestJS Summit 2023TestJS Summit 2023
8 min
Code coverage with AI
Codium is a generative AI assistant for software development that offers code explanation, test generation, and collaboration features. It can generate tests for a GraphQL API in VS Code, improve code coverage, and even document tests. Codium allows analyzing specific code lines, generating tests based on existing ones, and answering code-related questions. It can also provide suggestions for code improvement, help with code refactoring, and assist with writing commit messages.

Workshops on related topic

AI on Demand: Serverless AI
DevOps.js Conf 2024DevOps.js Conf 2024
163 min
AI on Demand: Serverless AI
Top Content
Featured WorkshopFree
Nathan Disidore
Nathan Disidore
In this workshop, we discuss the merits of serverless architecture and how it can be applied to the AI space. We'll explore options around building serverless RAG applications for a more lambda-esque approach to AI. Next, we'll get hands on and build a sample CRUD app that allows you to store information and query it using an LLM with Workers AI, Vectorize, D1, and Cloudflare Workers.
Leveraging LLMs to Build Intuitive AI Experiences With JavaScript
JSNation 2024JSNation 2024
108 min
Leveraging LLMs to Build Intuitive AI Experiences With JavaScript
Featured Workshop
Roy Derks
Shivay Lamba
2 authors
Today every developer is using LLMs in different forms and shapes, from ChatGPT to code assistants like GitHub CoPilot. Following this, lots of products have introduced embedded AI capabilities, and in this workshop we will make LLMs understandable for web developers. And we'll get into coding your own AI-driven application. No prior experience in working with LLMs or machine learning is needed. Instead, we'll use web technologies such as JavaScript, React which you already know and love while also learning about some new libraries like OpenAI, Transformers.js
Llms Workshop: What They Are and How to Leverage Them
React Summit 2024React Summit 2024
66 min
Llms Workshop: What They Are and How to Leverage Them
Featured Workshop
Nathan Marrs
Haris Rozajac
2 authors
Join Nathan in this hands-on session where you will first learn at a high level what large language models (LLMs) are and how they work. Then dive into an interactive coding exercise where you will implement LLM functionality into a basic example application. During this exercise you will get a feel for key skills for working with LLMs in your own applications such as prompt engineering and exposure to OpenAI's API.
After this session you will have insights around what LLMs are and how they can practically be used to improve your own applications.
Table of contents: - Interactive demo implementing basic LLM powered features in a demo app- Discuss how to decide where to leverage LLMs in a product- Lessons learned around integrating with OpenAI / overview of OpenAI API- Best practices for prompt engineering- Common challenges specific to React (state management :D / good UX practices)
Working With OpenAI and Prompt Engineering for React Developers
React Advanced Conference 2023React Advanced Conference 2023
98 min
Working With OpenAI and Prompt Engineering for React Developers
Top Content
Workshop
Richard Moss
Richard Moss
In this workshop we'll take a tour of applied AI from the perspective of front end developers, zooming in on the emerging best practices when it comes to working with LLMs to build great products. This workshop is based on learnings from working with the OpenAI API from its debut last November to build out a working MVP which became PowerModeAI (A customer facing ideation and slide creation tool).
In the workshop they'll be a mix of presentation and hands on exercises to cover topics including:
- GPT fundamentals- Pitfalls of LLMs- Prompt engineering best practices and techniques- Using the playground effectively- Installing and configuring the OpenAI SDK- Approaches to working with the API and prompt management- Implementing the API to build an AI powered customer facing application- Fine tuning and embeddings- Emerging best practice on LLMOps
Building AI Applications for the Web
React Day Berlin 2023React Day Berlin 2023
98 min
Building AI Applications for the Web
Workshop
Roy Derks
Roy Derks
Today every developer is using LLMs in different forms and shapes. Lots of products have introduced embedded AI capabilities, and in this workshop you’ll learn how to build your own AI application. No experience in building LLMs or machine learning is needed. Instead, we’ll use web technologies such as JavaScript, React and GraphQL which you already know and love.
Building Your Generative AI Application
React Summit 2024React Summit 2024
82 min
Building Your Generative AI Application
WorkshopFree
Dieter Flick
Dieter Flick
Generative AI is exciting tech enthusiasts and businesses with its vast potential. In this session, we will introduce Retrieval Augmented Generation (RAG), a framework that provides context to Large Language Models (LLMs) without retraining them. We will guide you step-by-step in building your own RAG app, culminating in a fully functional chatbot.
Key Concepts: Generative AI, Retrieval Augmented Generation
Technologies: OpenAI, LangChain, AstraDB Vector Store, Streamlit, Langflow