NOGDUS

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



Title: Converting PNGs to BMPs on Linux using Ruby and ImageMagick
Post by: Richard Marks on 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
Code:
#!/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
Code:
#!/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)


Title: Re: Converting PNGs to BMPs on Linux using Ruby and ImageMagick
Post by: Richard Marks on 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.


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

All PNGs -> BMP

Code:
$ 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!


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