This question already has an answer here:
Closed 10 years ago.
Possible Duplicate:
How to define DPi for a new JPG image?
Is there a way to get a resolution (pixels per inch) of an image in Java? Either with Native APIs or third party libraries. I don't just want the height and width of an image but actually the resolution that determines the output quality of an image.
An image has only pixels. it has not ppi.
a monitor, an image sensor and an printer have ppi because they all have dots/pixels related to a measurable distance in inch.
ppi is short for pixels per inch, and therefore a measure for pixel density.
for calculation of ppi / dpi see http://en.wikipedia.org/wiki/Dots_per_inch
You simply divide the number of pixels or points by the number of inch that the output device or photo paper has.
But be carefull if points or pixels is wanted.
Related
I have a neural network (made in java) which classifies handwritten digits, trained using the mnist data set.
I have a GUI where the user will draw a number (number on the left) and when the user hits the "guess" button the drawing is converted into a 400 by 470 image and is down-scaled to a 20 by 20 image, then is centered to a 28 by 28 image to feed into the network where the output is given on the right.
Here is what the GUI looks like:
My problem however, is that when I have a number that doesn't take up the majority of the panel (such as the 3 in the image above) the down-scaled image that is used as the input for the network is too small which causes the network to guess incorrectly.
Here is the final input image when the number is drawn small:
Here is the final input image when the number is drawn large:
What I'm asking is: is there any way to make the number that is drawn small the same size as the number drawn large while still keeping the size of the image as 28 by 28?
You can either use another object-detection network just to find the bounding box, or just calculate where the leftmost, rightmost, upmost, and bottom-most drawn pixel is. If you fear there will be outliers (there should not unless the user purposefully clicks an area far from the figure), you can remove outliers fairly easily. There are a number of ways, but method is to compute the distance of each drawn pixel to the center of the image, putting them into a distribution (normal might be good enough), and then compute which are outliers, and get rid of them. (Or compute the distance beyond which pixels become outliers, and cropping the box to fit). Then you scale the rectangle up to the correct size.
This is just a general method. As for specifics, I do not know how exactly your images are represented, but you can iterate over every pixel and note their positions (the number of iterations is not overly expensive).
I've read the posts but I still do not understand how to convert my image to dpi.
I have a 2689x1900px image. I have created the drawable-hdpi, mdpi, xhdpi, xxhdpi and xxxhdpi folders.
I know that I need to insert the images there (same name) but I don't know to what pixel size do I need to convert. I'm using photoshop.
Can someone please explain the process of scaling and use my example (2689x1900) ?
Let's assume that 2689x1900 is your 100% image, the biggest one. The biggest density is xxxhdpi. So, xxxhdpi is a 100% density.
Next, we need to calculate the smaller sizes:
xxhdpi is 75% of xxxhdpi, so it is 0.75 * (2689x1900);
xhdpi is 50% of xxxhdpi, so it is 0.5 * (2689x1900);
hdpi is 37.5% of xxxhdpi, so it is 0.375 * (2689x1900);
mdpi is 25% of xxxhdpi, so it is 0.25 * (2689x1900)
You can find more on that topic in Supporting Multiple Screens.
Note though that your image size slightly differs from the mostly used xxxhdpi screen resolution, 2560x1920, and you may want to adjust the size before scaling so that the images will fit perfectly into different screen resolutions.
first of all, reducing the size of the image in photoshop can cause pixellations. i suggest you to use inkscape or any other vector graphics editor.
then for full screen image
mdpi - 1 (360x640)
hdpi - 1.5 (540×960)
xhdpi - 2 (720×1280)
xxhdpi - 3 (1080x1920)
xxxhdpi - 4 (1440×2560)
these are the ideal sizes of the image.
the integers represent the scaling factor. if you want to maintain the aspect ratio of the image, then select the nearest image folder and scale.it using this factor.
more details here https://developer.android.com/training/multiscreen/screendensities.html#TaskProvideAltBmp
How to calculate the percentage of an image (jpeg) that has been edited using Java? For example, when an image is uploaded, the user wants to know how many percent of that image has been edited, so if it is 0% it shows that it is an original picture.
If the original image and the edited image haven't changed size this is trivial. Compare every pixel in your original image to the edited image. Those pixels which are not the same may have been edited. The amount of change is ChangedPixels / TotalPixels.
Some image formats are lossy (JPG for instance.) Because the format is lossy, saving an image with no edits, and comparing it against the original copy will likely measure some change when there were no human edits because some content was distorted.
Images with different sizes are easy too. Just compare as many pixels as possible (ie. the minimum of the widths, and minimum of heights.) The amount of change then is:
int maxPixels = Max(width1, width2) * Max(height1, height2);
double amountDifferent = totalChangedPixels / (double)maxPixels;
Any pixels which were added or removed are considered "changed" pixels.
When using int resolution = Toolkit.getDefaultToolkit().getScreenResolution(); to get my monitor's resolution, the integer resolution returns 96. What does this mean, how could it be used, and, if possible, could I get the resolution in a 4:3, 16:9, etc. format?
getScreenResolution() return dots-per-inch (DPI). If you want the size of the screen, just use getScreenSize().
Here's a quote that gets you the basic idea of DPI vs. pixels:
So, if you have a 600 pixel x 600 pixel image at 300DPI, it will output at 2 inches square. If you change this images DPI to 150, this will mean it will output at 4 inches square. So, as you can see, changing the DPI of an image changes it output size.
You can find rest of the explanation here.
When I first asked this, I included as much information as I could, since I never know what will help someone if they can give m a solution, but from the answers, it seems I didn't make my point clear.
I'm leaving the original text at the bottom and shortening this. I hope that makes it clear.
I am working with images returned to me from another program. I can get an image that's, for example, 8x10 at 72 DPI or the same image at 16x20, also at 72DPI.
I'm working in Java. What I'd like to do is take the 16x20 image and resize it. If possible, I'd like to just change the file so instead of being 16x20 at 72DPI, it's 8x10 at 144DPI.
I know that it's still the same number of pixels, but viewers will say how big an image is and what the DPI is and also some open an image to its full size, so I'd rather them treat the 16x20 as a smaller image with a higher resolution.
Can I do that in Java? Will I lose any data? How would I do it - can I just change the metadata in the png or jpg file?
-----(original question below here)-----
I've seen a few questions that come close to this, but this is a little different.
I know you can decrease image resolution with an image in Java (also here). Any searches I've done have been concerned with decreasing the resolution of an image.
I have the opposite problem. I'm working with an image that I'm getting back in 72DPI. The advantage is I can increase the size of the picture, so I could make the size 100%, 200%, 400% and so on. All images are at 72DPI, so if I use 200% for the zoom, then open the image in a viewer and make it the same size as the 100% image, I am seeing a better resolution.
For clarity - I am getting an image from someone else, it could be generate from a PDF or picture, but I am given the image from another program. No matter what I do, this image is at 72DPI. I can't change that, but I can specify the size at 200% and it still comes back at 72DPI, just twice as big and with more detail.
I'm no graphics expert and this is the first time I've done any programming that uses graphics. The output could be jpg or png, so the format isn't that important. (I could probably also use other formats if needed.)
Let's say the original image is a photo - 8" x 10", and at 72 DPI. If I ask for a 200% zoom, I get a 16" x 20" image at 72DPI.
Can I, within Java, change that larger 16" x 20" 72DPI image to a 144DPI image that's 8" x 10" without losing data? In other words, double the DPI, but cut the size in half?
And, most importantly, can I do it without losing resolution? (I would think that can be done.)
No, if you take an 8x10 72dpi image and ask for a 200% zoom you'll get a 16x20 at 36 dpi. You can't magically increase image resolution.
What counts is the total number of pixels in the image. The stated DPI is just what the DPI will be at a specific size. Take your 8x10 image at 72dpi. It has 576x720 pixels. If you zoom to 200% you still have the same number of pixels, but now they'll be spread out over twice the linear dimensions, meaning the individual pixels will be bigger. Some software will attempt to increase resolution by interpolating pixels, i.e. creating new pixels by averaging the nearest neighbors. This can give the impression of higher resolution but the image does not contain any more data. It always has the effect of softening the image, especially any sharp edges or lines.
If you reduce the size of an image without resampling, you do increase the printed or displayed resolution, but the image gets smaller. For the same image, increasing the DPI to 144 will reduce the image size to 4x5.
This is tricky because, like so much in Java, you can't just access something in a simple way. Java does not keep track of DPI, but it does use dots per millimeter. Also, another part that is confusing is that you cannot change this kind of information in an image, or in a BufferedImage. You can only change this information when you are writing a BufferedImage out through an ImageWriter.
I was able to do this. As I said, I could specify the zoom on the image that was returned to me. No matter what zoom level, the output was 72 DPI. My goal was 300DPI. I specified a zoom level of 400%. So, on an 8" wide image of 72 DPI, that returned to me a 32" image of 72 DPI. All I had to do was specify a DPI of 288 (72 x 4) to override the default 72 DPI I was dealing with and then when it was written out, the image had the same number of pixels, but was considered to be done at 288 DPI rather than 72 DPI.
Here's a code snippet:
//Assumed there's already an ImageWriter iw
ImageWriteParam writeParam = writer.getDefaultWriteParam();
ImageTypeSpecifier typeSpecifier =
ImageTypeSpecifier.createFromBufferedImageType(BufferedImage.TYPE_INT_RGB);
IIOMetadata metadata = writer.getDefaultImageMetadata(typeSpecifier, writeParam);
if (metadata.isReadOnly() || !metadata.isStandardMetadataFormatSupported()) {
continue;
}
// Set Parameters and info
int DPI = 72 * scaling/100;
double dotsPerMilli = 1.0 * DPI / 10 / 2.54;
double checkDots = 1.0 * 144 / 10 / 2.54;
System.out.println("Dots per inch: " + DPI + ", DotsPerMilli: " + dotsPerMilli + ",
CheckDots = " + checkDots);
IIOMetadataNode root = new IIOMetadataNode("javax_imageio_1.0");
IIOMetadataNode horiz = new IIOMetadataNode("HorizontalPixelSize");
horiz.setAttribute("value", Double.toString(dotsPerMilli));
IIOMetadataNode vert = new IIOMetadataNode("VerticalPixelSize");
vert.setAttribute("value", Double.toString(dotsPerMilli));
IIOMetadataNode dim = new IIOMetadataNode("Dimension");
dim.appendChild(horiz);
dim.appendChild(vert);
root.appendChild(dim);
metadata.mergeTree("javax_imageio_1.0", root);
// From here, just write out file using an ImageOutputStream
final ImageOutputStream stream = ImageIO.createImageOutputStream(outFile);
System.out.println("Output file: " + outFile);
try {
writer.setOutput(ImageIO.createImageOutputStream(outFile));
writer.write(metadata, new IIOImage(image_to_save, null, metadata),
writeParam);
} catch (Exception e) {
System.out.println("Caught exception " + e + " when trying to write out
file.");
System.exit(0);
} finally {
stream.close();
}