fukuchi / libqrencode

A fast and compact QR Code encoding library
https://fukuchi.org/works/qrencode/
GNU Lesser General Public License v2.1
2.53k stars 592 forks source link

Setting --dpi has no effect on the PNG output #210

Open pd3 opened 1 year ago

pd3 commented 1 year ago

In version 4.1.1 setting the -d, --dpi option has no effect.

Steps to reproduce:

$ qrencode -V
qrencode version 4.1.1
Copyright (C) 2006-2017 Kentaro Fukuchi

$ qrencode -d 72 -t PNG -o rmme.png "test"
$ identify rmme.png 
rmme.png PNG 87x87 87x87+0+0 8-bit sRGB 290B 0.000u 0:00.000

$ qrencode -d 300 -t PNG -o rmme.png "test"
$ identify rmme.png 
rmme.png PNG 87x87 87x87+0+0 8-bit sRGB 290B 0.000u 0:00.000
zertap commented 9 months ago

Seems to work for me

$ qrencode -V
qrencode version 4.1.1
Copyright (C) 2006-2017 Kentaro Fukuchi

$ qrencode -t PNG -d 72 -o d72.png "test"
$ qrencode -t PNG -d 300 -o d300.png "test"

$ identify -verbose d72.png | grep -A3 Geometry
  Geometry: 87x87+0+0
  Resolution: 28.34x28.34
  Print size: 3.06987x3.06987
  Units: PixelsPerCentimeter

$ identify -verbose d300.png | grep -A3 Geometry
  Geometry: 87x87+0+0
  Resolution: 118.11x118.11
  Print size: 0.736601x0.736601
  Units: PixelsPerCentimeter

https://fukuchi.org/works/qrencode/ In 3.2.0 version notes:

"--dpi" (or "-d") option has been added to qrencode. This option set DPI information in an output PNG image. (Thanks to David Dahl)

The man-page or help does not seem to be as specific, but the dpi setting is metadata only. It does not affect pixel size (geometry).

-d NUMBER, --dpi=NUMBER specify the DPI of the generated PNG. (default=72)

If you wanted to have a bigger pixel size instead to match the dpi (like I did), you can resample it with imagick

$ convert -units PixelsPerInch -interpolate Nearest -filter point -resample 300 d72.png d300-imagick-resample.png
$ identify -verbose d300-imagick-resample.png | grep -A3 Geometry
  Geometry: 363x363+0+0
  Resolution: 118.11x118.11
  Print size: 3.07341x3.07341
  Units: PixelsPerCentimeter

This yields the same "print size" as the 72 (default) dpi setting but with bigger pixel size. the -interpolate and -filter options are needed so the resampling does not make the image blurry. This does have the downside that the margins are also multiplied. Setting -m 0 or -m 1 with qrencode might be desired.

You can also just specify the desired pixel size : convert -interpolate Nearest -filter point -resize 500 d72.png 500px.png For full list of options with ImageMagick's -resize see https://imagemagick.org/script/command-line-options.php#resize


So, this is not really a bug but a feature request for setting the desired pixel size and scaling the qr-code accordingly as I see it.

pd3 commented 9 months ago

Mmm, okay. I will not question the usefulness of the --dpi option, but the only thing that ever mattered to me was the resolution of an image. That would be a useful addition!

Thank you for offering a workaround via imagemagick.