<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>python | yukin.net</title>
	<atom:link href="https://yukin.net/category/programming/python/feed/" rel="self" type="application/rss+xml" />
	<link>https://yukin.net</link>
	<description>stay curious</description>
	<lastBuildDate>Wed, 27 Dec 2023 04:44:37 +0000</lastBuildDate>
	<language>ja</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9</generator>

<image>
	<url>https://yukin.net/wp-content/uploads/2023/10/cropped-yukin.fabicon-e1697637677176-32x32.png</url>
	<title>python | yukin.net</title>
	<link>https://yukin.net</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>Inkscapeで作ったtiffファイルをpythonで圧縮する</title>
		<link>https://yukin.net/programming/compress_tiff_by_python/</link>
		
		<dc:creator><![CDATA[yukin]]></dc:creator>
		<pubDate>Tue, 26 Dec 2023 04:51:00 +0000</pubDate>
				<category><![CDATA[programming]]></category>
		<category><![CDATA[python]]></category>
		<guid isPermaLink="false">https://yukin.net/?p=1631</guid>

					<description><![CDATA[Inkscapeではtiffでエクスポートするときに圧縮が選べなくてファイルサイズがインフレしたので、Pythonのtifffileでまとめて圧縮したときのメモ 必要なパッケージ コード本体 pathに保存してあるtif [&#8230;]]]></description>
										<content:encoded><![CDATA[
<p>Inkscapeではtiffでエクスポートするときに圧縮が選べなくてファイルサイズがインフレしたので、Pythonの<code>tifffile</code>でまとめて圧縮したときのメモ</p>



<h2 class="wp-block-heading" id="packages"><span>必要なパッケージ</span></h2>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: python; title: Code: ダブルクリックで編集モード; notranslate">
pip install tifffile 
pip install imagecodecs #これがインストールされてなくてハマった
</pre></div>


<h2 class="wp-block-heading" id="code_body"><span>コード本体</span></h2>



<p><code>path</code>に保存してあるtiffファイルをまとめて処理する。</p>



<p>処理後のファイルは<span class="marker-under">_lzw.tiff</span>を付加して同じフォルダに保存している。</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: python; title: Code: ダブルクリックで編集モード; notranslate">
#python
import os
from tifffile import TiffFile, imwrite

# フォルダ内のすべてのTIFFファイルを取得
path = &quot;処理するTIFFファイルが入ってるフォルダへのパス&quot;
files = &#x5B;file for file in os.listdir(path) if file.endswith(&quot;.tif&quot;) or file.endswith(&quot;.tiff&quot;)]

# すべてのTIFFファイルをLZW圧縮されたTIFFファイルに変換
for tiff_file in files:

    # TIFFファイルを読み込む
    full_path = os.path.join(path, tiff_file)
    print(full_path)
    with TiffFile(full_path) as tif:

        #DPI情報をとっておく
        x_dpi = tif.pages&#x5B;0].tags.get(282, None)
        x_dpi = round(x_dpi.value&#x5B;0] / x_dpi.value&#x5B;1])
        y_dpi = tif.pages&#x5B;0].tags.get(283, None)
        y_dpi = round(y_dpi.value&#x5B;0] / y_dpi.value&#x5B;1])
        # unit_dpi = tif.pages&#x5B;0].tags.get(296, None)

        #新しいファイル名を設定
        new_tiff_file = os.path.join(path, os.path.splitext(tiff_file)&#x5B;0] + &quot;_lzw.tiff&quot;)
        #DPIを設定し保存する
        img = tif.asarray()
        imwrite(new_tiff_file, img, compression=&quot;lzw&quot;, resolution=(x_dpi, y_dpi))
</pre></div>


<h2 class="wp-block-heading" id="memo"><span>メモ</span></h2>



<p><code>ggplot2</code>の<code>ggsave()</code>でtiffを書き出すときは<code>compression="lzw"</code>をつければいいが、今回はggplot2で書き出したファイルに少し加工を加える必要があったので、わざわざpythonでやることになった。</p>



<p>Inkscapeも裏はpython動いてるみたいなので、うまくプラグイン的なのを作ることもできそう。</p>
]]></content:encoded>
					
		
		
			</item>
	</channel>
</rss>
