Python: PIL (Pillow) & Numpy intersect images


Input images used :

1.png C.png

Continue reading “Python: PIL (Pillow) & Numpy intersect images”

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 most common element from list of lists


I took a swing at answering a question on StackOverflow, albeit I liked the accepted answer better than mine. But Continue reading “Python: find most common element from list of lists”

Python : sort int list based on another


This post discusses how to custom sort one list in python based on the order of elements of another list.

Continue reading “Python : sort int list based on another”

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)


>>> 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(ghchooseFrom[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

C# String Extension & Helper Methods


I enjoyed spending some time after creating some very useful (at least to me 😛 ) C# helper functions for string operation. So here is the gist of itContinue reading “C# String Extension & Helper Methods”

Python: Compare Dictionaries



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