4 Replies - 791 Views - Last Post: 03 April 2013 - 07:20 PM Rate Topic: -----

#1 Fyrdraca  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 02-April 13

Trying to pixelate a jpg by resizing it twice.

Posted 02 April 2013 - 08:24 PM

Here's how it is: I'm trying to create a pixelate effect for a jpg file with java. This is what I have so far. It will allow me to resize a jpg, but I'm looking to resize it back to get that pixelated effect, like an old-school arcade game. What changes do I need to make to my method to get it to do this?

public BufferedImage scaleImage(BufferedImage img, int width, int height,
        Color background) {
    int imgWidth = img.getWidth();
    int imgHeight = img.getHeight();
    if (imgWidth*height < imgHeight*width) {
        width = imgWidth*height/imgHeight;
    } else {
        height = imgHeight*width/imgWidth;
    }
    BufferedImage newImage = new BufferedImage(width, height,
            BufferedImage.TYPE_INT_RGB);
    Graphics2D g = newImage.createGraphics();
    try {
        g.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
                RenderingHints.VALUE_INTERPOLATION_BICUBIC);
        g.setBackground(background);
        g.clearRect(0, 0, width, height);
        g.drawImage(img, 0, 0, width, height, null);
    } finally {
        g.dispose();
    }
    return newImage;
}



Is This A Good Question/Topic? 0
  • +

Replies To: Trying to pixelate a jpg by resizing it twice.

#2 farrell2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 823
  • View blog
  • Posts: 2,532
  • Joined: 29-July 11

Re: Trying to pixelate a jpg by resizing it twice.

Posted 03 April 2013 - 12:17 AM

It seems like a waste of time, as you could just do this is any image editor, but here is allegedly a means to do it. http://www.javaprogr...g-tutorial.html

Links not working now...
Was This Post Helpful? 0
  • +
  • -

#3 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8327
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Trying to pixelate a jpg by resizing it twice.

Posted 03 April 2013 - 06:52 PM

90% of the posts here are about re-inventing the wheel so... we help people doing LinkedList, resizable arrays, Node, Hashmap, ....

May be you are the one wasting his time here :)
Was This Post Helpful? 0
  • +
  • -

#4 lordofduct  Icon User is offline

  • I'm a cheeseburger
  • member icon


Reputation: 2533
  • View blog
  • Posts: 4,633
  • Joined: 24-September 10

Re: Trying to pixelate a jpg by resizing it twice.

Posted 03 April 2013 - 06:59 PM

He did include a link that could help.

So it wasn't much a waste of time.

Unlike your post which didn't assist in all and only contained criticism.

Criticism and help VS criticism.

Sounds like you're the one wasting time.




As for OP, how about take a scale value from 0 to 1. The closer to 1, the more pixelation. Now how this works would be the scale of the pixelation size... 1 would be basically turning the entire image into 1 single giant pixel, 0 is no change at all.

So how'd you go about this is now take that scale. Subtract it from 1 to flip the value and multiply by the pixel height and width [ (1 - scale) * width ]. This would give you the number of pixels for this scale in both width and height. Now loop thru all the pixels in the image and find a cluster of pixels in these ranges. Take the values of each pixel and find their average, and set all the pixels in said cluster to this new averaged color. Now you got a pixelized effect with out resizing AND it's got a easy scale value to play with it.
Was This Post Helpful? 1
  • +
  • -

#5 farrell2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 823
  • View blog
  • Posts: 2,532
  • Joined: 29-July 11

Re: Trying to pixelate a jpg by resizing it twice.

Posted 03 April 2013 - 07:20 PM

pbl probably still has sour grapes because of another thread where he -1 me a few times for proving that I could easily do something that he claimed was BS on my part. :lol:
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1