Why can't I adjust the brightness levels of a PNG file - python

I have some scanned images which vary in brightness. I desire to equalize the brightness levels to make the images (as a group) more consistent. I borrowed some code from SO (with minor changes) that was described as doing what I needed. But the code seems to have no effect at all. Obviously I’m doing something wrong but I can’t seem to figure it out. What I am trying to do is read the perceived brightness level of the original images and from that calculate a target brightness level and pass that value to a function that will (theoretically) alter the image brightness to the perceived brightness level and create a new image file.
from os import listdir
import numpy as np
import cv2
from PIL import Image, ImageStat
import operator
import math
def brightness(fname):
with Image.open(fname) as img:
stat = ImageStat.Stat(img)
r,g,b = stat.mean
return int(math.sqrt(0.241*(r**2) + 0.691*(g**2) + 0.068*(b**2)))
def change_brightness(fname, value):
if value == 0: return
img = cv2.imread(SourceDir+fname,)
hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
cv2.add(hsv[:,:,2], value, hsv[:,:,2])
img = cv2.cvtColor(hsv, cv2.COLOR_HSV2BGR)
cv2.imwrite(DestDir+fname, img)
SourceDir = 'I:\\'
DestDir = 'G:\\Documents\\'
InputFiles = []
BrightnessLevels = {}
TargetBrightness = 130
InputFiles = listdir(SourceDir)
try:
InputFiles.remove('System Volume Information')
except:
pass
InputFiles.sort()
for InputFile in InputFiles:
old_b = brightness(SourceDir+InputFile)
change = TargetBrightness-old_b
change_brightness(InputFile, change)
new_b = brightness(DestDir+InputFile)
BrightnessLevels[InputFile] = (old_b, change, new_b)
sorted_b = sorted(BrightnessLevels.items(), key=operator.itemgetter(1))
for f,b in sorted_b:
print(f,b)
The results I get is as follows:
Page-003.png (89, 41, 89)
Page-082.png (89, 41, 89)
Page-047.png (90, 40, 89)
Page-075.png (92, 38, 91)
Page-111.png (92, 38, 91)
Page-071.png (92, 38, 92)
Page-074.png (92, 38, 92)
Page-077.png (92, 38, 92)
Page-112.png (93, 37, 93)
Page-079.png (94, 36, 93)
Page-078.png (94, 36, 94)
Page-031.png (95, 35, 95)
Page-083.png (95, 35, 95)
Page-023.png (96, 34, 96)
Page-050.png (96, 34, 96)
Page-081.png (96, 34, 96)
Page-098.png (96, 34, 96)
Page-099.png (96, 34, 96)
Page-033.png (97, 33, 96)
Page-094.png (97, 33, 97)
Page-107.png (97, 33, 97)
Page-038.png (98, 32, 98)
Page-039.png (98, 32, 98)
Page-095.png (98, 32, 98)
Page-096.png (98, 32, 98)
Page-106.png (98, 32, 98)
Page-108.png (98, 32, 98)
Page-018.png (99, 31, 99)
Page-046.png (99, 31, 99)
Page-072.png (99, 31, 99)
Page-097.png (99, 31, 99)
Page-066.png (100, 30, 99)
Page-080.png (100, 30, 99)
Page-110.png (100, 30, 99)
Page-011.png (100, 30, 100)
Page-121.png (100, 30, 100)
Page-109.png (101, 29, 100)
Page-006.png (101, 29, 101)
Page-051.png (101, 29, 101)
Page-007.png (102, 28, 102)
Page-010.png (102, 28, 102)
Page-015.png (102, 28, 102)
Page-025.png (102, 28, 102)
Page-049.png (102, 28, 102)
Page-052.png (102, 28, 102)
Page-056.png (102, 28, 102)
Page-065.png (102, 28, 102)
Page-076.png (102, 28, 102)
Page-086.png (102, 28, 102)
Page-102.png (102, 28, 102)
Page-122.png (102, 28, 102)
Page-067.png (103, 27, 102)
Page-092.png (103, 27, 102)
Page-116.png (103, 27, 102)
Page-014.png (103, 27, 103)
Page-084.png (103, 27, 103)
Page-103.png (103, 27, 103)
Page-115.png (103, 27, 103)
Page-026.png (104, 26, 103)
Page-059.png (104, 26, 103)
Page-012.png (104, 26, 104)
Page-024.png (104, 26, 104)
Page-048.png (104, 26, 104)
Page-123.png (104, 26, 104)
Page-009.png (105, 25, 105)
Page-034.png (105, 25, 105)
Page-087.png (105, 25, 105)
Page-114.png (105, 25, 105)
Page-118.png (105, 25, 105)
Page-119.png (105, 25, 105)
Page-030.png (106, 24, 105)
Page-043.png (106, 24, 105)
Page-062.png (106, 24, 105)
Page-063.png (106, 24, 105)
Page-035.png (106, 24, 106)
Page-126.png (106, 24, 106)
Page-113.png (107, 23, 106)
Page-044.png (107, 23, 107)
Page-068.png (107, 23, 107)
Page-019.png (108, 22, 107)
Page-070.png (108, 22, 107)
Page-054.png (108, 22, 108)
Page-064.png (108, 22, 108)
Page-090.png (108, 22, 108)
Page-002.png (109, 21, 108)
Page-058.png (109, 21, 108)
Page-091.png (109, 21, 108)
Page-020.png (109, 21, 109)
Page-101.png (109, 21, 109)
Page-104.png (110, 20, 109)
Page-032.png (110, 20, 110)
Page-004.png (111, 19, 111)
Page-040.png (112, 18, 111)
Page-021.png (112, 18, 112)
Page-041.png (112, 18, 112)
Page-061.png (113, 17, 113)
Page-016.png (114, 16, 114)
Page-055.png (114, 16, 114)
Page-008.png (115, 15, 114)
Page-117.png (115, 15, 115)
Page-042.png (116, 14, 116)
Page-036.png (118, 12, 117)
Page-085.png (130, 0, 129)
Page-022.png (131, -1, 131)
Page-073.png (131, -1, 131)
Page-027.png (133, -3, 132)
Page-124.png (133, -3, 132)
Page-005.png (133, -3, 133)
Page-057.png (134, -4, 133)
Page-013.png (135, -5, 135)
Page-037.png (135, -5, 135)
Page-105.png (137, -7, 137)
Page-120.png (137, -7, 137)
Page-045.png (138, -8, 138)
Page-060.png (139, -9, 139)
Page-093.png (139, -9, 139)
Page-017.png (140, -10, 140)
Page-125.png (141, -11, 140)
Page-089.png (142, -12, 141)
Page-029.png (142, -12, 142)
Page-028.png (143, -13, 142)
Page-069.png (145, -15, 145)
Page-053.png (147, -17, 146)
Page-100.png (149, -19, 149)
Page-088.png (150, -20, 149)
The first value of the tuple is the initial perceived brightness of the image. The second value is the calculated adjustment to the brightness level. The third value is the perceived brightness level of the new image file. No matter what adjustment values I try, they don't seem to have any effect. And by visually looking at the files seem to agree that they look the same as the originals.

Change the line:
cv2.add(hsv[:,:,2], value, hsv[:,:,2])
so it looks like this:
hsv[:,:,2] = cv2.add(hsv[:,:,2], value)
It doesn't work how the documentation implies - I don't know why.

Related

Find distance for every edge and keep separate routes

[[0, 100, 7, 27, 34, 40, 41, 48, 58, 65, 75, 78, 79, 96, 126, 127, 0],
[0, 2, 45, 54, 56, 57, 59, 66, 67, 82, 86, 102, 124, 133, 0],
[0, 35, 39, 52, 53, 60, 61, 80, 81, 83, 87, 97, 98, 101, 109, 0],
[0, 15, 28, 29, 30, 31, 32, 33, 37, 38, 49, 50, 51, 71, 95, 0],
[0, 3, 16, 22, 23, 44, 72, 73, 74, 90, 110, 131, 0],
[0, 10, 11, 18, 19, 36, 55, 89, 93, 94, 108, 113, 114, 0],
[0, 1, 5, 6, 9, 12, 17, 24, 43, 64, 77, 85, 88, 91, 92, 111, 112, 130, 0],
[0, 13, 20, 42, 62, 68, 84, 99, 104, 116, 119, 125, 128, 129, 132, 0],
[0, 8, 14, 26, 63, 69, 70, 103, 105, 123, 0],
[0, 4, 21, 25, 46, 47, 106, 107, 115, 117, 118, 120, 121, 122, 0],
[0, 76, 0]]
I have the different routes listed above. I need to calculate the distance of every route (11 routes in total)
After this, I have created all edges within a single route.
[[(0, 100),
(100, 7),
(7, 27),
(27, 34),
(34, 40),
(40, 41),
(41, 48),
(48, 58),
(58, 65),
(65, 75),
(75, 78),
(78, 79),
(79, 96),
(96, 126),
(126, 127),
(127, 0)],
[(0, 2),
(2, 45),
(45, 54),
(54, 56),
(56, 57),
(57, 59),
(59, 66),
(66, 67),
(67, 82),
(82, 86),
(86, 102),
(102, 124),
(124, 133),
(133, 0)],
[(0, 35),
(35, 39),
(39, 52),
(52, 53),
(53, 60),
(60, 61),
(61, 80),
(80, 81),
(81, 83),
(83, 87),
(87, 97),
(97, 98),
(98, 101),
(101, 109),
(109, 0)],
[(0, 15),
(15, 28),
(28, 29),
(29, 30),
(30, 31),
(31, 32),
(32, 33),
(33, 37),
(37, 38),
(38, 49),
(49, 50),
(50, 51),
(51, 71),
(71, 95),
(95, 0)],
[(0, 3),
(3, 16),
(16, 22),
(22, 23),
(23, 44),
(44, 72),
(72, 73),
(73, 74),
(74, 90),
(90, 110),
(110, 131),
(131, 0)],
[(0, 10),
(10, 11),
(11, 18),
(18, 19),
(19, 36),
(36, 55),
(55, 89),
(89, 93),
(93, 94),
(94, 108),
(108, 113),
(113, 114),
(114, 0)],
[(0, 1),
(1, 5),
(5, 6),
(6, 9),
(9, 12),
(12, 17),
(17, 24),
(24, 43),
(43, 64),
(64, 77),
(77, 85),
(85, 88),
(88, 91),
(91, 92),
(92, 111),
(111, 112),
(112, 130),
(130, 0)],
[(0, 13),
(13, 20),
(20, 42),
(42, 62),
(62, 68),
(68, 84),
(84, 99),
(99, 104),
(104, 116),
(116, 119),
(119, 125),
(125, 128),
(128, 129),
(129, 132),
(132, 0)],
[(0, 8),
(8, 14),
(14, 26),
(26, 63),
(63, 69),
(69, 70),
(70, 103),
(103, 105),
(105, 123),
(123, 0)],
[(0, 4),
(4, 21),
(21, 25),
(25, 46),
(46, 47),
(47, 106),
(106, 107),
(107, 115),
(115, 117),
(117, 118),
(118, 120),
(120, 121),
(121, 122),
(122, 0)],
[(0, 76), (76, 0)]]
However, I need to calculate the distance between the edges. Every edge consists of 2 numbers which are city numbers in a distance matrix (so 0,100 is the distance from city 0 to city 100). I tried to calculate the distances but cannot keep separate routes.
I already tried this:
a_list=[]
visiting_time={}
for k in range(len(result)):
for (i,j) in visits[k]:
visiting_time[(i,j)]= distance_matrix_new_time[i][j]
f=list(visiting_time.values())
a_list.append(f)
In my code Result is the list with different routes (first list), and visits is the list with all edges (second list)
the output should be like this
[2,3,5,6,3,2,5,8,3,5,2,4,6],[2,6,3,1,9,....],[....] etc.
Can someone help me out?
you could use a list comprehension:
a_list = [[distance_matrix_new_time[i][j] for i, j in l] for l in visits]

How to create an array from a given array of ranges

I have this code:
pg=[(10, 19), (30, 32), (37, 38), (50, 59), (63, 64),
(69, 69), (120, 121), (124, 129), (130, 139), (160, 161),
(164, 169), (180, 182), (185, 185), (189, 189), (190, 192),
(194, 194), (196, 199), (260, 269), (270, 279), (300, 309),
(330, 339), (358, 359), (360, 369)]
Those are given ranges, for example, pg[0] should be 10, pg[1] be 11, pg[2] be 12. and so on for the rest of the ranges. So I want the final array to be like this:
pg=[10, 11, 12, 13 ....19, 30, 31,....,32,37, 38,50,51,....,59,63.. and so on]
How can I do this in python? Is it possible to do it without hard coding every range of elements in a new array?
I guess the following might work
pg = [(10, 19), (30, 32), (37, 38), (50, 59), (63, 64),
(69, 69), (120, 121), (124, 129), (130, 139), (160, 161),
(164, 169), (180, 182), (185, 185), (189, 189), (190, 192),
(194, 194), (196, 199), (260, 269), (270, 279), (300, 309),
(330, 339), (358, 359), (360, 369)]
arr = []
for val in pg:
arr += list(range(val[0], val[1] + 1))
print(arr)
This is one approach using a list comprehension and itertools.chain(to flatten the list)
Ex:
from itertools import chain
pg=[(10, 19), (30, 32), (37, 38), (50, 59), (63, 64),
(69, 69), (120, 121), (124, 129), (130, 139), (160, 161),
(164, 169), (180, 182), (185, 185), (189, 189), (190, 192),
(194, 194), (196, 199), (260, 269), (270, 279), (300, 309),
(330, 339), (358, 359), (360, 369)]
result = list(chain.from_iterable([range(*i) for i in pg]))
print(result)
A one-linear
sum([list(range(x1, x2+1)) for x1, x2 in pg], [])
Try this
l = []
for r in pg:
l.extend(range(r[0], r[1]+1))
One more example using list comprehension
a = [(10, 19), (30, 35)]
b = [j for i in a for j in range(i[0], i[1]+1)]
print(b)
#output
[10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 30, 31, 32, 33, 34, 35]
with help of sum function also we can achieve this.
pg=[(10, 19), (30, 32), (37, 38), (50, 59), (63, 64),
(69, 69), (120, 121), (124, 129), (130, 139), (160, 161),
(164, 169), (180, 182), (185, 185), (189, 189), (190, 192),
(194, 194), (196, 199), (260, 269), (270, 279), (300, 309),
(330, 339), (358, 359), (360, 369)]
print (list(sum(pg,())))
#output:[10, 19, 30, 32, 37, 38, 50, 59, 63, 64, 69, 69, 120, 121, 124, 129, 130, 139,
#160, 161, 164, 169, 180, 182, 185, 185, 189, 189, 190, 192, 194, 194, 196, 199, 260,
#269, 270, 279, 300, 309, 330, 339, 358, 359, 360, 369]

How to get largest (unconnected) network / cluster using networkx in Python?

I have the following example data:
my_network_data = [(39, 118), (179, 14), (35, 118), (225, 14), (64, 118), (6, 14), (187, 14), (161, 14), (42, 14), (53, 14), (47, 1), (127, 14), (14, 118), (3, 1), (175, 14), (21, 118), (5, 14), (18, 14), (122, 1), (137, 14), (157, 14), (19, 14), (19, 118), (118, 14), (30, 118), (159, 14), (124, 118), (56, 14), (161, 118), (100, 14), (53, 118), (136, 118), (41, 14), (4, 14), (217, 14), (32, 14), (175, 118), (104, 14), (82, 118), (4, 118), (222, 14), (201, 118), (136, 14), (86, 1), (153, 14), (195, 14)]
If I plot it using networkx, I do the following:
import networkx as nx
g = nx.Graph()
g.add_edges_from(my_network_data)
print(nx.info(g))
Output:
Name:
Type: Graph
Number of nodes: 41
Number of edges: 45
Average degree: 2.1951
The graph looks like this:
nx.draw(g, with_labels=True)
How do I get the information using networkx that there are 2 distinctive clusters and which items are in those clusters?
Suggested output :
[[1, 86, 47, 3, 122], [14, 118, 136, 53, 179, 30, 100, 41, 35, 4, 19, 82, 104, 159, 64, 32, 124, 14, 39, 4, 137, 136, 187, 217, 153, 5, 53, 19, 42, 175, 18, 21, 222, 175, 6, 195, 56, 157, 201, 161, 161, 127, 225]]
I'm not sure if networkx is the best possible library for this task. If you have a better suggestion (using Python), I'm open to using it.
Looks like nx.connected_components() is what you need:
for c in nx.connected_components(g):
print(c)
{4, 5, 6, 136, 137, 14, 18, 19, 21, 153, 157, 30, 159, 32, 161, 35, 39, 41, 42, 175, 179, 53, 56, 187, 64, 195, 201, 82, 217, 222, 225, 100, 104, 118, 124, 127}
{1, 3, 47, 86, 122}

Finding number of combinations

I realize this may be more of a math problem than an actual programming problem. I'm trying to figure this out with python.
So the user is going to specify a range of numbers to me, the min range being 1-2 and max being 1-99. I then have to tell the user how many 3 number combinations can be made in that range. However, the combinations can ONLY be in increasing numeric order. So for example, if the given range is 1-50, I can't say 45 - 10 - 20 is a combination, because it is not in increasing numeric order.
Try the itertools module.
import itertools
numbers = range(1,100)
items = set(list(itertools.combinations(numbers,3)))
for item in items:
print item
It seems to give the desired output.
Be aware: It seems to take a long time (Edit: to print it all at once.).
This is part of the output:
, 73, 75), (76, 86, 91), (42, 91, 92), (8, 54, 71), (11, 54, 87), (37, 79, 86), (2, 17, 32), (44, 67, 78), (14, 24, 56), (10, 64, 79), (9, 90, 94), (39, 52, 88), (62, 78, 90), (9, 60, 71), (23, 25, 30), (5, 27, 92), (33, 74, 78), (68, 70, 84), (48, 79, 95), (8, 70, 95), (23, 68, 78), (14, 45, 78), (8, 36, 73), (72, 86, 88), (13, 26, 74), (35, 60, 86), (3, 29, 76), (6, 15, 74), (46, 54, 73), (7, 41, 88), (48, 59, 90), (23, 30, 73), (71, 83, 91), (42, 78, 96), (44, 60, 92), (27, 46, 68), (27, 72, 88), (34, 74, 78), (24, 55, 93), (84, 93, 97), (32, 36, 73), (7, 31, 38), (28, 43, 66), (29, 37, 40), (19, 33, 96), (45, 66, 77), (25, 66, 72), (22, 60, 74), (59, 60, 76), (30, 57, 82), (11, 16, 51), (41, 48, 99), (5, 21, 86), (18, 27, 98), (26, 34, 95), (19, 72, 74), (32, 34, 35), (43, 68, 93), (36, 57, 77), (20, 50, 90), (25, 71, 99), (47, 74, 87), (9, 26, 35), (20, 24, 89), (27, 67, 83), (3, 19, 70), (20, 72, 79), (24, 36, 79), (8, 25, 43), (49, 53, 87), (24, 63, 68), (21, 63, 92), (21, 56, 72), (26, 43, 87), (79, 92, 94), (22, 41, 98), (45, 55, 88), (30, 46, 94), (38, 71, 79), (17, 51, 81), (43, 65, 97), (40, 56, 72), (19, 62, 88), (31, 38, 98), (15, 25, 79), (24, 45, 71), (52, 87, 98), (20, 39, 82), (23, 33, 44), (43, 68, 88), (6, 8, 29), (36, 73, 95), (48, 78, 84), (22, 38, 84), (21, 65, 97), (30, 31, 57), (27, 28, 38), (2, 33, 46), (24, 29, 51), (4, 6, 45), (64, 71, 93), (14, 36, 68), (36, 51, 62), (20, 40, 68), (19, 71, 81), (33, 60, 81), (13, 25, 60), (17, 39, 68), (68, 69, 81), (18, 19, 89), (2, 28, 61), (4, 67, 71), (12, 26, 52), (34, 41, 46), (22, 27, 59), (28, 56, 96), (1, 25, 53), (39, 61, 90), (11, 31, 44), (17, 40, 82), (16, 21, 73), (19, 78, 93), (10, 16, 36), (21, 30, 32), (15, 23, 69), (9, 21, 28), (20, 29, 40), (11, 48, 61), (36, 71, 81), (19, 24, 48), (7, 49, 61), (15, 74, 99), (13, 45, 85)])

Breaking a long list into shorter, regular lists [duplicate]

This question already has answers here:
How do I split a list into equally-sized chunks?
(66 answers)
Closed 9 years ago.
I have a long list I would like to break into shorter lists. I am using a list comprehension but it seems a bit long and inelegant. Is there a better way?
# z is a list
z = range(99)
## zz should slice z into short lists with three members
## using list comprehension I get this
zz = [ z[i : i+3] for i,x in enumerate(z) if i%3 == 0 ]
# seems a bit verbose. is there a cleaner way?
From itertools (it's one of the common recipes):
import itertools
def grouper(iterable, n, fillvalue=None):
args = [iter(iterable)] * n
return itertools.izip_longest(fillvalue=fillvalue, *args)
Example:
>>> list(grouper(range(100), 3))
[(0, 1, 2), (3, 4, 5), (6, 7, 8), (9, 10, 11), (12, 13, 14), (15, 16, 17), (18, 19, 20), (21, 22, 23), (24, 25, 26), (27, 28, 29), (30, 31, 32), (33, 34, 35), (36, 37, 38), (39, 40, 41), (42, 43, 44), (45, 46, 47), (48, 49, 50), (51, 52, 53), (54, 55, 56), (57, 58, 59), (60, 61, 62), (63, 64, 65), (66, 67, 68), (69, 70, 71), (72, 73, 74), (75, 76, 77), (78, 79, 80), (81, 82, 83), (84, 85, 86), (87, 88, 89), (90, 91, 92), (93, 94, 95), (96, 97, 98), (99, None, None)]

Categories