Linux-Fan

changed bin2bmp.py

Feb 13th, 2012
163
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/usr/bin/python
  2. # -*- coding: utf-8 -*-
  3.  
  4. copyright = """
  5. Modification notice:
  6.  
  7.     This is a modified version of the original script.
  8.     This modification took place in 2012 by the Ma_Sys.ma.
  9.     For further info send an e-mail to Ma_Sys.ma@web.de.
  10.  
  11.     Small changelist
  12.      * Removed JPG and BMP support
  13.      * Changed the programs' structure
  14.      * Added TGA support
  15.      * Wrote a function to restore original data from images
  16.        created by this program.
  17.      * Marked "monochrome" option as not implemented because
  18.        it did not work on the test system
  19.  
  20.     bin2bmp.py, modified 0.1.6.4 -- a small program to convert
  21.     binary data to bitmaps for graphical analysis
  22.  
  23.     This program is free software: you can redistribute it and/or modify
  24.     it under the terms of the GNU General Public License as published by
  25.     the Free Software Foundation, either version 3 of the License, or
  26.     (at your option) any later version.
  27.  
  28.     This program is distributed in the hope that it will be useful,
  29.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  30.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  31.     GNU General Public License for more details.
  32.  
  33.     You should have received a copy of the GNU General Public License
  34.     along with this program. If not, see <http://www.gnu.org/licenses/>.
  35.  
  36. Original copyright notice:
  37.  
  38.     bin2bmp.py 0.1.6.4 Copyright (C) 2009 Bryan Harris
  39.  
  40.     bin2bmp.py is free software; you can redistribute it and/or modify
  41.     it under the terms of the GNU General Public License as published by
  42.     the Free Software Foundation; either version 2 of the License, or
  43.     (at your option) any later version.
  44.  
  45.     bin2bmp.py is distributed in the hope that it will be useful,
  46.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  47.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  48.     GNU General Public License for more details.
  49.  
  50.     You should have received a copy of the GNU General Public License
  51.     along with bin2bmp.py; if not, write to the Free Software
  52.     Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  53. """
  54.  
  55. import Image, os, sys, array, getopt, struct
  56.  
  57. class bin2bmp:
  58.  
  59.     output_file = None
  60.     input_file = None
  61.     output_type = "PNG"
  62.     width = 1200
  63.     bpp = 24
  64.     create = True
  65.     filename = None
  66.     monochrome = False
  67.  
  68.     def usage(self, filename):
  69.         print "\nUsage:", filename, "options followed by values (if required)\n"
  70.         print " -i, --input       [file]    Input filename (required)"
  71.         print " -w, --width       [pixels]  Width of output image (default is " + str(self.width) + ")"
  72.         print " -o, --output      [file]    Output filename (default is input_filename.png)"
  73.         print " -d, --depth       [bits]    Set color depth (24 default, 32 also possible)"
  74.         print " -v, --version               Display version and license information"
  75.         print " -m, --monochrome            Set to one bit per pixel mode (black and white)"
  76.         print " -r, --restore               Restore input file a to binary file"
  77.         print " -t, --tga                   Write TGA instead of PNG images\n"
  78.         print "Example:", filename, "-i test.bin -o test.tga -t\n"
  79.         exit(1)
  80.  
  81.     def parse_args(self, argv):
  82.         try:
  83.             (opts, args) = getopt.getopt(argv[1:], "w:ho:i:mvrtd:",
  84.                 ["width=", "help", "output=", "input=", "monochrome", "version", "restore", "tga", "depth="])
  85.         except getopt.GetoptError, err:
  86.             print "Unable to parse arguments:", str(err)
  87.             self.usage(argv[0])
  88.  
  89.         for (o, a) in opts:
  90.             if o in ("-v", "--version"):
  91.                 print copyright
  92.                 exit(0)
  93.             elif o in ("-w", "--width"):
  94.                 try:
  95.                     self.width = int(a)
  96.                 except:
  97.                     print a, "is not an integer!"
  98.                     self.usage(argv[0])
  99.             elif o in ("-h", "--help"): self.usage(argv[0])
  100.             elif o in ("-o", "--output"): self.output_file = a
  101.             elif o in ("-i", "--input"): self.input_file = a
  102.             elif o in ("-t", "--tga"): self.output_type = "TGA"
  103.             elif o in ("-m", "--monochrome"): self.monochrome = True # TODO IMPLEMENT
  104.             elif o in ("-d", "--depth"): self.bpp = int(a)
  105.             elif o in ("-r", "--restore"): self.create = False
  106.             else:
  107.                 print "Invalid arguments given"
  108.                 self.usage(argv[0])
  109.  
  110.         if self.input_file == None:
  111.             print "No input filename specified."
  112.             self.usage(argv[0])
  113.  
  114.         self.filename = os.path.split(self.input_file)[1]
  115.  
  116.     def create_image(self):
  117.         try:
  118.             fileobj = open(self.input_file, "rb")
  119.         except:
  120.             print "Can't open " + sys.argv[1] + " for some reason."
  121.             exit(2)
  122.         try:
  123.             #TODO Make this work for bigger files...
  124.             buf = array.array("B", fileobj.read())
  125.             buf.reverse()
  126.         except:
  127.             print "Could not read file into buffer. File size too big?"
  128.             exit(2)
  129.  
  130.         output_ext = self.output_type.lower()
  131.  
  132.         if self.monochrome == False:
  133.             white = (255, 255, 255)
  134.         else:
  135.             print "Monochrome not implemented yet."
  136.             exit(3)
  137.             #white = 1
  138.  
  139.         chunks = (len(buf) * 8) / self.bpp
  140.         if chunks % self.width == 0:
  141.             height = chunks / self.width
  142.         else:
  143.             height = chunks / self.width + 1
  144.  
  145.         img = Image.new("RGB", (self.width, height), white)
  146.  
  147.         for i in range(chunks - 1):
  148.             x = i % self.width
  149.             y = i / self.width
  150.  
  151.             color = (buf.pop(), buf.pop(), buf.pop())
  152.  
  153.             #TODO Does not work correctly... ?
  154.             #if i % 8 == 0:
  155.             #   color_byte = buf.pop()
  156.             #   color = 1 - (color_byte >> (7 - i) % 8 & 1)
  157.  
  158.             img.putpixel((x, y), color)
  159.  
  160.         if self.output_file == None:
  161.             self.output_file = self.filename + "." + output_ext
  162.  
  163.         img.save(self.output_file, self.output_type)
  164.  
  165.     def restore_from_image(self):
  166.         """Restore a binary file from an image"""
  167.  
  168.         if self.output_file == None:
  169.             self.output_file = self.filename + ".bin"
  170.  
  171.         img = Image.open(self.input_file)
  172.         data = img.getdata()
  173.         buf = file(self.output_file, "wb")
  174.  
  175.         for i in data:
  176.             buf.write(struct.pack("BBB", i[0], i[1], i[2]))
  177.  
  178.         buf.close()
  179.  
  180. if __name__ == '__main__' :
  181.     obj = bin2bmp()
  182.     obj.parse_args(sys.argv)
  183.     if obj.create == True:
  184.         obj.create_image()
  185.     else:
  186.         obj.restore_from_image()
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×