カテゴリー
プログラム

Pythonで処理時間計測

Pythonで高速化する場合に必須となるのが処理速度計測numpyとかlistとか混在した場合に非常に遅くなる場合があるので注意!

#!/usr/bin/env python3
# -*- coding: utf-8 -*-


import time
import numpy as np


#-------------------------------------------------
## main ###
#-------------------------------------------------
if __name__=='__main__':
    print("Hello Python!" )
    list10000 = [0] * 10000


    start_time = time.time()
    for i in range(0,len(list10000)-1):
        list10000[i] = i
    elapsed_time = time.time() - start_time
    print ("list 10000:{0}".format(elapsed_time) + "[sec]")

    nplist = np.zeros(10000)
    
    start_time = time.time()
    for i in range(0,len(nplist)-1):
        nplist[i] = i
    elapsed_time = time.time() - start_time
    print ("nplist 10000:{0}".format(elapsed_time) + "[sec]")
カテゴリー
プログラム

Pythonで座標表示

matplotlibのscatterを利用して座標系の位置を表示

#!/usr/bin/env python3
# -*- coding: utf-8 -*-


import numpy as np
import matplotlib.pyplot as pt

#-------------------------------------------------
## main ###
#-------------------------------------------------
if __name__=='__main__':

    # int型の0で初期化
    fig1 = np.zeros((10,10,2),dtype=np.int)
    fig2 = np.zeros((10,10,2),dtype=np.int)

    fig = pt.figure()
    asp = fig.add_subplot(1, 1, 1)

    for i in range(0,10):
        for j in range(0,10):
            fig1[i][j][0] = j*10
            fig1[i][j][1] = i*10
            fig2[i][j][0] = j*11
            fig2[i][j][1] = i*11
            asp.scatter(fig1[i][j][0], fig1[i][j][1], c='red')
            asp.scatter(fig2[i][j][0], fig2[i][j][1], c='c')

    print(fig1)
    print(fig2)

    asp.grid(True)

    pt.show()

カテゴリー
プログラム

YAHOO検索結果スクレイピング

YAHOO検索は10件ずつしか取れないし、連続でやってると取れなくなる?のでスリープで回避してます。

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

# 指定のURLをブラウザで開く
# Google検索結果は取得できるのか?

import re
import time
import webbrowser as wb
import requests
from bs4 import BeautifulSoup

#-------------------------------------------------
## main ###
#-------------------------------------------------
if __name__=='__main__':

    # 100ランク取得
    load_url = "https://www.google.co.jp/search?hl=ja&source=hp&q=ジョジョ+スタンド&ie=utf-8&oe=utf-8&num=101"

    # HTML取得
    html = requests.get(load_url)
    web_data = BeautifulSoup(html.content, "html.parser")
    list = web_data.findAll(True, {'class': 'BNeawe vvjwJb AP7Wnd'})

    # ランキング表示
    cnt = 0
    for ls in list:
        a = str(ls).strip('<div class="BNeawe vvjwJb AP7Wnd">')
        result_title = a.strip('</')
        print(str(cnt) + ":" + result_title)
        cnt = cnt + 1

    # Yahooは10件単位でしか取れないので10回まわす
    print("■Yahoo ランキング■")
    cnt = 0
    for i in range(10):
        # 10ランク取得
        pagenum = i * 10 + 1
        load_url = "https://search.yahoo.co.jp/search?p=ジョジョ+スタンド&ei=utf-8&b=" + str(pagenum)

        # HTML取得
        html = requests.get(load_url)
        web_data = BeautifulSoup(html.content, "html.parser")
        list = web_data.findAll('a')

        pattern = "(.*)clear.gif(.*)"
        # ランキング表示
        for ls in list:
            if str(ls).find('clear.gif') != -1:
                d = re.search(pattern, str(ls))
                a = d.group(2)
                a = a.replace("<b>", "")
                a = a.replace("</b>", "")
                a = a.replace(""""""">", "")
                a = a.replace("</a", "")

                result_title = a.strip('|')
                print(str(cnt) + ":" + result_title)
                cnt = cnt + 1

        time.sleep(1)
カテゴリー
プログラム

Googleの検索結果スクレイピング

Googleの検索結果を取得して、内容を表示。

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

# 指定のURLをブラウザで開く
# Google検索結果は取得できるのか?

import webbrowser as wb
import requests
from bs4 import BeautifulSoup

#-------------------------------------------------
## main ###
#-------------------------------------------------
if __name__=='__main__':

    # 100ランク取得
    load_url = "https://www.google.co.jp/search?hl=ja&source=hp&q=携帯+格安&ie=utf-8&oe=utf-8&num=101"

    # HTML取得
    html = requests.get(load_url)
    web_data = BeautifulSoup(html.content, "html.parser")
    list = web_data.findAll(True, {'class': 'BNeawe vvjwJb AP7Wnd'})

    # ランキング表示
    cnt = 0
    for ls in list:
        a = str(ls).strip('<div class="BNeawe vvjwJb AP7Wnd">')
        result_title = a.strip('</')
        print(str(cnt) + ":" + result_title)
        cnt = cnt + 1

カテゴリー
プログラム

Pythonで画像一括リサイズ

Wixサイトで書いてるブログをこちらに移転しようかと思います。新しい記事をこちらにも追加しておきます。

#!/usr/bin/env python3
# -*- coding: utf-8 -*-


import glob
import os.path
from PIL import Image


#-------------------------------------------------
## main ###
#-------------------------------------------------
if __name__=='__main__':

    #
    path = "/*"

    files = glob.glob(path)
    for file in files:
        # 拡張子取得
        root, ext = os.path.splitext(file)
        # .JPGだけ表示
        if ext == ".JPG":
            img = Image.open(file)
            # サイズ固定
            img_resize = img.resize((256, 256))
            img_resize.save(root+"_256.JPG")
            # 縦横比固定
            img_resize = img.resize((img.width//4, img.height//4))
            img_resize.save(root+"_25%.JPG")

指定フォルダ以下の画像をリサイズして名前を変えて保存してます。

カテゴリー
未分類

D-Studio始動

2021年3月よりD-Studioが始動します!

Digital&DesignのDを掲げて、お客様へのサービスを展開します。

[職歴:プログラマー]

・DSPのプログラム開発(アセンブラ)

・PLMシステム開発サポート(C#, VB)

・WEB管理システム開発サポート(HTML, JavaScript)

・施設運用GUI開発(Python, C#, C, LabVIEW)

・機器データ処理ソフト開発(C#, Python, アセンブラ)

・その他多数

[実績]

・HP作成:HPのデザイン、管理、戦略サポート、システムサポート、SEO対策

・ロゴデザイン

・データ解析プログラム作成

・WEBマーケティング:広告管理、SEO対策