Created
September 14, 2019 14:21
-
-
Save Jecosine/34f665e481f65666538cf37b5600ad80 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<component name="InspectionProjectProfileManager"> | |
<settings> | |
<option name="USE_PROJECT_PROFILE" value="false" /> | |
<version value="1.0" /> | |
</settings> | |
</component> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8"?> | |
<project version="4"> | |
<component name="JavaScriptSettings"> | |
<option name="languageLevel" value="ES6" /> | |
</component> | |
<component name="ProjectRootManager" version="2" project-jdk-name="Python 3.7 (piltest)" project-jdk-type="Python SDK" /> | |
</project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8"?> | |
<project version="4"> | |
<component name="ProjectModuleManager"> | |
<modules> | |
<module fileurl="file://$PROJECT_DIR$/.idea/piltest.iml" filepath="$PROJECT_DIR$/.idea/piltest.iml" /> | |
</modules> | |
</component> | |
</project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8"?> | |
<project version="4"> | |
<component name="PySciProjectComponent"> | |
<option name="PY_SCI_VIEW" value="true" /> | |
<option name="PY_SCI_VIEW_SUGGESTED" value="true" /> | |
</component> | |
</project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8"?> | |
<module type="PYTHON_MODULE" version="4"> | |
<component name="NewModuleRootManager"> | |
<content url="file://$MODULE_DIR$" /> | |
<orderEntry type="inheritedJdk" /> | |
<orderEntry type="sourceFolder" forTests="false" /> | |
</component> | |
<component name="PyDocumentationSettings"> | |
<option name="renderExternalDocumentation" value="true" /> | |
</component> | |
<component name="TestRunnerService"> | |
<option name="PROJECT_TEST_RUNNER" value="Unittests" /> | |
</component> | |
</module> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8"?> | |
<project version="4"> | |
<component name="ChangeListManager"> | |
<list default="true" id="e58c64e9-dadc-4090-8616-e7569cadfd31" name="Default Changelist" comment="" /> | |
<option name="EXCLUDED_CONVERTED_TO_IGNORED" value="true" /> | |
<option name="SHOW_DIALOG" value="false" /> | |
<option name="HIGHLIGHT_CONFLICTS" value="true" /> | |
<option name="HIGHLIGHT_NON_ACTIVE_CHANGELIST" value="false" /> | |
<option name="LAST_RESOLUTION" value="IGNORE" /> | |
</component> | |
<component name="FileTemplateManagerImpl"> | |
<option name="RECENT_TEMPLATES"> | |
<list> | |
<option value="Python Script" /> | |
</list> | |
</option> | |
</component> | |
<component name="ProjectId" id="1QmzKHCyIPwIoa2akYWDa0O7ePB" /> | |
<component name="PropertiesComponent"> | |
<property name="WebServerToolWindowFactoryState" value="false" /> | |
<property name="settings.editor.selected.configurable" value="com.jetbrains.python.configuration.PyActiveSdkModuleConfigurable" /> | |
</component> | |
<component name="RunDashboard"> | |
<option name="ruleStates"> | |
<list> | |
<RuleState> | |
<option name="name" value="ConfigurationTypeDashboardGroupingRule" /> | |
</RuleState> | |
<RuleState> | |
<option name="name" value="StatusDashboardGroupingRule" /> | |
</RuleState> | |
</list> | |
</option> | |
</component> | |
<component name="RunManager" selected="Python.test"> | |
<configuration default="true" type="PythonConfigurationType" factoryName="Python"> | |
<module name="piltest" /> | |
<option name="INTERPRETER_OPTIONS" value="" /> | |
<option name="PARENT_ENVS" value="true" /> | |
<envs> | |
<env name="PYTHONUNBUFFERED" value="1" /> | |
</envs> | |
<option name="SDK_HOME" value="" /> | |
<option name="WORKING_DIRECTORY" value="" /> | |
<option name="IS_MODULE_SDK" value="false" /> | |
<option name="ADD_CONTENT_ROOTS" value="true" /> | |
<option name="ADD_SOURCE_ROOTS" value="true" /> | |
<EXTENSION ID="PythonCoverageRunConfigurationExtension" runner="coverage.py" /> | |
<option name="SCRIPT_NAME" value="" /> | |
<option name="PARAMETERS" value="" /> | |
<option name="SHOW_COMMAND_LINE" value="true" /> | |
<option name="EMULATE_TERMINAL" value="false" /> | |
<option name="MODULE_MODE" value="false" /> | |
<option name="REDIRECT_INPUT" value="false" /> | |
<option name="INPUT_FILE" value="" /> | |
<method v="2" /> | |
</configuration> | |
<configuration name="image_process" type="PythonConfigurationType" factoryName="Python" temporary="true"> | |
<module name="piltest" /> | |
<option name="INTERPRETER_OPTIONS" value="" /> | |
<option name="PARENT_ENVS" value="true" /> | |
<option name="SDK_HOME" value="" /> | |
<option name="WORKING_DIRECTORY" value="$PROJECT_DIR$" /> | |
<option name="IS_MODULE_SDK" value="true" /> | |
<option name="ADD_CONTENT_ROOTS" value="true" /> | |
<option name="ADD_SOURCE_ROOTS" value="true" /> | |
<EXTENSION ID="PythonCoverageRunConfigurationExtension" runner="coverage.py" /> | |
<option name="SCRIPT_NAME" value="$PROJECT_DIR$/image_process.py" /> | |
<option name="PARAMETERS" value="" /> | |
<option name="SHOW_COMMAND_LINE" value="true" /> | |
<option name="EMULATE_TERMINAL" value="false" /> | |
<option name="MODULE_MODE" value="false" /> | |
<option name="REDIRECT_INPUT" value="false" /> | |
<option name="INPUT_FILE" value="" /> | |
<method v="2" /> | |
</configuration> | |
<configuration name="image_resize" type="PythonConfigurationType" factoryName="Python" temporary="true"> | |
<module name="piltest" /> | |
<option name="INTERPRETER_OPTIONS" value="" /> | |
<option name="PARENT_ENVS" value="true" /> | |
<envs> | |
<env name="PYTHONUNBUFFERED" value="1" /> | |
</envs> | |
<option name="SDK_HOME" value="" /> | |
<option name="WORKING_DIRECTORY" value="$PROJECT_DIR$" /> | |
<option name="IS_MODULE_SDK" value="true" /> | |
<option name="ADD_CONTENT_ROOTS" value="true" /> | |
<option name="ADD_SOURCE_ROOTS" value="true" /> | |
<EXTENSION ID="PythonCoverageRunConfigurationExtension" runner="coverage.py" /> | |
<option name="SCRIPT_NAME" value="$PROJECT_DIR$/image_resize.py" /> | |
<option name="PARAMETERS" value="" /> | |
<option name="SHOW_COMMAND_LINE" value="false" /> | |
<option name="EMULATE_TERMINAL" value="false" /> | |
<option name="MODULE_MODE" value="false" /> | |
<option name="REDIRECT_INPUT" value="false" /> | |
<option name="INPUT_FILE" value="" /> | |
<method v="2" /> | |
</configuration> | |
<configuration name="image_watermark1" type="PythonConfigurationType" factoryName="Python" temporary="true"> | |
<module name="piltest" /> | |
<option name="INTERPRETER_OPTIONS" value="" /> | |
<option name="PARENT_ENVS" value="true" /> | |
<envs> | |
<env name="PYTHONUNBUFFERED" value="1" /> | |
</envs> | |
<option name="SDK_HOME" value="" /> | |
<option name="WORKING_DIRECTORY" value="$PROJECT_DIR$" /> | |
<option name="IS_MODULE_SDK" value="true" /> | |
<option name="ADD_CONTENT_ROOTS" value="true" /> | |
<option name="ADD_SOURCE_ROOTS" value="true" /> | |
<EXTENSION ID="PythonCoverageRunConfigurationExtension" runner="coverage.py" /> | |
<option name="SCRIPT_NAME" value="$PROJECT_DIR$/image_watermark1.py" /> | |
<option name="PARAMETERS" value="" /> | |
<option name="SHOW_COMMAND_LINE" value="false" /> | |
<option name="EMULATE_TERMINAL" value="false" /> | |
<option name="MODULE_MODE" value="false" /> | |
<option name="REDIRECT_INPUT" value="false" /> | |
<option name="INPUT_FILE" value="" /> | |
<method v="2" /> | |
</configuration> | |
<configuration name="image_watermark2" type="PythonConfigurationType" factoryName="Python" temporary="true"> | |
<module name="piltest" /> | |
<option name="INTERPRETER_OPTIONS" value="" /> | |
<option name="PARENT_ENVS" value="true" /> | |
<envs> | |
<env name="PYTHONUNBUFFERED" value="1" /> | |
</envs> | |
<option name="SDK_HOME" value="" /> | |
<option name="WORKING_DIRECTORY" value="$PROJECT_DIR$" /> | |
<option name="IS_MODULE_SDK" value="true" /> | |
<option name="ADD_CONTENT_ROOTS" value="true" /> | |
<option name="ADD_SOURCE_ROOTS" value="true" /> | |
<EXTENSION ID="PythonCoverageRunConfigurationExtension" runner="coverage.py" /> | |
<option name="SCRIPT_NAME" value="$PROJECT_DIR$/image_watermark2.py" /> | |
<option name="PARAMETERS" value="" /> | |
<option name="SHOW_COMMAND_LINE" value="false" /> | |
<option name="EMULATE_TERMINAL" value="false" /> | |
<option name="MODULE_MODE" value="false" /> | |
<option name="REDIRECT_INPUT" value="false" /> | |
<option name="INPUT_FILE" value="" /> | |
<method v="2" /> | |
</configuration> | |
<configuration name="test" type="PythonConfigurationType" factoryName="Python" temporary="true"> | |
<module name="piltest" /> | |
<option name="INTERPRETER_OPTIONS" value="" /> | |
<option name="PARENT_ENVS" value="true" /> | |
<option name="SDK_HOME" value="" /> | |
<option name="WORKING_DIRECTORY" value="$PROJECT_DIR$" /> | |
<option name="IS_MODULE_SDK" value="true" /> | |
<option name="ADD_CONTENT_ROOTS" value="true" /> | |
<option name="ADD_SOURCE_ROOTS" value="true" /> | |
<EXTENSION ID="PythonCoverageRunConfigurationExtension" runner="coverage.py" /> | |
<option name="SCRIPT_NAME" value="$PROJECT_DIR$/test.py" /> | |
<option name="PARAMETERS" value="" /> | |
<option name="SHOW_COMMAND_LINE" value="true" /> | |
<option name="EMULATE_TERMINAL" value="false" /> | |
<option name="MODULE_MODE" value="false" /> | |
<option name="REDIRECT_INPUT" value="false" /> | |
<option name="INPUT_FILE" value="" /> | |
<method v="2" /> | |
</configuration> | |
<recent_temporary> | |
<list> | |
<item itemvalue="Python.test" /> | |
<item itemvalue="Python.image_process" /> | |
<item itemvalue="Python.image_resize" /> | |
<item itemvalue="Python.image_watermark1" /> | |
<item itemvalue="Python.image_watermark2" /> | |
</list> | |
</recent_temporary> | |
</component> | |
<component name="SvnConfiguration"> | |
<configuration /> | |
</component> | |
<component name="TaskManager"> | |
<task active="true" id="Default" summary="Default task"> | |
<changelist id="e58c64e9-dadc-4090-8616-e7569cadfd31" name="Default Changelist" comment="" /> | |
<created>1568390493925</created> | |
<option name="number" value="Default" /> | |
<option name="presentableId" value="Default" /> | |
<updated>1568390493925</updated> | |
<workItem from="1568390496335" duration="32987000" /> | |
</task> | |
<servers /> | |
</component> | |
<component name="TypeScriptGeneratedFilesManager"> | |
<option name="version" value="1" /> | |
</component> | |
<component name="com.intellij.coverage.CoverageDataManagerImpl"> | |
<SUITE FILE_PATH="coverage/piltest$image_watermark1.coverage" NAME="image_watermark1 Coverage Results" MODIFIED="1568435458011" SOURCE_PROVIDER="com.intellij.coverage.DefaultCoverageFileProvider" RUNNER="coverage.py" COVERAGE_BY_TEST_ENABLED="true" COVERAGE_TRACING_ENABLED="false" WORKING_DIRECTORY="$PROJECT_DIR$" /> | |
<SUITE FILE_PATH="coverage/piltest$image_process.coverage" NAME="image_process Coverage Results" MODIFIED="1568448089999" SOURCE_PROVIDER="com.intellij.coverage.DefaultCoverageFileProvider" RUNNER="coverage.py" COVERAGE_BY_TEST_ENABLED="true" COVERAGE_TRACING_ENABLED="false" WORKING_DIRECTORY="$PROJECT_DIR$" /> | |
<SUITE FILE_PATH="coverage/piltest$image_convert.coverage" NAME="image_convert Coverage Results" MODIFIED="1568430994700" SOURCE_PROVIDER="com.intellij.coverage.DefaultCoverageFileProvider" RUNNER="coverage.py" COVERAGE_BY_TEST_ENABLED="true" COVERAGE_TRACING_ENABLED="false" WORKING_DIRECTORY="$PROJECT_DIR$" /> | |
<SUITE FILE_PATH="coverage/piltest$image_thumbnail.coverage" NAME="image_thumbnail Coverage Results" MODIFIED="1568434089582" SOURCE_PROVIDER="com.intellij.coverage.DefaultCoverageFileProvider" RUNNER="coverage.py" COVERAGE_BY_TEST_ENABLED="true" COVERAGE_TRACING_ENABLED="false" WORKING_DIRECTORY="$PROJECT_DIR$" /> | |
<SUITE FILE_PATH="coverage/piltest$myutils.coverage" NAME="myutils Coverage Results" MODIFIED="1568428395877" SOURCE_PROVIDER="com.intellij.coverage.DefaultCoverageFileProvider" RUNNER="coverage.py" COVERAGE_BY_TEST_ENABLED="true" COVERAGE_TRACING_ENABLED="false" WORKING_DIRECTORY="$PROJECT_DIR$" /> | |
<SUITE FILE_PATH="coverage/piltest$image_resize.coverage" NAME="image_resize Coverage Results" MODIFIED="1568436368390" SOURCE_PROVIDER="com.intellij.coverage.DefaultCoverageFileProvider" RUNNER="coverage.py" COVERAGE_BY_TEST_ENABLED="true" COVERAGE_TRACING_ENABLED="false" WORKING_DIRECTORY="$PROJECT_DIR$" /> | |
<SUITE FILE_PATH="coverage/piltest$image_watermark2.coverage" NAME="image_watermark2 Coverage Results" MODIFIED="1568434564071" SOURCE_PROVIDER="com.intellij.coverage.DefaultCoverageFileProvider" RUNNER="coverage.py" COVERAGE_BY_TEST_ENABLED="true" COVERAGE_TRACING_ENABLED="false" WORKING_DIRECTORY="$PROJECT_DIR$" /> | |
</component> | |
</project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import myutils as ut | |
from PIL import Image | |
''' | |
@para path: <string> Directory where images you want to covert in | |
@para outpath: <string> Directory where you want to put the converted file in | |
''' | |
# From jpg to png | |
def jpg2png(path, outpath): | |
imlist = ut.get_imlist(path, ".jpg") | |
# Check whether outpath exists | |
try: | |
assert(os.path.exists(outpath)) | |
except: | |
print("{0} path not exists".format(outpath)) | |
# Format outpath | |
for i in imlist: | |
fname = os.path.join(outpath, os.path.splitext(os.path.split(i)[-1])[0] + ".png") | |
try: | |
Image.open(i).save(fname) | |
except Exception as e: | |
print("Cannot convert file {0} due to error: {1}".format(i, e)) | |
else: | |
print("{0} converted".format(i)) | |
# From png to jpg | |
def png2jpg(path, outpath): | |
imlist = ut.get_imlist(path, ".png") | |
# Check whether outpath exists | |
try: | |
assert(os.path.exists(outpath)) | |
except: | |
print("{0} path not exists".format(outpath)) | |
for i in imlist: | |
fname = os.path.join(outpath, os.path.splitext(os.path.split(i)[-1])[0] + ".jpg") | |
try: | |
Image.open(i).convert("RGB").save(fname) | |
except Exception as e: | |
print("Cannot convert file {0} due to {1}".format(i, e)) | |
else: | |
print("{0} converted".format(i)) | |
if __name__ == "__main__": | |
png2jpg("images", "converted") | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from PIL import Image | |
from numpy import * | |
from scipy import ndimage | |
''' | |
First implement: use numpy to convert image to an array, then rebuild an image copy from this array | |
@para im: <class 'PIL.PngImagePlugin.PngImageFile'> image object that you want to copy | |
''' | |
# def copy(im): | |
# imdata = array(im) | |
# clone = Image.fromarray(imdata) | |
# clone.show() | |
# return clone | |
''' | |
Second implement: use nested-loop to iterate each pixel | |
@para im: <class 'PIL.PngImagePlugin.PngImageFile'> image object that you want to copy | |
''' | |
def copy(im): | |
width, height = im.size | |
clone = Image.new(im.mode, im.size) | |
for i in range(width): | |
for j in range(height): | |
pixel = im.getpixel((i,j)) | |
clone.putpixel((i,j), pixel) | |
clone.show() | |
return clone | |
''' | |
First implement: use numpy to convert image to an array, then rebuild an image copy from this array | |
@para im: <class 'PIL.PngImagePlugin.PngImageFile'> image object that you want to copy | |
@para crop_box: <tuple> a 4d tuple include two point (x1, y1, x2, y2) | |
''' | |
def crop(im, crop_box): | |
x1, y1, x2, y2 = crop_box | |
x1, x2 = x1 > x2 and (x2, x1) or (x1, x2) | |
y1, y2 = y1 > y2 and (y2, y1) or (y1, y2) | |
# Judge whether input is valid | |
if x2 > im.size[0] or y2 > im.size[1] or x1 < 0 or y1 < 0: | |
print("position overlap") | |
return None | |
imdata = array(im) | |
print(x1,y1,x2,y2) | |
croped_data = imdata[y1: y2, x1: x2] | |
croped = Image.fromarray(croped_data) | |
croped.show() | |
return croped | |
''' | |
Second implement: use nested-loop to iterate each pixel | |
@para im: <class 'PIL.PngImagePlugin.PngImageFile'> image object that you want to crop | |
@para crop_box: <tuple> a 4d tuple include two point (x1, y1, x2, y2) | |
''' | |
# def crop(im, crop_box): | |
# w, h = im.size | |
# x1, y1, x2, y2 = crop_box | |
# x1, x2 = x1 > x2 and (x2, x1) or (x1, x2) | |
# y1, y2 = y1 > y2 and (y2, y1) or (y1, y2) | |
# # Judge whether input is valid | |
# if x2 > w or y2 > h or x1 < 0 or y1 < 0: | |
# print("position overlap") | |
# return None | |
# | |
# croped = Image.new(im.mode, (x2 - x1, y2 - y1)) | |
# for i in range(x1, x2): | |
# for j in range(y1, y2): | |
# pixel = im.getpixel((i,j)) | |
# croped.putpixel((i - x1, j - y1), pixel) | |
# croped.show() | |
# return croped | |
''' | |
First implement: use numpy to convert image to an array, then rebuild an image copy from this array | |
@para im: <class 'PIL.PngImagePlugin.PngImageFile'> image object that you want to copy | |
@para mode: <bool> default value is True, imply to Horizontal flip, while False imply to Vertical | |
''' | |
def flip(im, mode = True): | |
imdata = array(im) | |
flipedata = imdata[::, ::-1] if mode else imdata[::-1, ::] | |
fliped = Image.fromarray(flipedata) | |
fliped.show() | |
return fliped | |
''' | |
Second implement: use nested-loop to iterate each pixel | |
@para im: <class 'PIL.PngImagePlugin.PngImageFile'> image object that you want to flip | |
@para mode: <bool> default value is True, imply to Horizontal flip, while False imply to Vertical | |
''' | |
# def flip(im, mode = True): | |
# w, h = im.size | |
# if mode: | |
# _w = w // 2 | |
# for i in range(_w): | |
# for j in range(h): | |
# pixel = im.getpixel((i,j)) | |
# im.putpixel((i, j), im.getpixel((w - i - 1, j))) | |
# im.putpixel((w - i - 1, j), pixel) | |
# | |
# else: | |
# _h = h // 2 | |
# for i in range(w): | |
# for j in range(_h): | |
# pixel = im.getpixel((i, j)) | |
# im.putpixel((i, j), im.getpixel((i, h - j - 1))) | |
# im.putpixel((i, h - j - 1), pixel) | |
# im.show() | |
# return im | |
''' | |
First implement: use numpy to convert image to an array, then rebuild an image copy from this array | |
@para im: <class 'PIL.PngImagePlugin.PngImageFile'> image object that you want to rotate | |
@para mode: <bool> default value is True, imply to rotate 90 degree in clockwise , while False imply to in anticlockwise | |
''' | |
def rotate(im, mode = True): | |
imdata = array(im) | |
rotatedata = imdata[::-1].transpose((1, 0, 2)) if mode else imdata.transpose(1, 0, 2)[::-1] | |
rotated = Image.fromarray(rotatedata) | |
rotated.show() | |
return rotated | |
''' | |
Second implement: use nested-loop to iterate each pixel | |
@para im: <class 'PIL.PngImagePlugin.PngImageFile'> image object that you want to rotate | |
@para mode: <bool> default value is True, imply to rotate 90 degree in clockwise , while False imply to in anticlockwise | |
''' | |
def rotate(im, mode = True): | |
w, h = im.size | |
rotated = Image.new(im.mode, im.size[::-1]) | |
w, h = im.size | |
for i in range(w): | |
for j in range(h): | |
pixel = im.getpixel((i, j)) | |
if mode: | |
rotated.putpixel((h - j - 1, i), pixel) | |
else: | |
rotated.putpixel((j, w - i - 1), pixel) | |
rotated.show() | |
return rotated | |
''' | |
@para im: <class 'PIL.PngImagePlugin.PngImageFile'> image object that you want to smooth | |
''' | |
def smooth(im): | |
smoothed = Image.new(im.mode, im.size) | |
w, h = im.size | |
# print(im.getpixel((0, 0))) | |
#kernel = array([[[1/9, 1/9, 1/9],[1/9, 1/9, 1/9],[1/9, 1/9, 1/9]],[[1/9, 1/9, 1/9],[1/9, 1/9, 1/9],[1/9, 1/9, 1/9]],[[1/9, 1/9, 1/9],[1/9, 1/9, 1/9],[1/9, 1/9, 1/9]]]) | |
kernel = ones((3, 3, 3)) / 27 | |
imdata = array(im) | |
temp = ndimage.convolve(imdata, kernel) | |
print(temp.dtype) | |
smoothed = Image.fromarray(temp) | |
smoothed.show() | |
return smoothed | |
# def smooth(im): | |
# smoothed = Image.new(im.mode, im.size) | |
# w, h = im.size | |
# # print(im.getpixel((0, 0))) | |
# for i in range(w): | |
# for j in range(h): | |
# smoothed.putpixel((i, j), get_average(im, i, j)) | |
# smoothed.show() | |
# return smoothed | |
def get_average(im, i, j): | |
w, h = im.size | |
r, g, b = 0, 0, 0 | |
count = 0 | |
for i in range(max(i - 1, 0), min(w, i + 1)): | |
for j in range(max(j - 1, 0), min(j + 1, h)): | |
pixel = im.getpixel((i, j)) | |
r += pixel[0] | |
g += pixel[1] | |
b += pixel[2] | |
count += 1 | |
return (r // count, g // count, b // count) | |
# Example | |
if __name__ == "__main__": | |
im = Image.open("images/02.jpg") | |
smooth(im) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import myutils as ut | |
from PIL import Image | |
import os | |
''' | |
@para path: <string> select a directory to process all jpg | |
@para size: <list> a list of length 2 to describe the image size (width, height) | |
''' | |
def resize(path, size=(640, 480)): | |
# Default size of thumbnail is 128, 128 | |
imlist = ut.get_imlist(path, ".jpg") | |
for i in imlist: | |
fname = os.path.splitext(i)[0] + "_{0}.jpg".format(size[0]) | |
try: | |
im = Image.open(i) | |
im = im.resize(size, Image.ANTIALIAS) | |
im.save(fname) | |
except Exception as e: | |
print("Cannot resize {0} due to error(s): {1}".format(i, e)) | |
else: | |
print("{0} is resized to {1[0]}, {1[1]}".format(i, size)) | |
# Example | |
if __name__ == "__main__": | |
resize("images") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
from PIL import Image | |
import myutils as ut | |
def thumbnail(path, size=(128, 128)): | |
# Default size of thumbnail is 128, 128 | |
imlist = ut.get_imlist(path, ".jpg") | |
for i in imlist: | |
fname = os.path.splitext(i)[0] + "_s.jpg" | |
try: | |
im = Image.open(i) | |
im.thumbnail(size, Image.ANTIALIAS) | |
im.save(fname) | |
except Exception as e: | |
print("Cannot create thumbnail of file {0} due to error(s): {1}".format(i, e)) | |
else: | |
print("Thumbnail of {0} created".format(i)) | |
if __name__ == "__main__": | |
thumbnail("images") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from PIL import Image, ImageDraw, ImageFont | |
import myutils as ut | |
import os | |
''' | |
add_watermarks adds the image-type logo to origin images in specified directory | |
@para path: <string> select a directory to process all jpg | |
@para logopath: <string> select a image's path which used as water mark | |
@para logo_size: <list> a list of length 2 to describe the logo size (width, height) | |
@para position: <list> a list with length 2 | |
''' | |
def add_watermarks(path, logopath, logo_size = (200, 100), position = (0, 0)): | |
imlist = ut.get_imlist(path, ".jpg") | |
for i in imlist: | |
fname = os.path.splitext(i)[0] + '_w.jpg' | |
im = Image.open(i).convert('RGBA') | |
logo = Image.open(logopath).convert('RGBA') | |
try: | |
out = add_watermark_single(im, logo, logo_size, position) | |
out.convert('RGB').save(fname) | |
except Exception as e: | |
print("Cannot convert file.Error info: {0}".format(i), e) | |
else: | |
print("{0} coverted".format(i)) | |
''' | |
logo_resize resize the logo image to fit the image size | |
@para logo: <class 'PIL.PngImagePlugin.PngImageFile'> image used as water mark | |
@para logo_size: <list> a list of length 2 to describe the logo size (width, height) | |
''' | |
def logo_resize(logo, logo_size): | |
return logo.resize(logo_size) | |
''' | |
add_watermark_single add water mark to one image | |
@para im: <class 'PIL.PngImagePlugin.PngImageFile'> origin image | |
@para logo: <class 'PIL.PngImagePlugin.PngImageFile'> image used as water mark | |
@para position: <list> a list with length 2 | |
''' | |
def add_watermark_single(im, logo, logo_size, position): | |
panel = Image.new("RGBA", im.size, (0, 0, 0, 0)) | |
logo = logo_resize(logo, logo_size) | |
panel.paste(logo, position) | |
output = Image.alpha_composite(im, panel) | |
return output | |
# Example | |
if __name__ == "__main__": | |
add_watermarks("images", "images/python-logo.png") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from PIL import Image, ImageDraw, ImageFont | |
import myutils as ut | |
import os | |
''' | |
@para path: <string> select a directory to process all jpg | |
@para content: <string> content of watermark | |
@para size: <int> font size | |
@para position: <list> a list with length 2 | |
@para color: <list> a list with length 4 | |
@para fpath: <string> the path of font | |
''' | |
def add_watermarks(path, content = 'Python', size = 20, position = (0, 0), color = (255,255,255,100), fpath = "fonts/LucidaTypewriterRegular.ttf"): | |
# Get image list of specified directory | |
imlist = ut.get_imlist(path, ".jpg") | |
for i in imlist: | |
fname = os.path.splitext(i)[0] + '_w.jpg' | |
im = Image.open(i).convert('RGBA') | |
try: | |
out = add_watermark_single(im, content, size, position, color, fpath) | |
#out is RGBA after alpha composite with watermark | |
out.convert("RGB").save(fname) | |
except Exception as e: | |
print("Cannot add water mark to {0}, due to error(s): {1}".format(i, e)) | |
else: | |
print("Water mark added to {0}".format(i)) | |
''' | |
@para im: <class 'PIL.PngImagePlugin.PngImageFile'> origin image | |
''' | |
def add_watermark_single(im, content, size, position, color, fpath): | |
panel = Image.new("RGBA", im.size, (0,0,0,0)) | |
font = ImageFont.truetype(fpath, size) | |
mask = ImageDraw.Draw(panel) | |
mask.text(position, content, fill = color, font = font) | |
output = Image.alpha_composite(im, panel) | |
return output | |
#Example | |
if __name__ == "__main__": | |
add_watermarks(path = "images") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import re | |
# Use regular expression to get image which is before process (filter images' names end with '_w', '_s'...) | |
ban_ext = ['_w', '_s', '_640'] | |
pattern = '' | |
for i in ban_ext: | |
pattern += "(?:{0})".format(i.strip()) + '|' | |
pattern = pattern[:-1] | |
def get_imlist(path, ext): | |
global pattern | |
pattern += ext | |
# print pattern | |
return [os.path.join(path, f) for f in os.listdir(path) if f.endswith(ext) and not re.search(pattern, f)] | |
# Example | |
if __name__ == "__main__": | |
print(get_imlist("images", ".jpg")) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from PIL import Image, ImageFilter | |
import numpy | |
from matplotlib import pyplot as plt | |
im = Image.open("images/test.jpg") | |
contour = im.filter(ImageFilter.CONTOUR) | |
emboss = im.filter(ImageFilter.EMBOSS) | |
edged = im.filter(ImageFilter.FIND_EDGES) | |
figure = plt.figure(figsize=(10, 10)) | |
# Show origin | |
ax1 = figure.add_subplot(221) | |
ax1.imshow(im) | |
plt.title("Origin") | |
plt.axis('off') | |
# Show contour | |
ax2 = figure.add_subplot(222) | |
ax2.imshow(contour) | |
plt.title("Contour") | |
plt.axis('off') | |
# Show emboss | |
ax3 = figure.add_subplot(223) | |
ax3.imshow(emboss) | |
plt.title("Emboss") | |
plt.axis('off') | |
# Show Edged | |
ax4 = figure.add_subplot(224) | |
ax4.imshow(edged) | |
plt.title("Edged") | |
plt.axis('off') | |
figure.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment