import os
ORIGINAL_IMAGES_IN_PATH = '../img/icons'
MARKER_IMAGES_OUT_PATH = '../img/markers'
MARKER_TEMPLATE_PATH = '../css/img-base/marker-template.png'
script = '''
import os
images_in_path = '%(images_in_path)s'
markers_out_path = '%(markers_out_path)s'
marker_template_file = '%(marker_template_file)s'
image_extension = '.png'
image_filter = lambda file_name: file_name[-len(image_extension):] == image_extension
for root, dirs, files in os.walk(images_in_path):
for file in filter(image_filter, files):
input_path = os.path.join(root, file)
output_path = os.path.join(markers_out_path, file)
image = pdb.gimp_file_load(marker_template_file, 'template')
layer = pdb.gimp_file_load_layer(image, input_path)
image.add_layer(layer, 0)
layer.scale(23, 23, 0)
layer.set_offsets(7, 7)
merged_layer = image.merge_visible_layers(0)
pdb.file_png_save2(image, merged_layer, output_path, output_path, 0, 9, 0, 0, 0, 0, 0, 0, 0)
pdb.gimp_quit(0)
''' % {'images_in_path': os.path.abspath(ORIGINAL_IMAGES_IN_PATH),
'markers_out_path': os.path.abspath(MARKER_IMAGES_OUT_PATH),
'marker_template_file': os.path.abspath(MARKER_TEMPLATE_PATH),}
os.system("""gimp --no-interface --batch-interpreter python-fu-eval --batch "%s" """ % script)
Automatically modifies intensity levels in the specified drawable.
This procedure allows intensity levels in the specified drawable to be remapped according to a set of guessed parameters. It is equivalent to clicking the «Auto» button in the Levels tool. This procedure is only valid on RGB color and grayscale images. It will not operate on indexed drawables.
for index, literal in enumerate(a):
pass
#!/usr/bin/env python
rule_add = {
'I': 1,
'V': 5,
'X': 10,
'L': 50,
'C': 100,
'D': 500,
'M': 1000,
}
rule_div = {
('I', 'V'): 3,
('I', 'X'): 8,
('X', 'L'): 30,
('X', 'C'): 80,
('C', 'D'): 300,
('C', 'M'): 800,
}
def roman_to_arabic(roman_number):
number = 0
prev_literal = None
for literal in roman_number:
if prev_literal and rule_add[prev_literal] < rule_add[literal]:
number += rule_div[(prev_literal, literal)]
else:
number += rule_add[literal]
prev_literal = literal
return number
import unittest
class RomanNumTest(unittest.TestCase):
def test_roman_num(self):
self.assertEquals(roman_to_arabic('I'), 1)
self.assertEquals(roman_to_arabic('II'), 2)
self.assertEquals(roman_to_arabic('III'), 3)
self.assertEquals(roman_to_arabic('IV'), 4)
self.assertEquals(roman_to_arabic('V'), 5)
self.assertEquals(roman_to_arabic('VI'), 6)
self.assertEquals(roman_to_arabic('VII'), 7)
self.assertEquals(roman_to_arabic('VIII'), 8)
self.assertEquals(roman_to_arabic('IX'), 9)
self.assertEquals(roman_to_arabic('X'), 10)
self.assertEquals(roman_to_arabic('XXXI'), 31)
self.assertEquals(roman_to_arabic('XLVI'), 46)
self.assertEquals(roman_to_arabic('XCIX'), 99)
self.assertEquals(roman_to_arabic('DLXXXIII'), 583)
self.assertEquals(roman_to_arabic('DCCCLXXXVIII'), 888)
self.assertEquals(roman_to_arabic('MDCLXVIII'), 1668)
self.assertEquals(roman_to_arabic('MCMLXXXIX'), 1989)
self.assertEquals(roman_to_arabic('MMX'), 2010)
self.assertEquals(roman_to_arabic('MMXI'), 2011)
self.assertEquals(roman_to_arabic('MMXII'), 2012)
self.assertEquals(roman_to_arabic('MMMCMXCIX'), 3999)
def __getitem__(self, index ):
return self._children[index]
Библиотеки считаются на клиента, а не для хоста в целом.
# -*- coding: utf-8 -*-
from math import log10, fabs
import Image
def check_pixel_colour(pixel, colour, delta=(0,0,0)):
'''сравниваем цвет пиксиля с эталонным цветом с погрешностью дельта'''
return (colour[0] - delta[0]) <= pixel[0] <= (colour[0] + delta[0]) and\
(colour[1] - delta[1]) <= pixel[1] <= (colour[1] + delta[1]) and\
(colour[2] - delta[2]) <= pixel[2] <= (colour[2] + delta[2])
def better_colour_value(pixels, colour):
'''из списка цветов выбираем более подходящий эталонному цвету'''
delta = 1024
better_pixel = None
for pixel in pixels:
current_delta = fabs(pixel[0] - colour[0]) +\
fabs(pixel[1] - colour[1]) +\
fabs(pixel[2] - colour[2])
if current_delta < delta:
delta = current_delta
better_pixel = pixel
return better_pixel
def get_border_grids(image, grid_colour):
'''
получаем номера пиксилей по оси x первой и последней линий сетки, по оси y верхней и нижней
данные пиксили соответствуют переданным граничным значениям сетки
'''
width, height = image.size
for x in xrange(width):
if check_pixel_colour(image.getpixel((x, 0)), grid_colour):
yield x
break
for x in xrange(width - 1, -1, -1):
if check_pixel_colour(image.getpixel((x, 0)), grid_colour):
yield x
break
for y in xrange(height - 1, -1, -1):
if check_pixel_colour(image.getpixel((0, y)), grid_colour):
yield y
break
for y in xrange(height):
if check_pixel_colour(image.getpixel((0, y)), grid_colour):
yield y
break
def get_linear_value(x, x1, x2, x1_value, x2_value):
'''получаем значение для текущего пикселя с учетом граничных значений сетки для линейной оси'''
k = (x2 - x1) / (x2_value - x1_value)
b = x1 - k * x1_value
return (x - b) / k
def get_log_value(x, x1, x2, x1_value, x2_value):
'''получаем значение для текущего пикселя с учетом граничных значений сетки для логорифмической оси'''
k = (x1 - x2) / (log10(x1_value) - log10(x2_value))
b = x1 - k * log10(x1_value)
return 10**((x - b) / k)
def process_graph(image_path, x1_value, x2_value, y1_value, y2_value, colour, delta, grid_colour):
'''получаем список координат графика на изображении'''
image = Image.open(image_path)
width, height = image.size
x1, x2, y1, y2 = get_border_grids(image, grid_colour)
coordinates = []
for x in xrange(width):
pixels = {}
for y in xrange(height):
pixel = image.getpixel((x, y))
if check_pixel_colour(pixel, colour, delta):
pixels[pixel] = y
if len(pixels) > 0:
y = pixels[better_colour_value(pixels.keys(), colour)]
x_value = get_log_value(x, x1, x2, x1_value, x2_value)
y_value = get_linear_value(height - y, height - y1, height - y2, y1_value, y2_value)
coordinates.append((x_value, y_value))
return coordinates
if __name__ == '__main__':
print process_graph('test_001.png', 10., 10000., -25., 10., (0,0,255), (10,10,50), (221,221,221))
print process_graph('test_002.png', 30., 10000., 90., 132., (150,255,0), (50,50,10), (104,104,104))
except Exception as exception
и идите в этом направлении, но у меня просто не находит файл по вводимой строке и пропуская os.path.isdir и os.path.isfile выходит (у вас нет проверки что можно ввести неверное имя файла).any([file_path.endswith(extension) for extension in '.rar,.txt'.split(',')])
u"%s сконвертирован." % file_path
вместо file_path + u" сконвертирован."
.if __name__ == '__main__':
print u"-------------------------------"
можно писать print u"-" * 20
'b'
в команду open
: open(file_path, 'rb')
и open(file_path, 'wb')
# -*- coding: utf-8 -*-
import os
import sys
from pyPdf import PdfFileReader
PDF_EXTENSION = '.pdf'
DEFAULT_PATH = '.'
def pages_count(path):
return PdfFileReader(file(path, "rb")).getNumPages()
if __name__=="__main__":
path = sys.argv[1] if len(sys.argv) > 1 else DEFAULT_PATH
total_pages_count = 0
for root, dirs, files in os.walk(path):
for file_name in files:
if file_name[-len(PDF_EXTENSION):] == PDF_EXTENSION:
file_path = os.path.join(root, file_name)
file_pages_count = pages_count(file_path)
print file_path, file_pages_count
total_pages_count += file_pages_count
print 'total:', total_pages_count
wins = 0
# i1, i2, i3 и тд - выбор стакана на 1ом, 2ом, 3ем и тд шагах соответственно
for i1 in xrange(1, 11):
for i2 in xrange(1, 11):
for i3 in xrange(1, 11):
for i4 in xrange(1, 11):
for i5 in xrange(1, 11):
win = 0
# j - начальное положение шарика
# pos1, pos2, pos3 и тд
# - положение шарика в начале 1ого, 2ого, 3его и тд хода
for j in xrange(1, 11):
pos1 = min(j + 0, 10)
pos2 = min(j + 1, 10)
pos3 = min(j + 2, 10)
pos4 = min(j + 3, 10)
pos5 = min(j + 4, 10)
if i1 == pos1 or i2 == pos2 or i3 == pos3\
or i4 == pos4 or i5 == pos5:
win += 1
if win == 10:
print i1, i2, i3, i4, i5
wins += 1
print wins
wins = 0
# i1, i2, i3 и тд - выбор стакана на 1ом, 2ом, 3ем и тд шагах соответственно
for i1 in xrange(1, 11):
for i2 in xrange(1, 11):
for i3 in xrange(1, 11):
for i4 in xrange(1, 11):
for i5 in xrange(1, 11):
for i6 in xrange(1, 11):
win = 0
# j - начальное положение шарика
# pos1, pos2, pos3 и тд
# - положение шарика в начале 1ого, 2ого, 3его и тд хода
for j in xrange(1, 11):
pos1 = min(j + 0, 10)
pos2 = min(j + 1, 10)
pos3 = min(j + 2, 10)
pos4 = min(j + 3, 10)
pos5 = min(j + 4, 10)
pos6 = min(j + 5, 10)
if i1 == pos1 or i2 == pos2 or i3 == pos3\
or i4 == pos4 or i5 == pos5 or i6 == pos6:
win += 1
if win == 10:
print i1, i2, i3, i4, i5, i6
wins += 1
print wins