Converting PNG to vector, scaling, and back to png.

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
gborbonus
Posts: 33
Joined: 2014-07-01T06:53:30-07:00
Authentication code: 6789

Converting PNG to vector, scaling, and back to png.

Post by gborbonus »

Hey Guys,

I've been trying to convert a png at original scale to a vector image, so I can resize it, maintaining the aspect ration.

The conversion is smooth, though it turns out black and white(with grayscale).

So, I was wondering,

would it be possible to convert to a vector image, resize, then using the original image as a template, put the color back in place?

Any thoughts?
Thank you,
Greg Borbonus
*nix system administrator
http://linkedin.com/in/gregborbonus
Skype: greg.borbonus
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Converting PNG to vector, scaling, and back to png.

Post by snibgo »

(You do realize that "-resize" by default does maintain the aspect ratio?)

In a word: yes.

I use potrace for vectorizing raster images. This works on black/white images, so the first step is to reduce the number of colours in the image to a small number (eg tens or hundreds, not millions). Then a script walks through the colours, making the image black/white, and adjusting the SVG file created by potrace to give the original colour. Then the SVG files can be combined into one, or each one rasterized and then the raster images merged.

My script isn't ideal because each border between colours will be in two SVGs, one for each colour, and these borders don't match exactly. For that reason, and because I was distracted by something else, I haven't written it up.
snibgo's IM pages: im.snibgo.com
gborbonus
Posts: 33
Joined: 2014-07-01T06:53:30-07:00
Authentication code: 6789

Re: Converting PNG to vector, scaling, and back to png.

Post by gborbonus »

LMAO, I thought you would be the one to reply.

And I must have had a brain fart, I don't mean aspect ratio, I mean image quality.

I was thinking if you could overlay the image on top of the traced svg, and blend them together, minimizing alias, and using the svg as the template.

but your way may work better.

Would it be possible for me to see this script and see if I can adapt it to my needs?
Thank you,
Greg Borbonus
*nix system administrator
http://linkedin.com/in/gregborbonus
Skype: greg.borbonus
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Converting PNG to vector, scaling, and back to png.

Post by snibgo »

For graphics images, you probably don't need the initial blur. I reduce to at most 9 colours, but choose any number. The for-loop reads through the colours. For each one, it makes an SVG file, then makes a PNG file from that. At the end, it flattens all the PNG files together.

Windows BAT script:

Code: Select all

set SRC=\prose\pictures\st_src_sm.jpg

set PNG_LIST=

%IM%convert ^
  %SRC% ^
  -blur 0x4 ^
  +dither ^
  -colors 9 ^
  -write x0.png ^
  -unique-colors txt:tCOLS.txt


for /F "usebackq skip=1 tokens=3 delims= " %%C in (tCOLS.txt) ^
do (
  echo %%C

  %IM%convert ^
    x0.png ^
    +transparent %%C ^
    -fill Black -opaque %%C ^
    -background White -flatten ^
    x.pnm

  %POTRACEDIR%potrace --turdsize 0 --color %%C -s -o x_%%C.svg x.pnm

  %IM%convert ^
    -background None x_%%C.svg ^
    -background %%C -alpha background ^
    x_%%C.png

  set PNG_LIST=!PNG_LIST! x_%%C.png
)

%IM%convert %PNG_LIST% -background None -flatten xs.png
I don't know if this might be useful to you. I suspect that tweaking the usual "-resize" or one of the other processes would be more promising. Perhaps you can put up a sample input/output and say how you want the output improved.

(I have to go out now, so don't expect me to respond for 10 hours or so!)
snibgo's IM pages: im.snibgo.com
gborbonus
Posts: 33
Joined: 2014-07-01T06:53:30-07:00
Authentication code: 6789

Re: Converting PNG to vector, scaling, and back to png.

Post by gborbonus »

anyway I could get a bash version?

I'm having trouble figuring out what usebackq is supposed to do.
Thank you,
Greg Borbonus
*nix system administrator
http://linkedin.com/in/gregborbonus
Skype: greg.borbonus
gborbonus
Posts: 33
Joined: 2014-07-01T06:53:30-07:00
Authentication code: 6789

Re: Converting PNG to vector, scaling, and back to png.

Post by gborbonus »

ok, I've adapted your code to bash and been working with it.

the SVG is perfect conversion and overlaying them is working out nicely. I removed the blur entirely, I want to make the image as clear as possible.

I'm working on a way to resize the SVG via pixels
Thank you,
Greg Borbonus
*nix system administrator
http://linkedin.com/in/gregborbonus
Skype: greg.borbonus
gborbonus
Posts: 33
Joined: 2014-07-01T06:53:30-07:00
Authentication code: 6789

Re: Converting PNG to vector, scaling, and back to png.

Post by gborbonus »

I've been working back and forth on this for the last few hours.

Here's what I got so far(it's not cleaned up):

Code: Select all

SRC=$1
SRC=$1
PNG_LIST=
RESIZE=
OVERLAY=

if [ "$2" != "" ]; then
  W=$(echo $2|cut -d'x' -f1);
  H=$(echo $2|cut -d'x' -f2);
  RESIZE=" -w $W -h $H";
fi

convert \
  $SRC \
  -background white \
  -flatten \
  -colors 16 \
  -write x0.png \
  -unique-colors txt:tCOLS.txt


#Make sure we're dealing with Unique results.

for C in `cat tCOLS.txt|grep -v '^#'|cut -d')' -f2|awk '{print $1}'|sort|uniq`; do 
  echo ${C}

    convert \
    x0.png \
    +transparent ${C}  \
    -fill Black  -opaque ${C} \
    -background White -flatten \
    -resize 500% \
    +antialias \
    x_${C}.pnm

    potrace --turdsize 0 -z black --color ${C} -s -n -o x_${C}.svg x_${C}.pnm
    inkscape -z -e x_${C}_resized.png $RESIZE x_${C}.svg
    #Blur it out heavy so the overlay will cut off the image cleanly.
   if [ "${C}" = "#FFFFFF" ]; then
      $OVERLAY="x_${C}.png";
    convert \
    -background None x_${C}_resized.png \
    -background ${C} -alpha background +antialias\
    x_${C}.png 
  else
    convert \
    -background None x_${C}_resized.png \
    -background ${C} -alpha background +antialias\
    x_${C}.png
   fi
  PNG_LIST=${PNG_LIST}" x_${C}.png"

done

convert ${PNG_LIST} $OVERLAY -background white -transparent white -flatten xs.png
It comes out with the right shape, but there are pixels out of place all over it.

TEST image:

Image

OUTPUT:
Image

transparency and white background, not an issue.

both are png, but was converted to jpg at mediafire
Thank you,
Greg Borbonus
*nix system administrator
http://linkedin.com/in/gregborbonus
Skype: greg.borbonus
gborbonus
Posts: 33
Joined: 2014-07-01T06:53:30-07:00
Authentication code: 6789

Re: Converting PNG to vector, scaling, and back to png.

Post by gborbonus »

ok, so I tested it without the scaling up 500%, which was done to correct, it came out looking a bit worse.

Is there anyway to bleed close pixels? Or, in your example you limit the colors to 9, I assume thats to keep images with many colors limited to only their base color, like dark grey should be black and light gray should be white.

Is it possible to have identify or convert tell you the base colors it's using, or the closest match?
Thank you,
Greg Borbonus
*nix system administrator
http://linkedin.com/in/gregborbonus
Skype: greg.borbonus
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Converting PNG to vector, scaling, and back to png.

Post by snibgo »

I don't know what you mean about "pixels out of place".

When rasterizing a vector image, a greater sampling density will reduce jaggies and improve the result. For Inkscape, use ...

Code: Select all

--export-dpi="1200,1200"
... or whatever you want.

I used "9" because that suited my sample image. IM should choose the best colours, up to 9 of them. Or whatever number you give.

I don't know what you mean by "base color". IM has told you what colours it has used. They are in tCOLS.txt.
snibgo's IM pages: im.snibgo.com
gborbonus
Posts: 33
Joined: 2014-07-01T06:53:30-07:00
Authentication code: 6789

Re: Converting PNG to vector, scaling, and back to png.

Post by gborbonus »

by "base color", I mean like black is the base color and anything that is close to black should be grouped as black, Adding the dpi switch has greatly improved things, but I still want to group the colors together better.

An image with only red blue and green only has 3 colors, until you take into consideration the aliasing the image does, the bleed into the background. so, for this image a color setting of 3 would be perect, but for an image with lets say 9 different colors, the 3 would not be enough.

so, what I'm wondering is, is it possible to group colors by a range of some sort, like analyze image, remove anything blending into the background, then get the base colors, and compare the colors to the original image, anything that is in the original and not in the modified gets grouped with the "base color" from the analyzed imaged.

Make sense?
Thank you,
Greg Borbonus
*nix system administrator
http://linkedin.com/in/gregborbonus
Skype: greg.borbonus
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Converting PNG to vector, scaling, and back to png.

Post by snibgo »

To get a list of all the colours, and how many pixels have each colour:

Code: Select all

convert in.png -format %c histogram:info:out.txt
Sort the output. A graphic image with mostly solid colours will have many pixels with some colours and only a few with other colours. You might read out.txt in a script and take note of any colours that are used in more than (say) 10% of the total pixels.

This tells you how many colours, and what those colours are. You can make an image with one pixel of each of those colours, and use that with "-remap" on the original image.
snibgo's IM pages: im.snibgo.com
gborbonus
Posts: 33
Joined: 2014-07-01T06:53:30-07:00
Authentication code: 6789

Re: Converting PNG to vector, scaling, and back to png.

Post by gborbonus »

I sort of gave up on this idea. Though I still think it's a good one, the trace system just does not trace it well enough for my project. I need a way of keeping the cleaner lines. I went with adaptive resize to get the best quality I could with dynamic images.

Thanks for the help though, I may come back and try to figure out a different approach.
Thank you,
Greg Borbonus
*nix system administrator
http://linkedin.com/in/gregborbonus
Skype: greg.borbonus
Post Reply