convert from tiff to jp2

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
wfa
Posts: 7
Joined: 2015-12-28T09:45:59-07:00
Authentication code: 1151

convert from tiff to jp2

Post by wfa »

I converted some images from tiff format to JPEG2000, but the image resolution changes from 300x300 to 72x72. How can prevent this? I used several options, but nothing helps.
Can anyone help me on this?
Version: ImageMagick 6.9.2-7 Q16 x86_64
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: convert from tiff to jp2

Post by fmw42 »

What was your exact command line? What platform?

Add -density 300x300 to your command
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: convert from tiff to jp2

Post by fmw42 »

Post one of your tiff files so we can test with it. You can upload to some free hosting service such as dropbox.com and put the URL here.
wfa
Posts: 7
Joined: 2015-12-28T09:45:59-07:00
Authentication code: 1151

Re: convert from tiff to jp2

Post by wfa »

Platform is Fedora 23, kernel 4.2.8-300.fc23.x86_64
I executed the following:
convert -define "jp2:quality 60 -density 300x300"" 108_00001.tif 108_lossy_60.jp2
gm convert -define "jp2:quality 60" 108_00001.tif 108_00001_60gm.jp2
gm convert -define "jp2:quality 60 -density 300x300" 108_00001.tif 108_00001_60gm300x300.jp2

After that I identified the files:
identify -format "%g %x %y %Q \n" 108_00001.tif
and I get these results:
2706x3881+0+0 300 300 92
identify -format "%g %x %y %Q \n" 108_00001_60.jp2
2706x3881+0+0 72 72 92

The files are in my dropbox:https://www.dropbox.com/sh/8dnac8w0wq8j ... qpYNa?dl=0
The filesize are:
642K 108_00001_60gm300x300.jp2
642K 108_00001_60gm.jp2
5.2M 108_00001_60.jp2

Now I have two questions:
1) Why does a jp2 does not have resolution or density
2) Why does the same command in Imagemagick compress an 11MB tiff to a 5.2MB JP2 and compresses Graphicsmagic with the same options to 624K?

Can you help me out?
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: convert from tiff to jp2

Post by fmw42 »

What program created the JP2 file? That is where the density and units are missing. IM only handles what it is given in the meta data. So if there is no density, most tools assumes density 72. If you want some other density, then you must set it with -density.

GraphicsMagick was an old spin off of Imagemagick and Imagemagick no longer maintains that. It was spun off and kept simple, perhaps to keep processing faster and file size smaller. But IM may be using a different delegate library to process it than GraphicsMagick. IM is using OpenJPEG2000 rather than the older Jasper delegate library for more functionality. I do not know what GraphicsMagick is using.

Using EXIFTOOL to see the meta data, it does not show density or resolution, which should be listed if included. So the creator of your JP2 file has not set it.

Code: Select all

exiftool -s -ee -g1 -u -n -D 108_00001_60.jp2
---- ExifTool ----
    - ExifToolVersion                 : 8.71
---- System ----
    - FileName                        : 108_00001_60.jp2
    - Directory                       : .
    - FileSize                        : 5401191
    - FileModifyDate                  : 2015:12:28 14:25:53-08:00
    - FilePermissions                 : 644
---- File ----
    - FileType                        : JP2
    - MIMEType                        : image/jp2
---- Jpeg2000 ----
    0 MajorBrand                      : jp2
    1 MinorVersion                    : 0.0.0
    2 CompatibleBrands                : jp2
    0 ImageHeight                     : 3881
    4 ImageWidth                      : 2706
    8 NumberOfComponents              : 1
   10 BitsPerComponent                : 7
   11 Compression                     : 7
    0 ColorSpecMethod                 : 1
    1 ColorSpecPrecedence             : 0
    2 ColorSpecApproximation          : 0
    3 ColorSpace                      : 17
---- Composite ----
    - ImageSize                       : 2706x3881
User avatar
magick
Site Admin
Posts: 11064
Joined: 2003-05-31T11:32:55-07:00

Re: convert from tiff to jp2

Post by magick »

To reduce the image size, reduce the image quality. A quality of 37 returns an image that is about 600K:
  • convert -define jp2:quality=37.0 -density 300x300 108_00001.tif 108_00001.jp2
The quality value is specific to ImageMagick. Different programs may return different results than ImageMagick because they use different algorithms to achieve the desired compression levels.

ImageMagick leverages the OpenJP2 delegate library to write the JP2 image format. We are not aware that OpenJP2 permits saving the image resolution metadata. If you find a way to save the image resolution to the JP2 file, let us know and we'll add a patch to the next release of ImageMagick.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: convert from tiff to jp2

Post by fmw42 »

User avatar
magick
Site Admin
Posts: 11064
Joined: 2003-05-31T11:32:55-07:00

Re: convert from tiff to jp2

Post by magick »

There is no reference to an OpenJP2 API call in the link you posted. OpenJP2 creates the JP2 image file so we need to make an OpenJP2 API call with the appropriate metadata, if its supported.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: convert from tiff to jp2

Post by fmw42 »

magick wrote:There is no reference to an OpenJP2 API call in the link you posted. OpenJP2 creates the JP2 image file so we need to make an OpenJP2 API call with the appropriate metadata, if its supported.

Is this relevant? Not sure if that is density or number of resolution levels? (EDIT: seems to be the latter)

http://www.openjpeg.org/doxygen/group___j_p2.html


OPJ_BOOL opj_jp2_set_decoded_resolution_factor ( opj_jp2_t * p_jp2,
OPJ_UINT32 res_factor,
opj_event_mgr_t * p_manager
)
User avatar
magick
Site Admin
Posts: 11064
Joined: 2003-05-31T11:32:55-07:00

Re: convert from tiff to jp2

Post by magick »

That method refers to the resolutions of a JPEG 2000 image, not the image density. Its a completely different concept.
wfa
Posts: 7
Joined: 2015-12-28T09:45:59-07:00
Authentication code: 1151

Re: convert from tiff to jp2

Post by wfa »

Thanks guys for the replies.

Both 64K files were created using gm and the 5M file using IM. I'd rather use IM, but it looks like IM is not using the options I add.

First, here's my environment:

Code: Select all

convert -version
Version: ImageMagick 6.9.2-7 Q16 x86_64 2015-12-06 http://www.imagemagick.org
Copyright: Copyright (C) 1999-2015 ImageMagick Studio LLC
License: http://www.imagemagick.org/script/license.php
Features: Cipher DPC Modules OpenMP
Delegates (built-in): bzlib cairo djvu fftw fontconfig freetype gslib jbig jng jp2 jpeg lcms ltdl lzma openexr pangocairo png ps rsvg tiff webp wmf x xml zlib

Code: Select all

rpm -qa|grep openj
openjpeg2-2.1.0-7.fc23.x86_64
openjpeg-libs-1.5.1-15.fc23.x86_64

I executed the following:

Code: Select all

convert test.tif -define "jp2:quality=37.0 -density 300x300" test.jp2
when I identify the jp2 with

Code: Select all

identify -format "%x x %y\n" test.jp2
it returns: 72 x 72. I don't get it...
The files are listed here: https://www.dropbox.com/sh/8dnac8w0wq8j ... qpYNa?dl=0
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: convert from tiff to jp2

Post by fmw42 »

As Magick said above, it seems that JP2 does not support density or units. No way to store that date in the JP2 file's meta data. So using -density, its values will not be stored in the JP2 meta data. Going the other way from JP2 to tiff -density should work.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: convert from tiff to jp2

Post by snibgo »

wfa wrote:convert test.tif -define "jp2:quality=37.0 -density 300x300" test.jp2
Your syntax is wrong. What are those quotes doing?
snibgo's IM pages: im.snibgo.com
Post Reply