vectorize a QR code

Post questions on how to use or achieve an effect in Inkscape.
barjak
Posts: 3
Joined: Tue Oct 16, 2012 7:52 pm

vectorize a QR code

Postby barjak » Tue Oct 16, 2012 8:09 pm

Hi,

I am trying to convert a bitmap image of a QR code (2D barcode) to a vector graphics. I regularly use the the "vectorize" feature of Inkscape with excellent results, for many kind of images. But for that one, I can't find the right configuration.

Here is an example of my attempts :

vectorize_qrcode.png
one of my attempts
vectorize_qrcode.png (103.58 KiB) Viewed 6041 times


Notice that the preview is exactly the result I want, but when the vectorization is applied for real the result is different.

Thanks for any advice.
Attachments
patiencedeau_barcode_reunion.gif
the original bitmap
patiencedeau_barcode_reunion.gif (923 Bytes) Viewed 6041 times

User avatar
brynn
Posts: 10309
Joined: Wed Sep 26, 2007 4:34 pm
Location: western USA
Contact:

Re: vectorize a QR code

Postby brynn » Wed Oct 17, 2012 12:16 am

Image
Welcome to InkscapeForum!

You can probably improve the trace by changing some settings on the Options tab. For example, try deselecting Smooth Corners altogether. Also, maybe put Optimize Paths tolerance as high as it will go. I'm not sure if you need to use Suppress Speckles. I don't know if it has any effect on the trace, if there are no speckles to be suppressed, but you could try deselecting it, to see if it helps. Also, I think I'd probably go with a single scan option, on the Mode tab -- probably Brightness Cutoff. But note that the Preview does not Update for that option. Instead, you have to do a trace, then if you need to change the Threshold, Undo, and trace again.

Having said all that though, if it's a tiny, postage stamp sized image, you might not be able to get a really clean trace. There's an online vector trace here http://www.roitsystems.com/cgi-bin/autotrace/tracer.pl. With some images, I've been able to get a better trace using it. It works very differently, so there's a bit of trial and error learning what all the settings mean. But in some cases, I've gotten a cleaner scan than what I could get with Trace Bitmap.

Anyway, good luck. I hope some of that helps. Let us know how it goes :D

barjak
Posts: 3
Joined: Tue Oct 16, 2012 7:52 pm

Re: vectorize a QR code

Postby barjak » Wed Oct 17, 2012 12:47 am

Some details on what I tried :
    - Brightness cutoff, threshold=1, invert image : the preview is what I expect, but when I click OK nothing happens (no shape is created)
    - Edge detection : not appropriate
    - Color quantization : not appropriate
    - Brightness steps, scans=2, smooth disabled : the preview is what I expect, but the result is smoothed
    - Colors, scans=2, smooth disabled : the preview is what I expect, but the result is smoothed
    - Grays, scans=2, smooth disabled : the preview is what I expect, but the result is smoothed
For the mode that are ok in preview, I tried every combination of options : "smooth corners" and "optimize paths" set to 0, 1, enabled and disabled. The result is either made of smooth curves or of triangles (no squares)

The original bitmap is a clean GIF. The file is attached to my post if anyone wants to try.

User avatar
brynn
Posts: 10309
Joined: Wed Sep 26, 2007 4:34 pm
Location: western USA
Contact:

Re: vectorize a QR code

Postby brynn » Wed Oct 17, 2012 3:05 am

The best scan I could make is attached. I think it's just too small to be able to pick up details. I don't know if you might be able to scale it larger and get a better scan. I know it will pixelate if you scale it larger, but you might be able to overcome the pixelation by tweaking the trace settings. But I don't know. I think it just might be mission impossible on this one.

Thinking outside the box.....it might not be hard to draw this image with Inkscape. Those tiny squares are exactly 2 square pixels. I've snapped the whole thing to a pixel grid! It would just be a matter of making squares that size, adding the proper color, and aligning to the grid. Just a thought :D

User avatar
heathenx
Posts: 388
Joined: Thu Jun 07, 2007 11:51 pm
Location: USA
Contact:

Re: vectorize a QR code

Postby heathenx » Wed Oct 17, 2012 3:34 am

Why trace it at all? Grab yourself an Inkscape QR Code plugin here: http://bit.ly/M1gbYY

Once installed, re-create the qr code and you'll have lovely vectors. Here's an example: qr_code

User avatar
RobA
Posts: 335
Joined: Fri Aug 10, 2007 1:22 am

Re: vectorize a QR code

Postby RobA » Wed Oct 17, 2012 4:14 am

if you really want to create it in svg, you can use tiled clones:

2012-10-16_135712.jpg
2012-10-16_135712.jpg (186.24 KiB) Viewed 5986 times


The only "trick" is the initial clones can't be the size of the QRcode pixels when using the "trace colour" option in the tiled clones dialog, as it samples the perimeter and you don;t end up with B&W clones.

To do this I created a 2x2 rectangle (as the source image had 4 px blocks in the QR code) which was centered in the upper left QR code block, and fill "unset". I then created the tiled clones, tracing the source image with colour, and added a 100% offset to the X and Y, to line up with the 4px QRcode grid. The next step was to select the original square, and change it's size to 4x4, which changes all the squares to 4x4, to get rid of the spaces. The last thing I did (optional, really) was to unlink the clones, then select all the black squares (using the Find dialog) and union them into one path, select all the white squares and delete them, and draw one large white square the size of the entire QRcode for the background.

-Rob A>

User avatar
druban
Posts: 1917
Joined: Fri Nov 20, 2009 10:48 pm

Re: vectorize a QR code

Postby druban » Wed Oct 17, 2012 9:03 am

all of these suggestions are great but there is no reason you can't do this in Inkscape with fair to excellent accuracy.

But your bitmap is just a bit (actually, several bits :D )too small.
Simply scaling it in Inkscape does not do anything - this is the common mistake made by users trying to increase the size of their trace object before tracing. Scaling it up, if you look at the info bar, does not change the pixel dimension, simply its onscreen size. What you need to do is to scale it up and THEN MAKE A BITMAP COPY. You will now have a bitmap with a higher pixel dimension! Using this as the trace source will give you better - or at least different - results.
You can even make the bitmap copy more accurate by making sure the original is aligned to a pixel grid before calling 'make bitmap copy', although I did not do this for this example, being of a lazy and shiftless character.

This image shows the result, with the vector path scaled back down to the size of the original bitmap.

rect3284.png
rect3284.png (9.95 KiB) Viewed 5970 times


Note that it is not absolutely exact. The slight deviation comes from the inaccuracy in the make bitmap copy process, NOT in the trace!

barcode2512.svg
(107.8 KiB) Downloaded 287 times

If you open the SVG file you will find:
1. The original bitmap,
2. The larger pixel dimension bitmap copy,
3. The red path - a trace run on this copy, and
4. the blue path - a copy of the red path scaled down to examine how well it fits to the original.
You can see by moving things around that the red path is a precise fit to the bitmap it traced, and that the blue path is close but not exactly a match to the original QR code image.

As for the settings, i used Brightness cutoff, the logical choice for a high contrast source, and a threshold setting of 0.450, although any setting from 0.05 to 0.95 yielded the same number of nodes. Uncheck everything else and don't give in to the temptation to simplify the resulting path!

The number of nodes was also unchanged by the size of the bitmap copy after making it about twice the size of the original, so bitmap copies from 169 to 5200 pixels square all yielded 2512 nodes!

The result should be perfectly usable, but if better accuracy is desired it would be advisable to scale the bitmap up in a raster editing program like the GIMP, where you could also reduce the bitdepth of the image to 1 bit (black and white).
Your mind is what you think it is.

User avatar
brynn
Posts: 10309
Joined: Wed Sep 26, 2007 4:34 pm
Location: western USA
Contact:

Re: vectorize a QR code

Postby brynn » Wed Oct 17, 2012 10:11 am

Wow druban, that's brilliant! When you did Make a Bitmap Copy, did you increase the dpi? I'm a little confused about "higher pixel dimension". Also, didn't it pixelate when you scaled it? Aaaackh! I'll have to look at your SVG file.... Image

Edit
:o :!: Ok, why doesn't it pixelate when you scale the GIF??? I knew the problem was that it was too small. I even said that. I even thought of scaling it, but rejected that plan because I expected it should become pixelated when scaled. I even tried snapping to pixel grid to see if the trace would work better. Why doesn't it pixelate???

Edit #2
Ooooohhh!! It doesn't pixelate because everything is perfectly vertical and perfectly horizontal. Wow -- Just brilliant! Image . Image

~suv
Posts: 2272
Joined: Sun May 10, 2009 2:07 am

Re: vectorize a QR code

Postby ~suv » Wed Oct 17, 2012 7:20 pm

Image
A sample SVG version [*] converted from the original, unedited GIF image with pixel2svg v0.3, using a square size of 4 px.

One might want to cleanup the result (remove the white squares which currently add an opaque background), etc., depending on the plans to further process the vectorized image.

JFYI: The next major version of Inskcape will include a QR code generator extension…

barjak
Posts: 3
Joined: Tue Oct 16, 2012 7:52 pm

Re: vectorize a QR code

Postby barjak » Wed Oct 17, 2012 9:17 pm

Thanks for all these answers.

Heathenx, next time I want to embed a QR in an SVG document, I'll give the QR generator a try ! Actually, I already had a QR bitmap in hand, so it was natural for me to try to vectorize it.
Resizing the original bitmap in an external editor (with no antialiasing, of course) works well. (or using the "make bitmap copy" feature, as druban said).
~suv, pixel2svg can be quite handy too, thanks for sharing the link.

Interresting fact : the QR code of my first attempts (the one with the curves) is well recognized by my smartphone ! It outputs the right URI !


Return to “Help with using Inkscape”