NOGDUS

Articles, Tutorials, and other things. => General Programming => : Richard Marks August 12, 2008, 06:26:02 AM



: Converting PNGs to BMPs on Linux using Ruby and ImageMagick
: Richard Marks August 12, 2008, 06:26:02 AM
WARNING! WARNING! WARNING! WARNING!

ImageMagick has a flaw in that it creates BMP4 format bitmaps when converting from PNG!
These are USELESS TO JUST ABOUT ANY PROGRAM! Gimp, Windows, Allegro cannot read them..wTF!@



PNG is better than BMP! Why would someone do this? Well I just so happened to need to do this exact task.
Because Allegro cannot use PNG files "out the box" and I did not feel like implementing the PNG library needed.
I needed to convert a LOT of PNG files into BMP files to be used in Allegro.
I made this simple one-liner script in about 2 minutes (1 minute to code 1 minute to test before using)

Png2Bmp.rb
:
#!/usr/bin/env ruby
# a tiny one-liner Ruby script to convert all png files in the current folder into bmp
# files in a sub folder called "bitmaps" assumed to be precreated.
# created by Richard Marks ccpsceo@gmail.com
`ls *.png`.split.each do |n| s='./bitmaps/'+n[0..-5]+'.bmp'; `convert #{n} #{s}`; end

This script assumes 3 things.

1. You have Ruby installed
2. You have ImageMagick installed
3. You have pre-created the bitmaps folder in the current folder

Have fun!

Oh and yes you can use this same one-liner to convert lots of other formats (anything ImageMagic can convert just by modifying a few things.

For example, here is the reverse of the script above. It converts BMPs into PNGs in a "pngs" sub folder.

Bmp2Png.rb
:
#!/usr/bin/env ruby
# a tiny one-liner Ruby script to convert all bmp files in the current folder into png
# files in a sub folder called "pngs" assumed to be precreated.
# created by Richard Marks ccpsceo@gmail.com
`ls *.bmp`.split.each do |n| s='./pngs/'+n[0..-5]+'.png'; `convert #{n} #{s}`; end

Presto! 8)


: Re: Converting PNGs to BMPs on Linux using Ruby and ImageMagick
: Richard Marks August 12, 2008, 06:51:25 AM
 >:( oh the irony is that I cannot use any of the converted BMPs since the damned Image Magick creates POORLY FORMATTED BITMAP FILES!

Damnit I'll need to figure out a way around this.


: Re: Converting PNGs to BMPs on Linux using Ruby and ImageMagick
: RedSlash November 01, 2008, 05:06:13 PM
:
$ sudo apt-get install netpbm

All PNGs -> BMP

:
$ for i in *.png; do pngtopnm "$i" | ppmtobmp > `basename "$i" .png`.bmp; done

The NetPBM (http://netpbm.sourceforge.net/) tools are mainly used to convert from one image format to another. It supports about 100 image formats.
Just check the man pages for each tool for more options and usage info!


: Re: Converting PNGs to BMPs on Linux using Ruby and ImageMagick
: Richard Marks November 01, 2008, 05:11:33 PM
Thanks! I'll try it out soon.


Sorry, the copyright must be in the template.
Please notify this forum's administrator that this site is missing the copyright message for SMF so they can rectify the situation. Display of copyright is a legal requirement. For more information on this please visit the Simple Machines website.