How to make "convert" retain size & resolution

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
tongro

How to make "convert" retain size & resolution

Post by tongro »

I'm new to ImageMagick, and using version 6.0.6 on Debian Linux, from the command line.

I'm trying to use convert to convert either TIFF or JPEG files to either PS or PDF, but am having trouble controlling the size of the output image. What I want is to simply retain the size and resolution of the original version; it seems convert always wants to change the output resolution to 72 dpi, which also screws up the size.

I've played around with -resize, -density, and -units, and can get correct results if I know the input size/resolution. However, if I don't know those figures, I'm stuck.

Can anybody please help?

Thanks.

Tony.
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Post by anthony »

Instead of -resize use -resample with the resolution you want the image to be converted to.

however Postscript and PDF are vector graphic formats, which do not contain any 'resolution'. The defult by Im is to read them in at 72x72 dpi. However you can change that by setting -density before reading them in.

WARNING: see IM Examples
A word about Vector Image formats
http://www.cit.gu.edu.au/~anthony/graph ... ts/#vector

This is vital if you plan to read from or generate postscript or PDF document formats.

Question, does the PDF you are reading contain a raster image instead of a vector image?
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
tongro

Post by tongro »

Thanks for that.

What I want to do is read TIFF/JPEG (raster) and output PS/PDF.
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Post by anthony »

Not a good idea, but if you don't mind a raster image in the a PDF or postscript wrapper, then just have IM output pdf or postscript.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
tongro

Post by tongro »

If somebody could read the original post and come up with some ideas, it would be much appreciated!
el_supremo
Posts: 1015
Joined: 2005-03-21T21:16:57-07:00

Post by el_supremo »

I think you will have to be a bit more precise about what you mean by
What I want is to simply retain the size and resolution of the original version

If by "size and resolution" you mean that an input JPG which is 1024x768 pixels (size) at 100dpi (resolution) should produce a PDF which IM will identify as 1024x768 at 100 dpi, then you can't do it. You can't store the 100dpi resolution units in a PDF because, as Anthony has said, a PDF file does not have a resolution. If you don't specify any extra options to convert then the output PDF will be 1024x768 and will not contain a resolution. When you ask IM's identify about the resulting PDF file it will say that it is 1024x768 and it will also say that it is 72dpi but *only* because IM sets that as the default interpretation of a PDF (perhaps identify should not even print resolution or units in this case).
Note that IM is not alone in this interpretation. When I use Paint Shop Pro X to read the test.pdf or test.ps files from my tests of convert, the first thing that happens is that a dialog pops up with a default resolution of 72dpi which can then be accepted or changed if necessary.

If you mean that an input 1024x768@100dpi JPG image should *print* at the same size once it has been converted to PDF then you need to add "-resample 72x72 -units pixelsperinch" to your convert command. This will change the size of the image to 737x553 so that at the default resolution of 72dpi a program like PSP X will print it at the same size as it would the original JPG (10.24x7.68 inches).

Hope this hasn't muddied the waters any further.
Pete
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Post by anthony »

If the image in the postscript is a raster image that you are just trying to extract, then GrEeZ! Miro had worked out a solution. I have NOT trying this solution, just saved it for future review. If this helps let us know. If not prehaps a specific example may be needed.
Von: "Miro Dietiker, MD Systems" <miro.dietiker.maillist@md-systems.ch>
Datum: Fr, 1.09.2006, 19:59
An: magick-users@imagemagick.org (mehr)


OK here's my solution for the general EPS density problem:
INFO: I'm posting this to users & developers list of imagemagick since
this topic was already posted on users list :-)

It solves the following issue:

Consider a User has a EPS (single image, single boundingbox) image with
and wants this to be rendered to use inside ImageMagick

$filename is the filename of input
The EPS contains a boundingbox (in points)
and the user has a X and Y in pixels ($size_x_px, $size_y_px)
$density Is the resulting density

In this case: density is a directly derivated value.

I implemented the following code to parse the EPS, extract the bounding
box parts and calculate the optimum density.

Tested and works within my solution great.

The following source is PHP (and very fast written code)... ;-)

Code: Select all

$density = null;
$right = strtolower(substr($filename, -4));

if($right=='.eps')
  {
  # openfile
  $epsh = fopen($source, 'rb');
  $bboxarr = NULL;
  $found = FALSE;
  $i=0;
  $bufferend = '';
  while (!feof($epsh))
    {
    # pass to "%!PS-Adobe-3.0 EPSF-3.0"
    if(!$found)
      {
      $buffer = fread($epsh, 4096);
      #header will be fetched in junks which could be misaligned!
      if(($pos = strpos($bufferend.$buffer, '%!PS-Adobe-3.0
EPSF-3.0'))!==FALSE)
        {
        $i = 0;
        # position curser at comments begin!
        fseek($epsh, -(4096-$pos), SEEK_CUR);
        $found = TRUE;
        }
      if($i>200) { break; } #800k header is too much
      }
    else
      {
      $buffer = fgets($epsh, 1024);
      if($i>20) { break; } #take only some lines of comment
            # pass to "%%BoundingBox:" or "%%HiResBoundingBox:"
            if(strpos($buffer, '%%BoundingBox:')!==FALSE)
              { $bboxarr = split(' ', $buffer); }
            if(strpos($buffer, '%%HiResBoundingBox:')!==FALSE)
              {
              $bboxarr = split(' ', $buffer);
              # nothing more needed!
              break;
              }
      }
    $bufferend = substr($buffer, -25);
    $i++;
    }
  fclose($epsh);
  if($bboxarr!=NULL)
    {
    #Res[dpi] = image_x[pixels]/(bbox_x[points]/72) = 350
    $density_x = $size_x/($bboxarr[3]-$bboxarr[1])*72;
    $density_y = $size_y/($bboxarr[4]-$bboxarr[2])*72;
    #the smaller is dominating! longer side is being reduced
    $density = round(min($density_x, $density_y));
    }
  }
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
tongro

Post by tongro »

Anthony:

Thanks for your efforts, but as I said before, I'm reading
TIFF/JPEG and writing PS/PDF.

-----

El Supremo:

Thanks for that information; it's on the right track.

The TIFF/JPEG files are being produced by a document scanner. They must be processed differently depending on the filenames; if PS/PDF files are produced, they must appear the same size (typically A4, but could be any size) as when they were scanned. However, I would also like them to keep all the original detail if possible - they are typically scanned at 200 dpi, but could be any resolution. Reducing the resolution during the conversion obviously loses detail, but if that's unavoidable, so be it.

The approach I've ended up taking is as follows:

The reason I wanted to produce PS files was so that they could then be printed and both files deleted. I've now installed JPEG and TIFF filters in my printing system, so the files can instead be printed directly instead of going through ImageMagick first.

The PDF files are destined for the users' home directories. What I do now is to first use "identify $infile" and parse its output to get the resolution. If the resolution is available, I use "convert -density ${xdpi}x${ydpi} -units pixelsperinch $infile $outfile.pdf". If the resolution is not defined, I simply use "convert $infile $outfile.pdf". This system gives satisfactory results.

-----


Thanks a lot.

Tony.
Post Reply