A) You most certainly can do it in one line. On your second iteration you're really only getting an attribute for each value in your dictionary.
img_dimension = {k: cv2.imread(v, 0).shape for k, v in static_templates.items()}
B) It is getting a bit harder to read and I don't think I would do exactly that. You can do comprehensions over multiple lines, which might help a bit.
img_dimension = {
k: cv2.imread(v, 0).shape
for k, v in static_templates.items()
}
C) I think what I would do is combine the comprehension with a function. This keeps the logic out of the comprehension and I think is easier to read. I don't know if defining a function will break your desire for a single line of code or not.
def get_shape(v):
x = cv2.imread(v, 0)
return x.shape
img_dimension = {k: get_shape(v) for k, v in static_templates.items()}
Note: This assumes that templates_gray
isn't something you need to use later.