Convert the list of images to PDF file and add watermark in Ubuntu

Tags

, , , ,

Converting list of images to PDF in Ubuntu
Let’s use¬†ImageMagick tool. Normally it is installed in Ubuntu. If you need to install it then run:

$ sudo apt-get install imagemagick

Then you can convert it by using:
$ convert image_1.jpg image_2.jpg output.pdf

You can specify any numbers of images but the last argument must be a name of your PDF file along with extension (.pdf).

Adding the watermark in the PDF file
We use Python script for adding watermark to each page in the PDF file. We will be using “PyPDF2” python library.

$ pip install PyPDF2

Make your watermark ready, convert it to PDF file. Make sure that watermark file is of same page size as of your PDF file.

Then run this code by modifying the file names:

import PyPDF2

originalFile = open('inputfile_pdf', 'rb')
pdfReader = PyPDF2.PdfFileReader(originalFile)
pdfWriter = PyPDF2.PdfFileWriter()

for i in range(0, pdfReader.numPages):
    originalfile_page = pdfReader.getPage(i)
    pdfWatermarkReader = PyPDF2.PdfFileReader(open('watermark.pdf', 'rb'))
    originalfile_page.mergePage(pdfWatermarkReader.getPage(0))
    pdfWriter.addPage(originalfile_page)

resultPdfFile = open('watermarked_outputfile.pdf', 'wb')
pdfWriter.write(resultPdfFile)
originalFile.close()
resultPdfFile.close()

Fix: ClockworkMod Recovery MD5 mismatch

Tags

Hello all, i was trying to restore my backup of the system image which is about 6 months older via ClockworkMod Recovery. But i got a error which says something like:

MD5 mismatch!

Then after searching over the internet i found out the solution to this. Hope this might be useful to you.

Solving the problem

In the backup image folder you will see several files including nandroid.md5. Open this file with text editor such as vim or any of your favorite editor.

Delete the content of the file and make it empty. Optionally if you have any problem editing the file and simply delete this file and make a new file with same name(nandroid.md5) and keep this in the back image folder.

So, by deleting the content of this file i was able to fix the problem. Hope you will be able to solve the problem with this method.

“Constitution of Nepal” Android App

Tags

It’s been so great to feel that the Constitution of Nepal has finally arrived. Congratulations to all the Nepalese. May this Constitution brings peace, prosperity and rapid development in the country is the wish of all the Nepalese.

On this occasion with the intention of easy distribution of Constitution and making available of Constitution in the hand of all the people, i have developed an Android app to read the Constitution.

It is available on Google Play

Download Constitution of Nepal.

Decorators in Python

While learning Python in the beginning, i skipped the portion of the chapters on Decorators. After i worked on Flask and Django, I was introduced with the Decorators.

Let us consider a simple flask app example:

@app.route("/")
def hello():
    return "Hello World!"

In this code the line with @ symbol is decorators. Decorators is one of the important feature in Python that you must learn to master it. It is not that hard, so let us learn it.

First let us see Closures

>>> def out_func():
...     def in_func():
...             print("I am inner function")
...     return in_func
... 
>>> call = out_func()
>>> call
<function in_func at 0x7f0d6819a758>
>>> call()
I am inner function
>>> 

Also let us see by passing the parameter in the function.

>>> def out_func():
...     def in_func(x):
...             print(x)
...     return in_func
... 
>>> call = out_func()
>>> call
<function in_func at 0x7f66ff93f5f0>
>>> call(3)
3
>>> 

We can also pass the function as the parameter in the function. Let us write a function wrap which takes a function. We write another function checkNumber inside the wrap. It’s main task is to return same number if the number is greater than or equal to 100 otherwise return number by adding 100. It actually adds 100 using function that we pass to wrap.

We also write function incrementHundered to add 100 to passed number.

def wrap(func):
   def checkNumber(a): # check
       return (a if a >= 100 else func(a))
   return checkNumber

def incrementHundered(a):
    return a+100

# We pass incrementHundered to wrap
incrementHundered = wrap (incrementHundered) # decorators

print (incrementHundered(2)) # 102
print (incrementHundered(300)) # 300
print (incrementHundered(29)) # 129

The above code can be re-written using @ symbol for decorators as follows:

def wrap(func):
   def checkNumber(a): # check
       return (a if a >= 100 else func(a))
   return checkNumber

@wrap
def incrementHundered(a):
    return a+100

print (incrementHundered(2)) # 102
print (incrementHundered(300)) # 300
print (incrementHundered(29)) # 129

By writing @wrap and function just below it, think as if we have passed whole function to wrap. And it works as usual. So that was the quick and easy introduction to Decorators.

I hope you understood the Decorators.

How to delete the commit history in github

Tags

Sometimes you may find deleting the commit history of your github project repository useful. You can easily delete the commit history by following the procedure below.

It is always useful to keep the backup of your repository in your computer before removing all the commit history.

Let us start by cloning a github project. I am cloning ‘myproject’, you clone yours.

$ git clone https://github.com/acsudeep/myproject.git

Since all the commit history are in the “.git” folder, we have to remove it. So, go inside your project folder. For me the project folder is ‘myproject’.

$ cd myproject

And delete the ‘.git folder’ with this command.

$ sudo rm -rf .git

Now, let us re-initialize the repository.

$ git init

$ git remote add origin https://github.com/acsudeep/myproject.git
# add remote url

$ git remote -v
# verify

Next, let us add all our files and commit the changes.

$ git add --all
$ git commit -am 'initial commit'

Now, since we just have one commit i.e ‘initial commit’. Let us force push update to our master branch of our project repository.

$ git push -f origin master

You may need to provide the credentials for your account.

Go and check your project repository in github, you should see only one commit.