Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update code to match book #4

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions 01_pillow_basics/image_viewer.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

def main():
layout = [
[sg.Image(key="-IMAGE-")],
[sg.Image(key="-IMAGE-", size=(400,400))],
[
sg.Text("Image File"),
sg.Input(size=(25, 1), key="-FILE-"),
Expand All @@ -33,7 +33,7 @@ def main():
image.thumbnail((400, 400))
bio = io.BytesIO()
image.save(bio, format="PNG")
window["-IMAGE-"].update(data=bio.getvalue())
window["-IMAGE-"].update(data=bio.getvalue(), size=(400,400))

window.close()

Expand Down
2 changes: 1 addition & 1 deletion 01_pillow_basics/open_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@
from PIL import Image

image = Image.open("flowers.jpg")
image.show("flowers")
image.show("flowers")
2 changes: 1 addition & 1 deletion 01_pillow_basics/open_image_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@
from PIL import Image

with Image.open("flowers.jpg") as image:
image.show("flowers")
image.show("flowers")
2 changes: 1 addition & 1 deletion 01_pillow_basics/open_image_from_memory.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# open_image_from_memory_2.py
# open_image_from_memory.py

import io
import urllib.request
Expand Down
2 changes: 1 addition & 1 deletion 01_pillow_basics/open_image_from_tar.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@

fobj = TarIO.TarIO("flowers.tar", "flowers.jpg")
image = Image.open(fobj)
image.show()
image.show()
6 changes: 3 additions & 3 deletions 01_pillow_basics/save_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ def image_converter(input_file_path, output_file_path):
image.save(output_file_path)
original_suffix = pathlib.Path(input_file_path).suffix
new_suffix = pathlib.Path(output_file_path).suffix
print(f"Converting {input_file_path} from {original_suffix} " f"to {new_suffix}")

print(f"Converting {input_file_path} from {original_suffix} "
f"to {new_suffix}")

if __name__ == "__main__":
image_converter("flowers.jpg", "flowers.png")
image_converter("flowers.jpg", "flowers.png")
2 changes: 1 addition & 1 deletion 01_pillow_basics/save_image_with_compression.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@ def image_quality(input_file_path, output_file_path, quality):
if __name__ == "__main__":
image_quality("blue_flowers.jpg",
"blue_flowers_compressed.jpg",
quality=95)
quality=95)
2 changes: 1 addition & 1 deletion 01_pillow_basics/save_image_with_new_dpi.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@ def image_converter(input_file_path, output_file_path, dpi):

if __name__ == "__main__":
image_converter("blue_flowers.jpg", "blue_flowers_dpi.jpg",
dpi=(72, 72))
dpi=(72, 72))
8 changes: 3 additions & 5 deletions 01_pillow_basics/thumbnail_maker.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,11 @@
from PIL import Image


def create_thumbnail(input_file_path, thumbnail_path,
thumbnail_size):
def create_thumbnail(input_file_path, thumbnail_path, thumbnail_size):
with Image.open(input_file_path) as image:
image.thumbnail(thumbnail_size)
image.save(thumbnail_path, "JPEG")
image.save(thumbnail_path, format="JPEG")


if __name__ == "__main__":
create_thumbnail("flowers.jpg",
"flowers.thumbnail", (128, 128))
create_thumbnail("flowers.jpg", "flowers.thumbnail", (128, 128))
2 changes: 1 addition & 1 deletion 02_colors/color_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ def get_rgb_value(color_name):

if __name__ == "__main__":
for color in ImageColor.colormap:
print(f"{color} = {get_rgb_value(color)}")
print(f"{color} = {get_rgb_value(color)}")
2 changes: 1 addition & 1 deletion 02_colors/create_4_color.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@ def four_color(input_image_path, output_image_path):


if __name__ == "__main__":
four_color("monarch_caterpillar.jpg", "four_color_caterpillar.png")
four_color("monarch_caterpillar.jpg", "four_color_caterpillar.png")
2 changes: 1 addition & 1 deletion 02_colors/create_bw.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@ def black_and_white(input_image_path, output_image_path):


if __name__ == "__main__":
black_and_white("monarch_caterpillar.jpg", "bw_caterpillar.jpg")
black_and_white("monarch_caterpillar.jpg", "bw_caterpillar.jpg")
2 changes: 1 addition & 1 deletion 02_colors/create_bw_dithering.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@ def black_and_white(input_image_path, output_image_path):


if __name__ == "__main__":
black_and_white("monarch_caterpillar.jpg", "dither_caterpillar.jpg")
black_and_white("monarch_caterpillar.jpg", "dither_caterpillar.jpg")
2 changes: 1 addition & 1 deletion 02_colors/create_grayscale.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@ def grayscale(input_image_path, output_image_path):


if __name__ == "__main__":
grayscale("monarch_caterpillar.jpg", "gray_caterpillar.jpg")
grayscale("monarch_caterpillar.jpg", "gray_caterpillar.jpg")
2 changes: 1 addition & 1 deletion 02_colors/create_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,4 @@ def create_image(path, size):


if __name__ == "__main__":
create_image("lines.png", (150, 150))
create_image("lines.png", (150, 150))
2 changes: 1 addition & 1 deletion 02_colors/create_sepia.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,4 @@ def create_sepia(input_image_path, output_image_path):


if __name__ == "__main__":
create_sepia("monarch_caterpillar.jpg", "sepia_caterpillar.jpg")
create_sepia("monarch_caterpillar.jpg", "sepia_caterpillar.jpg")
2 changes: 1 addition & 1 deletion 02_colors/image_colors.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@ def get_image_colors(image_path):


if __name__ == "__main__":
print(get_image_colors("cape_thick_knee.jpg"))
print(get_image_colors("cape_thick_knee.jpg"))
12 changes: 6 additions & 6 deletions 02_colors/image_converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@
"Black and White": black_and_white,
"Grayscale": grayscale,
"Sepia": sepia,
}
}


def main():
effect_names = list(effects.keys())
layout = [
[sg.Image(key="-IMAGE-", size=(400, 400))],
[sg.Image(key="-IMAGE-", size=(400,400))],
[
sg.Text("Image File"),
sg.Input(size=(25, 1), key="-FILENAME-"),
Expand All @@ -38,7 +38,7 @@ def main():
sg.Combo(
effect_names, default_value="Normal", key="-EFFECTS-",
enable_events=True, readonly=True
),
),
],
[sg.Button("Save")],
]
Expand All @@ -52,13 +52,13 @@ def main():
if event in ["Load Image", "-EFFECTS-"]:
selected_effect = values["-EFFECTS-"]
image_file = values["-FILENAME-"]
if os.path.exists(image_file):
if image_file:
effects[selected_effect](image_file, tmp_file)
image = Image.open(tmp_file)
image.thumbnail((400, 400))
bio = io.BytesIO()
image.save(bio, format="PNG")
window["-IMAGE-"].update(data=bio.getvalue())
window["-IMAGE-"].update(data=bio.getvalue(), size=(400,400))
if event == "Save" and values["-FILENAME-"]:
save_filename = sg.popup_get_file(
"File", file_types=file_types, save_as=True, no_window=True
Expand All @@ -75,4 +75,4 @@ def main():


if __name__ == "__main__":
main()
main()
2 changes: 1 addition & 1 deletion 03_metadata/exif_getter.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@ def get_exif(image_file_path):

if __name__ == "__main__":
exif = get_exif("bridge.JPG")
print(exif)
print(exif)
56 changes: 28 additions & 28 deletions 03_metadata/exif_viewer.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,27 @@
from PIL import Image
from PIL.ExifTags import TAGS

file_types = [("(JPEG (*.jpg)", "*.jpg"),
("All files (*.*)", "*.*")]
file_types = [
("(JPEG (*.jpg)", "*.jpg"),
("All files (*.*)", "*.*"),
]

fields = {
"File name": "File name",
"File size": "File size",
"Model": "Camera Model",
"ExifImageWidth": "Width",
"ExifImageHeight": "Height",
"DateTime": "Creation Date",
"static_line": "*",
"MaxApertureValue": "Aperture",
"ExposureTime": "Exposure",
"FNumber": "F-Stop",
"Flash": "Flash",
"FocalLength": "Focal Length",
"ISOSpeedRatings": "ISO",
"ShutterSpeedValue": "Shutter Speed",
}
"File name": "File name",
"File size": "File size",
"Model": "Camera Model",
"ExifImageWidth": "Width",
"ExifImageHeight": "Height",
"DateTime": "Creation Date",
"static_line": "*",
"MaxApertureValue": "Aperture",
"ExposureTime": "Exposure",
"FNumber": "F-Stop",
"Flash": "Flash",
"FocalLength": "Focal Length",
"ISOSpeedRatings": "ISO",
"ShutterSpeedValue": "Shutter Speed",
}


def get_exif_data(path):
Expand All @@ -49,19 +51,17 @@ def get_exif_data(path):


def main():
layout = [
[
layout = [[
sg.FileBrowse(
"Load image data", file_types=file_types, key="-LOAD-",
enable_events=True
)
]
]
enable_events=True,
)
]]
for field in fields:
layout += [
[sg.Text(fields[field], size=(10, 1)),
sg.Text("", size=(25, 1), key=field)]
]
layout += [[
sg.Text(fields[field], size=(10, 1)),
sg.Text("", size=(25, 1), key=field),
]]
window = sg.Window("Image information", layout)

while True:
Expand All @@ -81,4 +81,4 @@ def main():


if __name__ == "__main__":
main()
main()
2 changes: 1 addition & 1 deletion 03_metadata/gps_exif_getter.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,4 @@ def get_exif(image_file_path):

if __name__ == "__main__":
exif = get_exif("jester.jpg")
print(exif)
print(exif)
2 changes: 1 addition & 1 deletion 03_metadata/tiff_metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@ def get_metadata(image_file_path):

if __name__ == "__main__":
metadata = get_metadata("reportlab_cover.tiff")
print(metadata)
print(metadata)
3 changes: 1 addition & 2 deletions 04_filters/blur_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,5 @@ def blur(input_image, output_image):
filtered_image = image.filter(ImageFilter.BLUR)
filtered_image.save(output_image)


if __name__ == "__main__":
blur("butterfly.jpg", "butterfly_blurred.jpg")
blur("butterfly.jpg", "butterfly_blurred.jpg")
2 changes: 1 addition & 1 deletion 04_filters/boxblur_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@ def boxblur(input_image, output_image):


if __name__ == "__main__":
boxblur("trex.jpg", "trex_boxblur.jpg")
boxblur("trex.jpg", "trex_boxblur.jpg")
14 changes: 14 additions & 0 deletions 04_filters/color_lut_image.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# color_lut_image.py

from PIL import Image
from PIL import ImageFilter


def color_lut(input_image, output_image):
image = Image.open(input_image)
filtered_image = image.filter(ImageFilter.Color3DLUT(2, [0, 1, 2] * 8))
filtered_image.save(output_image)


if __name__ == "__main__":
color_lut("trex.jpg", "trex_lut.jpg")
3 changes: 1 addition & 2 deletions 04_filters/contour_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,5 @@ def contour(input_image, output_image):
filtered_image = image.filter(ImageFilter.CONTOUR)
filtered_image.save(output_image)


if __name__ == "__main__":
contour("flowers_dallas.jpg", "flowers_contour.jpg")
contour("flowers_dallas.jpg", "flowers_contour.jpg")
3 changes: 1 addition & 2 deletions 04_filters/detail_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,5 @@ def detail(input_image, output_image):
filtered_image = image.filter(ImageFilter.DETAIL)
filtered_image.save(output_image)


if __name__ == "__main__":
detail("butterfly.jpg", "detailed_butterfly.jpg")
detail("butterfly.jpg", "detailed_butterfly.jpg")
3 changes: 1 addition & 2 deletions 04_filters/edge_enhance_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,5 @@ def edge_enhance(input_image, output_image):
filtered_image = image.filter(ImageFilter.EDGE_ENHANCE)
filtered_image.save(output_image)


if __name__ == "__main__":
edge_enhance("cactus.jpg", "cactus_edge.jpg")
edge_enhance("cactus.jpg", "cactus_edge.jpg")
2 changes: 1 addition & 1 deletion 04_filters/emboss_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@ def emboss(input_image, output_image):


if __name__ == "__main__":
emboss("hummingbird.jpg", "hummingbird_emboss.jpg")
emboss("hummingbird.jpg", "hummingbird_emboss.jpg")
2 changes: 1 addition & 1 deletion 04_filters/find_edges_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@ def find_edges(input_image, output_image):


if __name__ == "__main__":
find_edges("buffalo.jpg", "buffalo_edges.jpg")
find_edges("buffalo.jpg", "buffalo_edges.jpg")
2 changes: 1 addition & 1 deletion 04_filters/gaussian_blur_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@ def gaussian_blur(input_image, output_image):


if __name__ == "__main__":
gaussian_blur("trex.jpg", "trex_gauss.jpg")
gaussian_blur("trex.jpg", "trex_gauss.jpg")
Loading