Losslessly double JPEG resolution without SmartScale - imagemagick

How can I convert a 256x256 JPEG to a 512x512 JPEG without losing any quality? (I can use jpegtran, but that uses SmartScale, which isn't widely supported.)
It must be converted to JPEG specifically. If it cannot be a literally lossless conversion, can it get very close? Virtually lossless? Thanks.
EDIT:
If it's not possible with any existing tool, please tell me if and how it might be possible in theory.
EDIT 2:
The reason I'm asking is because I want to losslessly combine 512x512 image tiles from the internet in order to re-assemble a panoramic image. The problem is that some of these tiles (along the top and bottom rows of the image) are 256x256 resolution, and require stretching before being combined with the other tiles. So in short, most of the tiles are 512x512 and thus wouldn't require reencoding, but some 256x256 tiles would.
If the 256x256 JPEGs became 512x512 JPEGs, I could combine all the tiles with jpegtran without reencoding, and they would form a simple equirectangular image.
I don't want to encode these panoramas to a different format like PNG, if I can help it, but I know I may need to. So I'm wondering if these few half-sized tiles can be scaled up essentially losslessly.

Related

What are some examples of imagemagick commands where option can be provided between magick and input filename

For e.g.
magick -density 100 apple.jpg -resize 100x100 apple-edited.jpg
Density here is the option which has to be provided prior to input file name.
Can someone please explain when is such prior options are needed. And also provide me few more examples.
PS: I tried looking for the options but not sure if there is an specific term for such options which will help limit the search results.
ImageMagick is, exclusively, a raster image processor, which means that it processes bitmap images made up of pixels laid out on a rectangular grid (or raster), rather than a vector image processor like Adobe Illustrator or Inkscape which deal with shapes, lines, rectangles and bezier curves described by their vertices or inflection points - not pixels.
When you load a vector image (e.g. an SVG image, or a PDF) into ImageMagick, the very first thing it does is rasterise your image onto a rectangular grid before it can work with the pixels. So, if your image is an SVG or a PDF, you need to set the density, or number of lines in the grid before you load the image.
I don't know of a reason to do that for a JPEG, like your example. I can't think of any other settings you absolutely need to make prior to loading an image.

PNG Compression Not Reducing the size of images

I have certain png files. They are of size approx 1MB. I tried several command but they didn't work for me. Any suggestions. One is as below :
"C:\\Program Files\\ImageMagick-6.9.9-Q16\\mogrify.exe" -depth 8 -format png -define PNG:compression-strategy=2 -define PNG:compression-filter=0 test.png
Thanks,
As pointed out by #fmw42 in comments, your image may already be optimized. Also, #Mark's comment regarding reducing colors is true.
But apart from this, the important thing to know is that "there is no ideal command". You will have to figure out bit depth in your color channels and reduce them. There will always be a trade-off between reducing colors and quality you wish to pick.
Apart from that, there can also be other methods that you can use:
If opacity of PNG is fully opaque, you can strip alpha channel as it
makes no sense in that case. This can give you some file size savings.
If the image is visibly grayscale and still color type is
true-color, true-color-alpha or indexed-color, you can make significant savings by saving the image with a grayscale color space.
Retry optimizing PNG files using adaptive delta filtering and LZ77 Optimizations. This can be done easily using "optipng". But if the image is already optimized enough, this won't provide significant file sizes reduction. Moreover, choice of filtering depends upon png bit depths, so you would have to look up and understand PNG compression from various documentation available online regarding PNG compression.

How to get the PDF from the Cytoscape graph without blurring the image?

I'm able to get the JPG and PNG images from the Cytoscape graph, but when converting them into PDF using jsPDF, the image shrinks and is blurry on zoom-in.
Is there a way the image or graph can be converted to PDF without blurring?
It seems to me like you need to learn the difference between bitmap formats and vector formats.
Bitmap formats like PNG, GIF, BMP and JPEG do not support limitless zooming because they are made of small squares, called pixels.
Vector formats like PDF, SVG and EPS support limitless zooming without blurriness because they are made of geometrical shapes (though PDF can include bitmaps).
So you can convert between bitmap formats without problems (though you may get compression artifacts when using JPEG) but you cannot directly go from vector to bitmap and vice versa.
If you convert a vector image to bitmap, you need to rasterize it to a specific resolution.
If you convert a bitmap image to a vector image, you could use some advanced algorithms to guess the shape behind the pixels, but there is almost never enough information for this to work perfectly.
If you convert a bitmap image to PDF, the tool you are using may just embed the bitmap in the PDF. Now the PDF editor may let you zoom, but the image will get blurry.
The only way to get a real vector PDF is by having the application, in this case Cytoscape, export PDF directly, not converting it afterwards.
You wrote Cytoscape cannot do this but I am reading the manual of version 3.7.0 and it says you can export directly to PDF using "File → Export as Image..." and then selecting "PDF". Maybe at the time you posted this question, this version wasn't released yet.
P.S.: As noted by #ideogram you are using Cytoscape.js and not Cytoscape, so you can use the cytoscape-svg NPM package to export as SVG, which is a vector format, which you can then convert to PDF without getting any blurriness.

Should I save an existing jpeg as a jpeg or as a png if I'm slightly altering it with a graphical overlay?

In particular, I'm using ImageMagick to draw a square over the jpeg, with a transparent fill and a pretty thin stroke?
My intuition says that the straight lines of the stroke might appear blurry, but that's less important than maintaining the quality and size of the original image.
Is there anything else that I should take into consideration?
Jpeg is lossy: compresses more but the quality decreases. You should try different levels of compression and comprare the results ( quality and size) against the png version. It may depend a lot on the contents of the image ( some images compress better than others)

ImageMagick help -- need to change DPI

I have a 1000x1000 300dpi image that I need to convert to a 100x100 96dpi thumbnail. How do I do this in ImageMagick? I'm after the smallest possible file size at the highest possible quality.
Doing something like this:
convert myimage.png -quality 100 -resize 100 PNG8:mynewimage.png
.... does change the dimension, but still maintains the DPI. If I can get this to change to 96dpi, I should get a smaller file size.
I've tried -density, etc., but can't seem to make them work for me. Maybe I put the commands in the wrong order or passed the wrong parameters. Any assistance is greatly appreciated. Thanks.
The short version is, if you want a 100x100 image in PNG format, the line you have will already give you best quality at smallest file size. You can't do any better than that without a) coding to a lossy format (JPEG) or b) reducing the color depth of your image.
For a slightly longer explanation, straight from Wikipedia: "Dots per inch (DPI) is a measure of spatial printing or video dot density, in particular the number of individual dots that can be placed in a line within the span of 1 inch (2.54 cm). The DPI value tends to correlate with image resolution, but is related only indirectly."
DPI has nothing to do with getting a smaller file size; your 100x100 image measures 100x100 pixels, no matter whether you see it on a 300dpi screen or a 96dpi one (it will just look smaller on the 300dpi screen). The amount of information is the same either way.
"-density" won't help either, as it only works when "encoding a raster image while rendering (reading) vector formats such as Postscript, PDF, WMF, and SVG into a raster image". Those formats are resolution-independant, so it makes sense to tell ImageMagick the DPI to which you want the image rasterized. However, your DPI should be a function of the output device you plan to use. In your case, since you're starting with an already-rasterized image, this has no use.
PNG is a lossless format, so the -quality parameter only controls the zlib compression level; any gains in image size will be minimal, but it's worth using.

Resources