# Converting image into graphics

I have a black-white image, but it is rather blurry so I was thinking of converting it into vector graphics so I could do some coloring. This is the image:

img = Import["https://i.stack.imgur.com/cyHjC.jpg"];


I used Binarize with ImageGraphics, but the result is not so good:

img2 = Binarize[ImageApply[If[Mean@# < 0.9, #, {1, 1, 1}] &, img], 0.8]
ImageGraphics[Erosion[img2, 1], 2, Method -> {"LinearSeparable", 10}]


As you can see, the lines are pretty rough with weird curves here and there.

ImageGraphics tends to deal with much more complicated stuff, so I thought maybe it is possible to optimize this with the knowledge that this is a black and white image with only the lines required. But how?

• Could you post the image here directly? – Carl Lange Oct 5 '18 at 11:53
• @CarlLange edited, thanks. – t-smart Oct 5 '18 at 12:08
• It doesn't look very blurry to me. What kind of coloring is it that you want to do? – C. E. Oct 5 '18 at 12:53
• Hm. I usually don't suggest to use external programs but I've just tried it with Inkscape (import the image, go to "Path" -> "Trace bitmap", and use "Brightness cutoff" 0.8). It was easy as cake. – Henrik Schumacher Oct 5 '18 at 14:10
• @VitaliyKaurov eye of agamotto, from doctor strange – t-smart Oct 6 '18 at 1:15

Your image is too small, so post-processing algorithms run into a lot of errors as initial information is tiny. Right form the start significantly resize image (upsample) trying to "guess" what info in the image is missing. As you are using original image

i = Import["https://i.stack.imgur.com/cyHjC.jpg"]


to do this before post-processing ImageResize will do a great job as it will have all available info. The lesson here is: do not upsample after some post-processing stages as some info is getting lost during these operations. BTW ImageResize generally upsamples nicely in my experience. Now both Binarize and ImageGraphics will have much more "room" to work and you get a very high quality vector:

ImageGraphics[Binarize[ImageResize[i, 2000], .8],2,Method -> "Exact"]


You can export it into a vector format

Export["vector.SVG", vector]


that you can see below. Now you can play further various improvement routes found in Image Restoration and other guides, but the key point is to increase the image resolution from the start.

• Thanks a lot, that's totally what I was expecting – t-smart Oct 6 '18 at 1:17
• You can show svgs on Stack Exchange, try to put e.g. <img src="https://svgshare.com/i/8b_.svg" /> in your post. – C. E. Oct 6 '18 at 8:13
• @C.E. thank you, done! – Vitaliy Kaurov Oct 6 '18 at 9:10