Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine

Re: GD @ 300dpi output

by Anonymous Monk
on Mar 27, 2016 at 09:41 UTC ( [id://1158860]=note: print w/replies, xml ) Need Help??

in reply to GD @ 300dpi output

BrowserUk has already said it, but for a computer, the specification "300dpi" by itself is useless, because it only tells us what density you want to print at. For it to be useful for the computer, you need to say something like "3000x2000 pixels printed at 300dpi" (which will result in a print of roughly 25.4x16.9cm), or "10x20cm at 300dpi", which is an image of 1181x2362 pixels. That same 1181x2362 pixel image can also be printed at 600dpi, in which case it will be 5x10cm, or at 150dpi, in which case it will be 20x40cm.

Anyway, maybe what you are asking about is the DPI information stored as part of the image headers. I have had success in changing it using Image::ExifTool, at least this changes the image's DPI reported by The Gimp.

use Image::ExifTool; my $DPI = 300; my $exif = Image::ExifTool->new; $exif->SetNewValue("XResolution",$DPI); $exif->SetNewValue("YResolution",$DPI); $exif->SetNewValue("ResolutionUnit","inches"); $exif->WriteInfo($FILE) or warn "Failed to update EXIF in $FILE";
Or on the command line:
exiftool -XResolution=300 -YResolution=300 -ResolutionUnit=inches file +.jpg
For the computer, this DPI information is relatively useless, because the computer only cares about how many pixels the image has. You can change this header all you want and the pixel data will not change, the image will not be resized to a different number of pixels. The DPI information will only become useful to a printer. But most printers that I know will always let you specify the DPI you want to print with as a separate setting. The only scenario I can imagine where the above will be useful to you is if you have some program or printer that does care about the DPI information stored in the image header.

Replies are listed 'Best First'.
Re^2: GD @ 300dpi output
by themcp (Novice) on Mar 27, 2016 at 16:13 UTC
    Yes! ... holy cow, finally a way to set the dpi value. Image::ExifTool is exactly what I needed - worked perfectly. Thank you for the recommendation and sample code.

    I had always assumed that GD would retain the source dpi value if you use a particular setting but simply couldn't find a way to have it set a desired value in the output file.

    The problem I was having is that when the batch job goes to send the image file to the printer, the dpi reduction changed the image to 35 inches wide instead of 8.5 and I'd have to go in and reset the dpi value in Photoshop every time. As BrowserUK said, I'm not completely in tune with how the conversions work so I appreciate the link to the explanation web page... will try to better understand the mechanics of dpi versus pixel counts going forward.

    And yep, jpg compression probably isn't the best format to use but it is just an office printer doing the work... didn't have to be photo quality. At 95% output it is pretty clear in the final print. May look into using png - I think that is lossless but it's been awhile since I've read up on it.

    Thanks again for the assistance and solution!
      Glad it helped :-)

      will try to better understand the mechanics of dpi versus pixel counts going forward.

      Here's a try: In the world of bitmaps, only the pixel counts matter. A bitmap has a width in pixels, and a height in pixels. "DPI", or "Dots Per Inch", is as the name says a measure of how dense the pixels are in the physical world, how many of them per inch are input/output. By itself, a bitmap does not have "DPI". Only your monitor, printer and scanner have a measure of DPI. The DPI information in the bitmap header is only a hint for output!

      So by itself, DPI is a measure of print quality only. It does not tell you anything about the size of the bitmap. Only in combination with a size in pixels or a size in inches or centimeters is it a measure of bitmap size. It's kind of like miles and mph, or kilometers and km/h - kilometers (DPI) are related to km/h (DPI+size), but they're measuring/expressing different things!

      When you're talking to a graphics designer or a printer, they're much more used to thinking in DPI. That's why you'll need to translate between the language of "X by Y pixels" and "Z DPI at X by Y cm"/"Z DPI at X by Y pixels" - try typing "10 inches at 300 dpi" or "3000 dots / 300 dpi" into Wolfram Alpha ("dots"="pixels"). The confusion starts happening when people forget to specify a size - a designer might tell you "I need the image at 300 DPI" and forget to tell you the dimensions of the image, or "I need the image to be A5" without telling you the DPI! In my experience this happens regularly, and you'll have to get used to asking for the missing information.

      Some bitmap processing tools try to be helpful to people who live in the world of DPI by specifying sizes with DPI and in/cm everywhere. They try to take into account the DPI measures of your monitor and printer, font sizes, etc. in order to try to present a world that can always be measured in inches or centimeters (and DPI). And when loading bitmaps into such software, it may take into account the DPI information stored in the bitmaps' headers. However, this world often clashes with the world of bitmaps, and people who only think in pixels - for example web designers, who are much more used to pixels.

      It can be confusing, kind of like the German tourists who are regularly questioned by US Customs as to whether they want to poison their relatives!

      Personally, I always just do everything in pixels, and convert to and from the language of "X by Y cm at Z DPI" only when talking to people who think in those terms.

      Graphics Department: "We need the image at 600 DPI."
      You: "Okay, what's the largest you want to print that at?"
      Graphics Department: "We're thinking 5 by 8 cm right now but we may make it as big as 10 by 16 cm."
      At this point you know you need to make the bitmap 2362 by 3780 pixels large, and that you may need to scale down the number of pixels later. Scaling down a photo is of course no problem at all, but if it contains small fonts or fine lines that won't survive the scaling down, you might need to make a second version of the bitmap.

      When handing off a bitmap back to the graphics department, you just set the "print size", i.e. the DPI information in the bitmap headers, using for example the "Print Size" menu item in Gimp's "Image" menu, or the method in the above post. This does not affect the pixels at all, only the "hint" in the bitmap's header as to what DPI it was intended to be printed at (and whoever or whatever is now handling the image can choose to use any other DPI value they want).

      If the graphics department comes to you later and says something like "the printer has asked for the image to be at 1200 DPI", then in my experience this issue can often be settled by simply scaling up the size of the bitmap to twice its size in pixels - even though because you've simply interpolated the image to a larger number of pixels, the quality of the image will be the same as a 600 DPI image, some printers just want a specific number of pixels per inch because that's what they're printing at (maybe the graphics department has specified that the texts on the page should be printed at 1200 DPI, and the image next to the text can't be printed at a different number of dots per inch). Only if the graphics department later complains about the quality of the image, do you have to tell them "sorry, you asked for 600 DPI and that's what you got, if you now really want 1200 DPI I'll have to redo the whole image".

      That's my brain dump on the topic, HTH!

        like the German tourists who are regularly questioned by US Customs as to whether they want to poison their relatives.
        It may be a bit tangential to the topic at hand, but could you expand a bit on that?

        I have absolutely no clue what you are referring to...

        Dang... tons of info lol. Thx again for the detail on this!

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1158860]
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (5)
As of 2024-06-19 11:02 GMT
Find Nodes?
    Voting Booth?

    No recent polls found

    erzuuli‥ 🛈The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.