Open Technical Blog
Thursday, 13 November 2014
Save Image to File
The script shows some techniques in saving an image to a file. The image can be saved in jpeg as well as png formats.
Save a pixbuf to a file in jpeg format with various qualities.
Save a pixbuf to a file in png format with various compression levels.
New jpeg and png files with different qualities and compression levels will be created by this script.
Source Code
#!/usr/bin/python # Title : Convert Image 01 # Date : 13/11/2014 # Description : # 1. Read an image file and show it on in a window # 2. Save the image to new files in jpeg and png formats, # various qualities are supplied in these files # 3. The following operations are related to pixbuf which # can be used to manipulate images(jpeg or png) # # Create pixbuf from file # pixbuf = Pixbuf.new_from_file(filename) # Draw pixbuf to Gtk.DrawingArea # onDraw(self, pDrawArea, pCr): # Gdk.cairo_set_source_pixbuf(pCr, pixbuf, StartX, startY) # Crop pixbuf # pixbuf_new = Pixbuf.new_subpixbuf(pixbuf_old, # startX, startY, width, height) # Resize pixbuf # quality = # InterpType.NEAREST # fastest, lowest quality # InterpType.BILINEAR # best quality/speed balance # InterpType.HYPER # slowest, highest quality # # pixbuf_new = Pixbuf.scale_simple(pixbuf_old, # width, height, quality) # Save pixbuf to file # Pixbuf.savev(pixbuf, filename, 'jpeg', # ['quality'],['100']) # 100(High Quality)-0(Low Quality) # Pixbuf.savev(pixbuf, filename, 'png', # ['compression'],['0']) # 0(High Quality)-9(Low Quality) from gi.repository import Gtk, Gdk from gi.repository.GdkPixbuf import Pixbuf, InterpType class mainApp(): def __init__(self): self.win = Gtk.Window() self.win.connect('destroy', Gtk.main_quit) self.win.set_default_size(600,400) self.win.set_position(Gtk.WindowPosition.CENTER) self.win.set_title('Image Converter') mBox = Gtk.VBox(False,2) # not equal height, space # draw area self.drawArea = Gtk.DrawingArea() self.drawArea.connect("draw", self.onDraw) mBox.pack_start(self.drawArea,True,True,2) # control bar mHBox = Gtk.HBox(False,2) mBox.pack_end(mHBox,False,True,2) # save button self.butSave = Gtk.Button("Save") self.butSave.connect("clicked", self.onSave) mHBox.pack_start(self.butSave,True,True,2) # message label self.msg = Gtk.Label("Message") mHBox.pack_end(self.msg,True,True,2) # self.win.add(mBox) self.win.show_all() # load image self.pixbuf = None self.loadImg2Pixbuf('d6-03.jpg') def loadImg2Pixbuf(self, pFile): self.pixbuf = \ Pixbuf.new_from_file(pFile) def onDraw(self, pDrawArea, pCr): mFac = 0.5 mW = self.pixbuf.get_width() mH = self.pixbuf.get_height() # resize pixbuf to fit screen dimensions mquality = InterpType.HYPER mpixbuf1 = Pixbuf.scale_simple( self.pixbuf,mW*mFac,mH*mFac,mquality) # draw pixbuf on screen Gdk.cairo_set_source_pixbuf(pCr, mpixbuf1, 5, 5) pCr.paint() # message mstr = 'Image [W:%d, H:%d] ' % (mW,mH) self.msg.set_text(mstr) def onSave(self, widget): mFac = 0.5 mW = self.pixbuf.get_width() * mFac mH = self.pixbuf.get_height() * mFac mquality = InterpType.HYPER mpixbuf = Pixbuf.scale_simple( self.pixbuf,mW,mH,mquality) # save the image in jpeg with 4 qualities for mi in [20, 40, 70, 90]: mfile = 'sav-%s.jpg' % str(mi) Pixbuf.savev(mpixbuf,mfile, 'jpeg',['quality'],[str(mi)]) # save the image in png with 4 qualities for mi in [9, 5, 3, 1]: mfile = 'sav-%s.png' % str(mi) Pixbuf.savev(mpixbuf,mfile, 'png',['compression'],[str(mi)]) mApp = mainApp() Gtk.main()
No comments:
Post a Comment
Newer Post
Older Post
Home
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment