วันอังคารที่ 14 มีนาคม พ.ศ. 2560

Python กับ ฺBloggang

Python กับ ฺBloggang

เข้าเขียน blog บางที่มันมีบางอย่างที่ต้องทำซ้ำ ๆ เช่นการ ย่อขนาดรูป โดยใช้ code HTML
ตัวอย่าง มันก็ไม่ยากอะไร แต่อยากให้ชีวิตง่ายขึ้นก็เลยคิดว่า ลองเขียนโปรแกรม จัดการ
script ดูโดยใช้ python โดยไม่ว่าจะ input image ขนาด width เท่าไหร่ มันก็จะปรับ ให้เท่ากับ
width = 520 height =xxx ที่ใช้ตัวเลข 520 เพราะให้ขนาดภาพพอดีกับ page



ตัวอย่าง code
run on Ubuntu 10.04
#-----------------------------------------------Start----------------------------------------------------- #
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# image_info.py
#Script write under pythin 2.6 test on Ubuntu 10.04
#By Mr.Sompoch Thuphom
#E-mail mjko68@hotmail.com


import pygtk
pygtk.require('2.0')
import gtk, gobject
import sys,getopt,re,os
import string,time
import cStringIO # *much* faster than StringIO
import urllib
import Image,os


def image_connect(self):
global rem_file # global variable to be used in dlProgress
buf = view1.get_buffer()


try:
textlog ='Loading from URL : ' + entry1.get_text() + '\r\n'
rem_file = entry1.get_text()
file = urllib.urlopen(rem_file)
im = cStringIO.StringIO(file.read()) # constructs a StringIO holding the image
getInfo = file.info()
print file.info()
img = Image.open(im)
#print 'getInfo Server by = ' + getInfo[0]
# now use PIL
textlog =''
print img.format, img.size, img.mode
textlog = str(getInfo) + "\r\n File Format is " + img.format
print "Image width : " + str(img.size[0])
textlog = textlog + "\r\n Image size width = " + str(img.size[0])
print "Image height : " + str(img.size[1])
textlog = textlog + "\r\n Image size height = " + str(img.size[1])
buf.set_text(textlog)
entryWidth.set_text(str(img.size[0]))
entryHeight.set_text(str(img.size[1]))
except:
errText= 'Error while loading image url ' + entry1.get_text() + \
'\r\nplease check URL valid path or input file format incorrect!'
print errText
buf.set_text(errText)
#img.save('my_copy.gif')'''


def getHTML(self):
textHTML = '






'print textHTML


class Image_Info:


def show_dialog(self,widget,buttonok):
try:
DEF_PAD =20
label = gtk.Label('Copy')
dialog = gtk.Dialog("Summary of report",
None,
gtk.DIALOG_MODAL | gtk.DIALOG_DESTROY_WITH_PARENT,
(gtk.STOCK_CLOSE, gtk.RESPONSE_REJECT,
gtk.STOCK_OK, gtk.RESPONSE_ACCEPT))
dialog.vbox.pack_start(label,False,False,DEF_PAD)
label.show()
response = dialog.run()
dialog.destroy()
print 'show copy page html code'
except :
print 'Error found'


def auto_size(self,widget,view1):
buf = view1.get_buffer()
itr = buf.get_end_iter()
imgsize = float(entryWidth.get_text())
realSize = 520/imgsize
print type(realSize)
print 'value of number from divide = ' + str(realSize)
imgHsize = int(realSize * int(entryHeight.get_text()))
textHTML = '












\r\n\r\n'print textHTML
buf.set_text(textHTML)


getURLtest = entry1.get_text()
countSlash =0
url_filter = ''
for i in range(0, len(getURLtest)):
if getURLtest[i] =='/':
countSlash =countSlash +1
if countSlash==3:
url_filter = getURLtest[0:i] # Load URL Filter
break


sepText = entry3.get_text()
#print sepText.split('อ')


getFooter = "\r\n"+entry3.get_text()+" \r\n" \
+ url_filter + "
" + 'มากมาย ครับ
'


print getFooter


buf.insert_at_cursor(getFooter)




#getURLtest = getURLtest.split("/")[0:3]




def clear_text(self,widget,view1):
buf = view1.get_buffer()
#buf.add_selection_clipboard()
buf.set_text('')


def past_from_clibboard(self,widget,buttonPast):
# get the clipboard
clipboard = gtk.clipboard_get()
text = clipboard.wait_for_text()
if len(text) != 0:
print text
entry1.set_text(text)
def copy_to_clibboard(self,widget,buttonCopy):
citation_buffer = view1.get_buffer()
clipboard = gtk.clipboard_get("CLIPBOARD")
#citation_buffer.select_range(citation_buffer.get_start_iter(), citation_buffer.get_end_iter())
citation_buffer.get_selection_bound()
citation_buffer.copy_clipboard(clipboard)


def __init__(self):
# create a new window
window = gtk.Window(gtk.WINDOW_TOPLEVEL)
window.set_size_request(500, 300)
window.set_title("Bloggang Image Custom Size V0.1")
window.connect("delete_event", lambda w,e: gtk.main_quit())
window.set_border_width(10)


vbox = gtk.VBox(False, 5)
window.add(vbox)
vbox.show()


table = gtk.Table(4, 6, True)
table.set_row_spacings(7)
table.set_col_spacings(10)
#table.set_col_spacing(1, 10)
vbox.pack_start(table, False, False, 5)
table.show()


'''# Insert Table Row 1 for input box '''
label1 = gtk.Label("Image URL:")
label1.set_justify(gtk.JUSTIFY_RIGHT)
#button1 = gtk.Button("button 1")
table.attach(label1, 0, 1, 0, 1)
global entry1
entry1 = gtk.Entry()
entry1.set_max_length(200)
url_file ="http://freegee.sourceforge.net/FG_EN/src/teasers_en/t_gee-power_en.gif"
entry1.set_text(url_file)
table.attach(entry1, 1, 5, 0, 1)


buttonPast = gtk.Button("Past")
buttonPast.connect("clicked", self.past_from_clibboard,buttonPast)
table.attach(buttonPast,5,6,0,1)
label1.show()
entry1.show()
buttonPast.show()


'''*# Insert Table Row 2 for input box*** '''


labelWidth = gtk.Label("Width")
labelWidth.set_justify(gtk.JUSTIFY_RIGHT)
#button1 = gtk.Button("button 1")
table.attach(labelWidth, 0, 1, 1, 2)
labelWidth.show()


global entryWidth
entryWidth = gtk.Entry()
entryWidth.set_max_length(50)
entryWidth.set_text("345")
table.attach(entryWidth, 1, 2, 1, 2)


labelWidth = gtk.Label("Height") # Create label Height
labelWidth.set_justify(gtk.JUSTIFY_RIGHT)
table.attach(labelWidth, 2, 3, 1, 2) # Set position label
labelWidth.show() # Show for this label


global entryHeight
entryHeight = gtk.Entry()
entryHeight.set_max_length(50)
entryHeight.set_text("456")
table.attach(entryHeight, 3, 4, 1, 2)
entryHeight.show()


buttonLoad = gtk.Button("Load...") #show_folder_choose
buttonLoad.connect("clicked", image_connect)
table.attach(buttonLoad,5,6,1,2)
labelWidth.show()
entryWidth.show()
buttonLoad.show()


'''*# Insert Table Row 3 for input box*** '''
label3 = gtk.Label("Footter :")
label3.set_justify(gtk.JUSTIFY_RIGHT)
#button1 = gtk.Button("button 1")
table.attach(label3, 0, 1, 2, 3)
global entry3
entry3 = gtk.Entry()
entry3.set_max_length(150)
entry3.set_text("ขอขอบคุณ รูปภาพงาม ๆ จาก ")
#entry3.connect("button-press-event", self.show_calendar)
table.attach(entry3, 1, 5, 2, 3)
label3.show()
entry3.show()


'''*# Insert Table Row 4 for input box*** '''
label5 = gtk.Label("HTML Code:")
label5.set_justify(gtk.JUSTIFY_RIGHT)
table.attach(label5, 0, 1, 3, 4)
global view1
view1 = gtk.TextView();


sw = gtk.ScrolledWindow()
sw.set_shadow_type(gtk.SHADOW_ETCHED_IN)
sw.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)
sw.add(view1)


vbox.pack_start(sw, True, True, 0)
#table.attach(sw, 1, 5, 3, 7)
label5.show()
sw.show()
view1.show()
buf = view1.get_buffer()
buf.set_text('First to start text view')


# Create Button in Vbutton Box


bbox = gtk.HButtonBox()
vbox.pack_start(bbox, True, True, 0)
layout=gtk.BUTTONBOX_END
bbox.set_layout(layout)
bbox.set_spacing(5)
buttonok = gtk.Button(stock='gtk-copy')
buttonok.connect("clicked", self.copy_to_clibboard,buttonok)
bbox.add(buttonok)
buttonok.show()


buttonClear = gtk.Button(stock='gtk-clear')
bbox.add(buttonClear)
buttonClear.connect("clicked", self.clear_text,view1)
buttonClear.show()
bbox.show()


buttonClose = gtk.Button(stock='gtk-close')
buttonClose.connect("clicked", lambda w: gtk.main_quit())
bbox.add(buttonClose)
buttonClose.show()


'''*# Insert Table Row 5 for input box*** '''


buttonAuto = gtk.Button('auto size')
buttonAuto.connect("clicked", self.auto_size,view1)
table.attach(buttonAuto,5,6,2,3)
buttonAuto.show()


window.set_position(gtk.WIN_POS_MOUSE) #Show windo on Mouse position
window.set_resizable(False) # User can't resize of window
window.show()


def main():
gtk.main()
return 0


if __name__ == "__main__":
Image_Info()
main()

#-----------------------------------------------End----------------------------------------------------- #


Download Example Code Image_Info.py

ไม่มีความคิดเห็น:

แสดงความคิดเห็น

Set MongoDB in the windows path environment

  Let’s set MongoDB in the windows environment in just a few steps. Step 1: First download a suitable MongoDB version according to your mach...