Matlab image conversion jp2 to jpg - image

I have a weird problem in matlab. I have code that takes in a directory of jp2 files and converts all of them to either tiff, png, or a jpg file. Then it puts these files in a new directory. The user can specify how big they want the file to be in terms of how many pixels are used (EX: 1:3:end is every three pixels). This code works perfectly for the png and tiff conversions.
With the jpg conversion there is no error whatsoever but when I go to click on the jpeg file in the new folder (which it does go to at least) It says "Windows Photo Viewer can't open this picture because the file appears to be damaged, corrupted, or is too large" I tried opening the pictures in other viewers but it said the same thing. All of the png and tiff pictures opened fine.
Some help would be greatly appreciated, thanks!
Edit: I noticed when I call imshow on the location of the jpeg file it actually does show up in matlab. It still does not show up in any image viewers though

Related

Difference between TIFF image vs TIFF file

These 2 files are TIFF files but the file type is different, one is TIFF IMAGE and TIFF File. Can someone tell me the difference between them? I can't find the exact reason.
EDIT:
After enabling file extension

Create small high quality PDF embedding optimized PNG?

I'm trying to create a small PDF file, embedding one optimized PNG image displayed as a header and footer on a 3 page PDF (same image must appear 6x in the PDF)
My optimized PNG image is only 2.3KB. It looks very sharp.
Failed with libreoffice
When I insert just one instance of the 2.3KB PNG image into a Libreoffice Writer doc containing only text, then export as PDF I can see that the image gets re-compressed to JPG and the resulting PDF file grows by about 40KB after adding the image. It also loses quality, the PNG also gets JPG fuzzy edges.
If I right click the image and select compression, there is no way to disable recompressing the image (it's already optimized better than libreoffice could do it) I've tried setting a compression level of 0,1,9 etc. Choosing JPG, no resize, lossless, etc but there was no improvement.
Failed with wkhtmltopdf
I also tried making a test page and used wkhtml2pdf but it did the same thing. Adding the low quality flag made no difference.
PDF Spec suggests PNG is supported?
From skimming the PDF spec, it looks like PNG images are supported.
Even plain text PDF files are surprisingly large
The disappointing thing is also when I take a 7KB HTML file which is basically just <html><body><p>foo...</p><p>bar...</p> (only about 15 paragraphs) with no CSS. The resulting 2 page PDF file is 30KB. Why should a 7kb (almost plain text) file become 30kb as a PDF?
Suggestions?
Can someone please suggest how to make a small PDF file in Linux?
I need to include 7KB of text and repeat one PNG image 6 times.
Manually or programatically. I'll take whatever I can get at this point.
PDF Spec suggests PNG is supported?
PNG isn't supported per se; PDF allows embedding JPEG images as-is, but not PNG images. PDF does borrow a set of features of the PNG format, however.
rinohtype (full disclosure: I'm the author) tries to embed as much as possible from PNG images as-is into the PDF. This does involve some bit-juggling to separate the alpha channel from the color data for example, but no reencoding of the image is performed. It does not (yet) support interlaced PNGs.
rinohtype should be able to do what you want to achieve. But please note that it currently is in a beta stage, so you might encounter some bugs.
Even plain text PDF files are surprisingly large
To keep the PDF size as small as possible, make sure not to embed/subset any of the fonts. Use only the fonts from the base 14 PDF fonts which are provided by PDF readers.
What you want is certainly achievable. Regarding the image quality, I would recommend making your image twice the size that you want it to actually display at in the PDF to keep it looking sharp.
As to the size, I've just modified a test in my PDF writer module (WIP..) to include a 7.2K png, 200px x 70px, in a PDF twice and the PDF came out at 6.8K 8). There's not much text included, but more text will only add what it's worth + a small percentage.
You can see the module and original test here.. https://github.com/DoccaPDF/docca-pdf-writer/blob/master/src/tests/writer.js#L40
That test adds ~112K of images to the PDF and results in a 103K PDF.
Of course not all images are created equal so you milage may vary..
*the images are only actually added to the PDF once, but are displayed multiple time.

Can a PNG image contain multiple pages?

On OSX I converted a multi-page PDF file to PNG and (somehow) it created a multi-page PNG file.
Is there an extension to the PNG format that allows this? Or is this not something I can validly create?
~~~~
To clarify, this is a PNG file, per the builtin file command and the identify command from imagemagick.
$ file algorithms-combined-print.png
algorithms-combined-print.png: PNG image data, 1275 x 1650, 8-bit/color RGBA, non-interlaced
$ identify algorithms-combined-print.png
algorithms-combined-print.png PNG 1275x1650 1275x1650+0+0 8-bit sRGB 3.537MB 0.000u 0:00.000
And here is a pastebin of the command identify -verbose algorithms-combined-print.png: http://pastebin.com/hw1yuRKa
What is notable from that output is that the pixel count is Number pixels: 2.104M which corresponds to one page. However, the file size is 3.537MB, which is clearly sufficient to hold all the pages.
Per request, here is the output of pngcheck: http://pastebin.com/aCRMEd9L
PNG does not support "multipage" images.
MNG is a PNG variant that supports multiple images - mostly for animations, but it's not a real PNG image (diffent signature/header), and has never become popular.
APNG is a similar attempt, but more focused on animations - it's more popular and alive, though it's less official - it's also PNG compatible (a standard PNG viewer, unaware of APNG, will display it as a single PNG image).
Another possible explanation is that your image is actually a TIFF image with a wrong .png extension, and the viewer ignores it.
The only way to know for sure is to look inside the image file itself (at least to the first bytes)
Update: given the pngcheck output, it seems to be a APNG file.

what is the format of a binary image & how is it different from jpg, png images?

I searched the internet for the basic formats of image files (e.g. .jpg, .png, .gif) as there is a specific format for .doc, .pdf etc. But didn't got anything relevant. And today I also came with an .bin image format. BIN signifies that the image is in the Binary format. So, what is the Internal format of .jpg image file. And How is it different from .bin (Binary) format. Because everything is Basically saved in Binary Form. And How is BITMAP Image different from .jpg format.
if you open the files in notepad or change the jpg to .txt and a exe to .txt you will see the first X amount of bytes defines what type of file it is etc. I have never looked into where the "standard" is but as you will see all JPEGS start with a specific byte and EXE start with a specific byte no matter what the content
Also JPEG is a licensed compressed form of an image and BMP is Microsoft Windows version of an image(with little or no compression I believe. png is open source or GPL licensed and technically your files do not need to be "licensed" to convert to JPEG. This is the same as a .MP3 vs a.OGG in terms of music

File created using imwrite does not appear

New to Matlab, so sorry if this is a silly question. I'm filtering a series images for my research. I'm not having a problem with the actual image processing, it's when I go to save the modified images that I run into trouble. For some reason, I can only save the modified images using imwrite as .gif files. If I try to save them as .jpg, .bmp, etc., the file does not appear in the working folder. The corresponding generic file appears, but the actual .jpg does not. Additionally, when I use imread to reopen the midified files (that actually saved as .gifs), the image is just black. But, if I open the .gif file outside Matlab, it appears as expected. Code below.
close all
N=90;
IMAGES=cell(1,N); %creates a cell to store image data
FNAMEFMT='20110805115033(1)_%d.jpg';
for i=1:N
IMAGES{i}=imread(sprintf(FNAMEFMT,i)); %reads original images into IMAGES
end
RESULT=cell(1,N); %to store modified/filtered images
for i=1:N
gray=rgb2gray(IMAGES{i}); %converts to grayscale
binary=im2bw(gray,.5); %converts to bw
filter=bwareaopen(binary,35); %removes small features
RESULT{i}=filter; %saves modified image in RESULTS
end
for i=1:N
WRITEFMT='filter_%d';
imwrite(RESULT{i},sprintf(WRITEFMT,i),'gif'); %writes RESULTS as .gif
end
If I try to save them as .jpg, .bmp, etc., the file does not appear in the working folder.
You need to change
WRITEFMT='filter_%d';
to
WRITEFMT='filter_%d.jpg';
The files you are outputting are jpeg files (as per the imwrite argument 'jpg' instead of 'gif'), but they don't have a file extension. If you manually add the extension, they open as jpgs.
For the black gif, see if this helps.
Once you export as jpg, viewing them works
imshow(imread('filter_1.jpg'))

Resources