<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Cihat on Medium]]></title>
        <description><![CDATA[Stories by Cihat on Medium]]></description>
        <link>https://medium.com/@cihataydin?source=rss-f71a84c279fe------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*s1xCVpxpz6hZn_sVzT6jqQ.jpeg</url>
            <title>Stories by Cihat on Medium</title>
            <link>https://medium.com/@cihataydin?source=rss-f71a84c279fe------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Wed, 18 Nov 2026 09:43:47 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@cihataydin/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Building a Python Web App for Image Comparison: A Step-by-Step Guide]]></title>
            <link>https://medium.com/@cihataydin/building-a-python-web-app-for-image-comparison-a-step-by-step-guide-c9538f1fdcb4?source=rss-f71a84c279fe------2</link>
            <guid isPermaLink="false">https://medium.com/p/c9538f1fdcb4</guid>
            <dc:creator><![CDATA[Cihat]]></dc:creator>
            <pubDate>Mon, 22 Jul 2024 20:32:37 GMT</pubDate>
            <atom:updated>2024-07-22T20:32:37.412Z</atom:updated>
            <content:encoded><![CDATA[<p>Introduction: In this article, we’ll walk through the process of creating a web application that compares two images and highlights their differences. We’ll use Python, Flask, and OpenCV to build this app, and then deploy it using Render. This guide is perfect for beginners looking to create their first web application or for those interested in image processing.</p><p>Step 1: Setting Up the Environment First, ensure you have Python installed on your system. We’ll be using Flask as our web framework, OpenCV for image processing, and other libraries. Install the required packages:</p><pre>pip install Flask opencv-python-headless numpy scikit-image</pre><p>Step 2: Creating the Flask Application Let’s start by creating a basic Flask application. Here’s the initial structure:</p><pre>from flask import Flask, render_template, request<br>import cv2<br>import numpy as np<br>from io import BytesIO<br>import base64<br>from skimage.metrics import structural_similarity<br><br>app = Flask(__name__)<br><br>@app.route(&#39;/&#39;, methods=[&#39;GET&#39;, &#39;POST&#39;])<br>def index():<br>    if request.method == &#39;POST&#39;:<br>        # We&#39;ll add image comparison logic here<br>        pass<br>    return render_template(&#39;index.html&#39;)<br><br>if __name__ == &#39;__main__&#39;:<br>    app.run(debug=True)</pre><p>Step 3: Implementing Image Comparison Next, we’ll add the image comparison functionality. We’ll use OpenCV to process the images and highlight the differences:</p><pre>def compare_images(img1, img2):<br>    img1 = cv2.resize(img1, (500, 500))<br>    img2 = cv2.resize(img2, (500, 500))<br>    <br>    diff = cv2.absdiff(img1, img2)<br>    gray_diff = cv2.cvtColor(diff, cv2.COLOR_BGR2GRAY)<br>    <br>    threshold = 20<br>    _, thresh = cv2.threshold(gray_diff, threshold, 255, cv2.THRESH_BINARY)<br>    <br>    kernel = np.ones((3,3), np.uint8)<br>    thresh = cv2.morphologyEx(thresh, cv2.MORPH_OPEN, kernel)<br>    <br>    diff_mask = thresh &gt; 0<br>    img1[diff_mask] = [0, 0, 255]  # Highlight differences in red<br>    <br>    contours, _ = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)<br>    cv2.drawContours(img1, contours, -1, (0, 255, 0), 1)  # Draw green contours<br>    <br>    _, buffer = cv2.imencode(&#39;.jpg&#39;, img1)<br>    result_image = base64.b64encode(buffer).decode(&#39;utf-8&#39;)<br>    <br>    diff_pixel_count = np.sum(diff_mask)<br>    result_text = f&quot;Images are {&#39;the same&#39; if diff_pixel_count == 0 else &#39;different&#39;}. Different pixel count: {diff_pixel_count}&quot;<br>    <br>    return result_image, result_text</pre><p>Step 4: Handling File Uploads Update the index route to handle file uploads:</p><pre>@app.route(&#39;/&#39;, methods=[&#39;GET&#39;, &#39;POST&#39;])<br>def index():<br>    if request.method == &#39;POST&#39;:<br>        file1 = request.files[&#39;img1&#39;]<br>        file2 = request.files[&#39;img2&#39;]<br>        <br>        img1 = cv2.imdecode(np.frombuffer(file1.read(), np.uint8), cv2.IMREAD_COLOR)<br>        img2 = cv2.imdecode(np.frombuffer(file2.read(), np.uint8), cv2.IMREAD_COLOR)<br>        <br>        result_image, result_text = compare_images(img1, img2)<br>        return render_template(&#39;result.html&#39;, result_text=result_text, result_image=result_image)<br>    <br>    return render_template(&#39;index.html&#39;)</pre><p>Step 5: Creating HTML Templates Create two HTML templates: index.html for the upload form and result.html to display the comparison results.</p><p>Step 6: Deploying to Render To deploy our app on Render:</p><ol><li>Create a requirements.txt file with all necessary packages.</li><li>Create a Procfile with the command: web: gunicorn app:app</li><li>Push your code to GitHub.</li><li>Set up a new Web Service on Render, linking to your GitHub repository.</li></ol><p>Conclusion: We’ve built a web application that compares two images and highlights their differences. This project demonstrates the power of combining web development with image processing techniques. By deploying on Render, we’ve made our application accessible to anyone on the internet.</p><p>For a complete example of this project, you can check out the GitHub repository at <a href="https://github.com/cihataydin/image-process">https://github.com/cihataydin/image-process</a>. This repository contains the full source code and can serve as a reference for your own implementation.</p><p>Remember, this is a basic implementation and can be further improved with error handling, better UI, and more advanced image processing techniques. Happy coding!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c9538f1fdcb4" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>