Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8943639
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T11:49:35+00:00 2026-06-15T11:49:35+00:00

I m using GD library to create images on the fly. But when i

  • 0

I m using GD library to create images on the fly.
But when i rotate image using imagerotate() function
it works fine but it gives very much irritating rough edges of image
which is rotated.
as it is shown in this picture.
So how to make these sides/edges of rotated image smooth ?
enter image description here

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T11:49:37+00:00Added an answer on June 15, 2026 at 11:49 am

    One way to avoid from getting the Jaggies effect when rotating images is by using another way to sample the pixels than just taking the adjusted pixels, for example to use Nearest-neighbor interpolation to make the edges smoother. You can see matlab code example:

    im1 = imread('lena.jpg');imshow(im1);  
    [m,n,p]=size(im1);
    thet = rand(1);
    mm = m*sqrt(2);
    nn = n*sqrt(2);
    for t=1:mm
       for s=1:nn
          i = uint16((t-mm/2)*cos(thet)+(s-nn/2)*sin(thet)+m/2);
          j = uint16(-(t-mm/2)*sin(thet)+(s-nn/2)*cos(thet)+n/2);
          if i>0 && j>0 && i<=m && j<=n           
             im2(t,s,:)=im1(i,j,:);
          end
       end
    end
    figure;
    imshow(im2);
    

    taken from (here). Basically it means that when sampling the pixels in the original picture, we sample near pixels and interpolate them to get the target pixel value. This way
    you can achive waht you want withourt installing any additiional packages.

    EDIT

    I’ve found some old code I once wrote in Java, which contains implementations of a couple of sampling algorithems. Here is the code:

    Nearest Neighbor sampler:

    /**
     * @pre (this!=null) && (this.pixels!=null)
     * @post returns the sampled pixel of (x,y) by nearest neighbor sampling
     */
    private Pixel sampleNearestNeighbor(double x, double y) {
        int X = (int) Math.round(x);
        int Y = (int) Math.round(y);
        if (X >= 0 && Y >= 0 && X < this.pixels.length
                && Y < this.pixels[0].length)
            // (X,Y) is within this.pixels' borders
            return new Pixel(pixels[X][Y].getRGB());
        else
            return new Pixel(255, 255, 255);
        // sample color will be default white
    }
    

    Bilinear sampler:

    /**
     * @pre (this!=null) && (this.pixels!=null)
     * @post returns the sampled pixel of (x,y) by bilinear interpolation
     */
    private Pixel sampleBilinear(double x, double y) {
        int x1, y1, x2, y2;
        x1 = (int) Math.floor(x);
        y1 = (int) Math.floor(y);
        double weightX = x - x1;
        double weightY = y - y1;
        if (x1 >= 0 && y1 >= 0 && x1 + 1 < this.pixels.length
                && y1 + 1 < this.pixels[0].length) {
            x2 = x1 + 1;
            y2 = y1 + 1;
    
            double redAX = (weightX * this.pixels[x2][y1].getRed())
                    + (1 - weightX) * this.pixels[x1][y1].getRed();
            double greenAX = (weightX * this.pixels[x2][y1].getGreen())
                    + (1 - weightX) * this.pixels[x1][y1].getGreen();
            double blueAX = (weightX * this.pixels[x2][y1].getBlue())
                    + (1 - weightX) * this.pixels[x1][y1].getBlue();
            // bilinear interpolation of A point
    
            double redBX = (weightX * this.pixels[x2][y2].getRed())
                    + (1 - weightX) * this.pixels[x1][y2].getRed();
            double greenBX = (weightX * this.pixels[x2][y2].getGreen())
                    + (1 - weightX) * this.pixels[x1][y2].getGreen();
            double blueBX = (weightX * this.pixels[x2][y2].getBlue())
                    + (1 - weightX) * this.pixels[x1][y2].getBlue();
            // bilinear interpolation of B point
    
            int red = (int) (weightY * redBX + (1 - weightY) * redAX);
            int green = (int) (weightY * greenBX + (1 - weightY) * greenAX);
            int blue = (int) (weightY * blueBX + (1 - weightY) * blueAX);
            // bilinear interpolation of A and B
            return new Pixel(red, green, blue);
    
        } else if (x1 >= 0
                && y1 >= 0 // last row or column
                && (x1 == this.pixels.length - 1 || y1 == this.pixels[0].length - 1)) {
            return new Pixel(this.pixels[x1][y1].getRed(), this.pixels[x1][y1]
                    .getGreen(), this.pixels[x1][y1].getBlue());
        } else
            return new Pixel(255, 255, 255);
        // sample color will be default white
    }
    

    Gaussian sampler:

    /**
     * @pre (this!=null) && (this.pixels!=null)
     * @post returns the sampled pixel of (x,y) by gaussian function
     */
    private Pixel sampleGaussian(double u, double v) {
        double w = 3; // sampling distance
        double sqrSigma = Math.pow(w / 3.0, 2); // sigma^2
        double normal = 0;
        double red = 0, green = 0, blue = 0;
        double minIX = Math.round(u - w);
        double maxIX = Math.round(u + w);
        double minIY = Math.round(v - w);
        double maxIY = Math.round(v + w);
    
        for (int ix = (int) minIX; ix <= maxIX; ix++) {
            for (int iy = (int) minIY; iy <= maxIY; iy++) {
                double sqrD = Math.pow(ix - u, 2) + Math.pow(iy - v, 2);
                // squared distance between (ix,iy) and (u,v)
                if (sqrD < Math.pow(w, 2) && ix >= 0 && iy >= 0
                        && ix < pixels.length && iy < pixels[0].length) {
                    // gaussian function
                    double gaussianWeight = Math.pow(2, -1 * (sqrD / sqrSigma));
                    normal += gaussianWeight;
                    red += gaussianWeight * pixels[ix][iy].getRed();
                    green += gaussianWeight * pixels[ix][iy].getGreen();
                    blue += gaussianWeight * pixels[ix][iy].getBlue();
                }
            }
        }
        red /= normal;
        green /= normal;
        blue /= normal;
        return new Pixel(red, green, blue);
    }
    

    Actual rotate:

         /**
     * @pre (this!=null) && (this.pixels!=null) && (1 <= samplingMethod <= 3)
     * @post creates a new rotated-by-degrees Image and returns it
     */
    public myImage rotate(double degrees, int samplingMethod) {
        myImage outputImg = null;
    
        int t = 0;
        for (; degrees < 0 || degrees >= 180; degrees += (degrees < 0) ? 180
                : -180)
            t++;
    
        int w = this.pixels.length;
        int h = this.pixels[0].length;
        double cosinus = Math.cos(Math.toRadians(degrees));
        double sinus = Math.sin(Math.toRadians(degrees));
    
        int width = Math.round((float) (w * Math.abs(cosinus) + h * sinus));
        int height = Math.round((float) (h * Math.abs(cosinus) + w * sinus));
    
        w--;
        h--; // move from (1,..,k) to (0,..,1-k)
    
        Pixel[][] pixelsArray = new Pixel[width][height];
        double x = 0; // x coordinate in the source image
        double y = 0; // y coordinate in the source image
    
        if (degrees >= 90) { // // 270 or 90 degrees turn
            double temp = cosinus;
            cosinus = sinus;
            sinus = -temp;
        }
    
        for (int i = 0; i < width; i++) {
            for (int j = 0; j < height; j++) {
    
                double x0 = i;
                double y0 = j;
                if (degrees >= 90) {
                    if ((t % 2 == 1)) { // 270 degrees turn
                        x0 = j;
                        y0 = width - i - 1;
                    } else { // 90 degrees turn
                        x0 = height - j - 1;
                        y0 = i;
                    }
                } else if (t % 2 == 1) { // 180 degrees turn
                    x0 = width - x0 - 1;
                    y0 = height - y0 - 1;
                }
    
                // calculate new x/y coordinates and
                // adjust their locations to the middle of the picture
                x = x0 * cosinus - (y0 - sinus * w) * sinus;
                y = x0 * sinus + (y0 - sinus * w) * cosinus;
    
                if (x < -0.5 || x > w + 0.5 || y < -0.5 || y > h + 0.5)
                    // the pixels that does not have a source will be painted in
                    // default white
                    pixelsArray[i][j] = new Pixel(255, 255, 255);
                else {
                    if (samplingMethod == 1)
                        pixelsArray[i][j] = sampleNearestNeighbor(x, y);
                    else if (samplingMethod == 2)
                        pixelsArray[i][j] = sampleBilinear(x, y);
                    else if (samplingMethod == 3)
                        pixelsArray[i][j] = sampleGaussian(x, y);
                }
            }
            outputImg = new myImage(pixelsArray);
        }
    
        return outputImg;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm processing hundreds of images using an image manipulation library to create thumbnails. Thumbnails
I am using the PIL (python image library) to crop a very large image
I'm using the GD Library to create images from data I'm pulling from an
We're using version 8d of IJG's libjpeg library to create JPEG images from 24-bit
I'm creating an dynamic image, which creates headers on my page using PHPs GD-library.
I'm using iTextsharp library to create PDF files. I can declare for A4 Landscape
When using the ajax library to create an Ajax Client Control, I have always
I'm using the HighCharts library to create a chart and then update it via
I am using the DOMPDF library to create an invoice in PDF. This document
I'm using the box2dflash library to create a game, and so far I've been

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.