Python: Импорт файла из родительской папки

... Теперь я знаю, что этот вопрос задавали много раз, и я просмотрел эти другие потоки. Пока ничего не помогло, от использования sys.path.append ('.') До простого импорта foo

У меня есть файл python, который хочет импортировать файл (находящийся в родительском каталоге). Можете ли вы помочь мне выяснить, как мой дочерний файл может успешно импортировать свой файл в родительский каталог. Я использую python 2.7

. Структура такая (в каждом каталоге также есть файл __ init __ .py):

StockTracker /
__ Comp /
____ a.py
____ SubComp /
__ _ ___ b.py

Внутри b.py я хотел бы импортировать a.py: заголовок ('Content-type: image / png'); $ color = RgbfromHex ($ _ GET ['цвет']); $ text = urldecode ($ _ GET ['текст']); $ font = 'arial ....

Я использовал этот простой скрипт для создания изображений из текста:

<?php
header('Content-type: image/png');

$color = RgbfromHex($_GET['color']);

$text = urldecode($_GET['text']);

$font = 'arial.ttf';

$im = imagecreatetruecolor(400, 30);

$bg_color = imagecolorallocate($im, 255, 255, 255);

$font_color = imagecolorallocate($im, $color[0], $color[1], $color[2]);

imagefilledrectangle($im, 0, 0, 399, 29, $bg_color);

imagettftext($im, 20, 0, 10, 20, $font_color, $font, $text);

imagepng($im);

imagedestroy($im);

function RgbfromHex($hexValue) {
    if(strlen(trim($hexValue))==6) {
        return array(
                     hexdec(substr($hexValue,0,2)), // R
                     hexdec(substr($hexValue,2,2)), // G
                     hexdec(substr($hexValue,4,2))  // B
                    );
    }
    else return array(0, 0, 0);
}

?>

Я вызываю скрипт с помощью file.php? text = testing script & color = 000000

Теперь я бы хотел бы знать, как я могу создать текст с обычным и полужирным шрифтами, смешанными в одном изображении, например, file.php? text = testing script & color = 000000


Спасибо dqhendricks за помощь я разобрался.

Вот небольшой сценарий, который я написал, все еще нуждается в большом количестве улучшений, но для базовой функциональности он, похоже, работает нормально:

<?php
header('Content-type: image/png');

$color = RgbfromHex($_GET['color']);

$im = imagecreatetruecolor(400, 30);

$white = imagecolorallocate($im, 255, 255, 255);

imagefilledrectangle($im, 0, 0, 399, 29, $white);

$tmp = $_GET['text'];

$words = explode(" ", $tmp);

$x = array(0,0,10); // DUMMY ARRAY WITH X POSITION FOR FIRST WORD

$addSpace = 0;

foreach($words as $word)
{
    if($addSpace) $word = " ".$word; // IF WORD IS NOT THE FIRST ONE, THEN ADD SPACE

    if(stristr($word, "<b>"))
    {
        $font = 'arialbd.ttf'; // BOLD FONT
        $x = imagettftext($im, 20, 0, $x[2], 20, imagecolorallocate($im, $color[0], $color[1], $color[2]), $font, str_replace(array("<b>","</b>"), "", $word));
    }
    else
    {
        $font = 'arial.ttf'; // NORMAL FONT
        $x = imagettftext($im, 20, 0, $x[2], 20, imagecolorallocate($im, $color[0], $color[1], $color[2]), $font, $word);
    }

    $addSpace = 1;
}

imagepng($im);

imagedestroy($im);

function RgbfromHex($hexValue) {
    if(strlen(trim($hexValue))==6) {
        return array(
                     hexdec(substr($hexValue,0,2)), // R
                     hexdec(substr($hexValue,2,2)), // G
                     hexdec(substr($hexValue,4,2))  // B
                    );
    }
    else return array(0, 0, 0);
}

?>

Примечание: это будет работать только для выделенных жирным шрифтом отдельных слов, разделенных пробелами и не для выделения части слова жирным шрифтом.

Вызов сценария с file.php? Text = testing + script & color = 000000

6
задан Meredith 9 January 2011 в 04:29
поделиться