NOGDUS $1670.00 has been donated to NOGDUS!
September 20, 2017, 08:34:32 PM *
Welcome, Guest. Please login or register.

Login with username, password and session length
 
   Home   Help Search Login Register  
Pages: [1]   Go Down
  Print  
Author Topic: Converting PNGs to BMPs on Linux using Ruby and ImageMagick  (Read 5084 times)
0 Members and 1 Guest are viewing this topic.
Richard Marks
Administrator
Offline Offline

Respect: 3425
« 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! Cool
Logged

Richard Marks
Administrator
Offline Offline

Respect: 3425
« Reply #1 on: August 12, 2008, 06:51:25 AM »

 Angry 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.
Logged

RedSlash
Offline Offline

Respect: 10
« Reply #2 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 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!
Logged
Richard Marks
Administrator
Offline Offline

Respect: 3425
« Reply #3 on: November 01, 2008, 05:11:33 PM »

Thanks! I'll try it out soon.
Logged

Tags: ruby image conversion scripting utility programming 
Pages: [1]   Go Up
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.21 | SMF © 2015, Simple Machines
.: Theme by Richard Marks :.
Valid XHTML 1.0! Valid CSS!