270 likes | 286 Views
Understand the different file formats for images such as BMP, JPEG, GIF, PNG, TIFF, HDF, PCX, XWD, ICO, CUR. Learn how to read and manipulate image files in Python using libraries like scipy, numpy, and matplotlib.
E N D
Ch. 2: Image Files and File Types Physically, an image file is a binary file, which can be shown in hexadecimal dump. Example: : img BMP format GIF format hexdump (‘img.bmp’, 64) hexdump (‘img.gif, 64)
An image file contains (a) Header: Characteristics of image e.g., image size, color map, compression method, ….. (b) Image data: pixel values, index values 2.1 File Formats -- Different formats for different application purposes, e.g., transmission, storage, operation, …… BMP (Microsoft Bitmap Pattern) -- without compression, need larger storage
JPEG (Joint Photo-graphics Experts Group) -- lossy compression, need smaller storage GIF (Graphics Interchange Format) -- lossless compression PNG(Portable Network Graphics) TIFF (Tagged Image File Format) HDF (Hierarchical Data Format) PCX (PC Paintbrush) XWD (X Window Dump) ICO (ICOns) CUR (CURsor) 2-3
BMP Format Example:
C/C++ Program • How to read a BMP image?
GIF Format Colors are stored using a color map of 256 colors Pixel data is compressed using LZW compression Allow for multiple images per file A GIF file contains a header including 1. image size (in pixels), 2. color map, 3. color resolution (# bits per pixel), 4. flag indicating whether the color map is ordered, 5. color map size ………………………. -- for communication
Example: GIF format
PNG Format -- Support grayscale, true color, and indexed images Also support Gamma correction: associated different display systems Alpha channels: associate variable transparencies
A PNG file consists of 4 critical chunks: IHDR : image dimensions and bit depth PLTE : color palette IDAT : image data IEND : end 5 ancillary chunks: pHYS: pixel size and aspect ratio bKGD: background color gAMA: gamma sRGB: RGB color space cHRM: primary chromaticites and white point
Example IHDR: 2-13
2.2 Introduction to Python Python was first released in 1991 by Guido van Rossum. ○ Versions: Python 1.1 – 1.5, Python 1.5.2 (1999.4) -------------------------------------------------- Python 3.6.7 (2018.10.20) ○ Installation: (A) https://www.python.org/ (B) google search “Python” download execute
○ Libraries: • Standard library: mathematics, internet protocols, • multimedia, system interfaces, file and data • handling, graphics interface design • Most used libraries: scipy (Scientific Python) : modules for optimization, numerical analysis, signal and image processing, diferential equation solvers. numpy (Numerical Python) : routines for array manipulation, functions for operating on arrays.
matplotlib (Plotting library) : plotting tools. • Many functions have to be imported either by • importing their libraries or the functions needed ○ Interfaces: • IDLE (Integrated Development Environment): • bundled with the language • IPython: includes syntax highlighting, • automatic indentation, inline graphics
Spyder: a graphics development environment with • integration of numpy, scipy, matplotlib, iPython • including a variable and file browser, integrated • editor, interface to Python debugger
2.2.1 Basic Use • Prompt: (a) IDLE - >>>, (b)iPython - In [#]: • Arithmetic: • // - integer division • ^ - bitwise conjunction • ** - exponentiation Examples: In : 37+42 Out : 79 In : 2**67 - 1 Out : 147573952589676412927L?
In : 1.0/7 Out : 0.14285714285714285 In : 11/7 Out : 1 In : 11.0/7 Out : 1.57142857142857714 In : 11.0//7 Out : 1.0 * Python returns a value of the same type as the input
Mathematical functions, in math, must be loaded • before they are available (import the math library) In : importmath Individual libraries (e.g., sqrt) are called as (math.sqrt) In : math.sqrt(3.0) Out :1.7320508075688772 In : from math import exp In : exp(1.5) Out :4.4816890703380645
In : from math import (import all libraries ) In : sqrt(3.0) Out : 1.7320508075688772 In : sin(pi/6) Out : 0.49999999999999994 In : cos(pi/12) Out : 0.9659258262890683 • Import library with an alias In : importmath as m In : m.tan(pi/8) Out : 0.41421356237309503
To see all math functions In : math. tab 2-22
2.2.2 Image I/O • Input images In : importskimage.io as io In : w = io.imread (‘wombats.png’) This takes the grayscale image wombats.pgn and puts it into array w. In : io.imshow (w) This shows array w as a grayscale image. 2-23
Interactive display using ImageViewer method In : from skimage.viewer import ImageViewer as IV In : viewer = IV(w) In : viewer.show ( ) • Directly display the image In : io.imshow (‘wombats.png’)
Output images In : io.imsave (x, ‘filename.abc’) Write an image array x to an image file. e.g., In : io.imsave (c, ‘camerman.pgn’) Write image array c to a PGN image. 2-25
Homework 1: • Input a color image C(R,G,B); • 2. Transform the color image C into a • grayscale image O by O = (R+G+B)/3; • 3. Show images C and O.
(B) Submit the assignment file through Moodle https://moodle.ntnu.edu.tw/ 27 (A) Prepare the assignment file File Name: NAME.HW# , e.g., 陳世旺.HW1 Content: (1) Input/Output images (2) Source code (3) Comments