## Python reverse and splice a tuple

This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters

 """ About: img is an OpenCV 2 object. img.shape prints a tuple as (height, width, channels) e.g. (512, 768, 3) We need a tuple as (width, height) e.g. (768, 512) """ import cv2 image_path = './image.jpg' img = cv2.imread(image_path, flags=cv2.CV_LOAD_IMAGE_COLOR) print img.shape # (512, 768, 3) print img.shape[::–1] # (3, 768, 512) print img.shape[1::–1] # (768, 512)

view raw

code.py

hosted with ❤ by GitHub

## Python: PIL ( Pillow ) & NumPy add images

Pillow (PIL) and NumPy libraries can do wonders in Python! I had once he requirement to overlap two images – not watermarking.

I found several alternatives, but curious to see which would work best.

1. (x+y)/2 … Mathematically, x/2+y/2 seems equivalent to above, but it is not. We’d be loosing a ton of info by doing so!
2. Numpy.minimum((x+y),256)
3. final = (x+y/2) and then addition = addition[addition>256]=256
4. Pillow’s Image.blend(x,y,0.5)
5. Pillow’s Image.composite(x,y,y)

Continue reading “Python: PIL ( Pillow ) & NumPy add images”

## Python: Find closest from given dictionary values

Objective: I have a dictionary, and a float value. I would like to sort the dictionary in such a way that, first key-value pair’s value is nearest (min absolute difference) to a given float value, and last would be the farthest (max absolute difference)

This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters

 >>> chooseFrom {'1': 10.3438090737, '3': 7.73275047259, '2': 12.9046550095, '5': 10.3438090737, '4': 12.9046550095, '7': 7.88437303088, '6': 5.12169187146, '8': 0.0} >>> gh 7.73275047259 # reference : http://stackoverflow.com/a/12141207/799593 >>> test = sorted(chooseFrom, key=lambda x:abs(chooseFrom[x]–gh)) >>> test ['3', '7', '1', '5', '6', '2', '4', '8'] >>> for x in test: … print '{} : {} ==> {}'.format(x, chooseFrom[x], abs(gh–chooseFrom[x])) … 3 : 7.73275047259 ==> 0.0 7 : 7.88437303088 ==> 0.15162255829 1 : 10.3438090737 ==> 2.61105860111 5 : 10.3438090737 ==> 2.61105860111 6 : 5.12169187146 ==> 2.61105860113 2 : 12.9046550095 ==> 5.17190453691 4 : 12.9046550095 ==> 5.17190453691 8 : 0.0 ==> 7.73275047259

## Python: Compare Dictionaries

This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters

 def compareDicts(d1, d2): differences = [] # present in d1 but not in d2 for key, value in ({k: d1[k] for k in set(d1) – set(d2)}).iteritems(): temp = list[[key, value], None] differences.append(temp) # present in d2 but not in d1 diff2 = {k: d2[k] for k in set(d2) – set(d1)} for key, value in (diff2).iteritems(): temp = [None, [key, value]] differences.append(temp) # updated from d1 to d2 updatedKeys = [item for item in list(set(d1.keys()) & set(d2.keys())) if d1.get(item) != d2.get(item)] for key in updatedKeys: temp = [[key, d1.get(key)],[key, d2.get(key)]] differences.append(temp) return differences pass

view raw

compareDicts.py

hosted with ❤ by GitHub

## Getting Started: Python

I personally think that Visual Studio is the best IDE, and I spend most of my day in it. And I’d definitely by using Visual Studio as much as I can –  if you are on Windows, Visual Studio’s Python support is worth exploring.

I’d leave the choice up to you. Both IDE have free and paid options.

I think Visual Studio is an engineering marvel. If as much effort went into space travel as went into the design of this, we’d be on Mars by now.

That said if you  prefer to use PyCharm:

• Please go to https://www.jetbrains.com/student/ and get your free full blown tools offered by Jet Brains, with all bells and whistles.
• Here are two YouTube video series if you want:

Getting Started with PyCharm:

PyCharm Video Demos:

If you want to use Visual Studio: