Sunday, October 13, 2024

Do these libraries PIL and OpenCV produce exactly the same image, or will there be changes in the rendered images?

 In earlier posts we have seen how images can be displayed using PIL and OpenCV.

Now the question is will these two methods look at the same input image and display the same way?

Here is the chosen image in PNG format, I just created using PAINT. The idea is colors may change, but the text will remain same.

----------------------------------------------------

Using OpenCV

import cv2


# Load the image from file

image = cv2.imread(r'C:\Users\hoden\PycharmProjects\exploreImage\Images_2\RGB.png'

# Display the image in a window

cv2.imshow('Loaded Image', image)

# Wait for a key press and close the window

cv2.waitKey(0)

cv2.destroyAllWindows()

-------------

Using PILLow or PILL

from PIL import Image

# Open an image file

image = Image.open(r'C:\Users\hoden\PycharmProjects\exploreImage\Images_2\RGB.png')

# Display the image

image.show()

---------------------------------------------

The images displayed by the two codes are as shown:



Displayed Image: Using OpenCV
Displayed Image: Using PIl

What I found using PyCharm and a chosen image was that there was no difference. They displayed the same way without any changes to color. The only difference is that the image displayed using OpenCV has a slight cut-off in the left side. This may be corrected by placing the image in the center of a predesigned canvas.

What do the AI assistants say:

However, I decided to quiz the AI tools available on the web Co_Pilot, Gemini and Meta AI.

Gemini's answer:

In most cases, these two methods will display the image with minimal to no noticeable differences. However, there are some potential scenarios where there might be slight variations:

1. Color Space:

OpenCV: By default, OpenCV loads images in BGR (Blue, Green, Red) color space.

Pillow: Pillow loads images based on the image format's native color space (usually RGB for common formats).

This difference is usually negligible for human perception, but it's important to be aware of if you're doing further processing that relies on specific color channels. You can convert the color space in OpenCV using cv2.cvtColor(image, cv2.COLOR_BGR2RGB).

note: Looks like it is not the default behaviour.

2. Alpha Channel Handling:

Images with an alpha channel (transparency information) might be handled slightly differently between the libraries. In most cases, both will display the image with transparency preserved, but there could be subtle differences in how the alpha channel information is interpreted.

3. Advanced Image Formats:

Some uncommon image formats may be supported by one library but not the other, leading to different behavior. However, common formats like JPEG, PNG, and BMP should be handled identically.

In conclusion:

For most display purposes, the two methods will produce visually identical results. However, if you need precise control over color space, alpha channel handling, or are working with uncommon formats, understanding the potential differences between OpenCV and Pillow is important.


Co-Pilot's answer:

The two methods of displaying an image using OpenCV and PIL (Pillow) will not produce exactly the same rendered image due to differences in how each library handles image loading and display. Here are the key differences:

OpenCV

Color Format: OpenCV loads images in BGR format by default. This means that the colors might appear different if you don’t convert the image to RGB before displaying it.

Display Window: OpenCV uses its own windowing system (cv2.imshow) to display images. This window is managed by OpenCV and has its own look and feel.

PIL (Pillow)

Color Format: PIL loads images in RGB format by default. This means that the colors will be displayed as expected without any need for conversion.

Display Method: PIL uses the default image viewer of your operating system to display images (image.show()). This means the image will be displayed in whatever application is set as the default for viewing images on your system.

Summary

Color Handling: OpenCV displays images in BGR format, which can cause color inversion if not converted to RGB. PIL displays images in RGB format.

Display Method: OpenCV uses its own window, while PIL relies on the system’s default image viewer



Meta AI's answer:

Do OpenCV and PIL display the same image in the same way without any color differences?

OpenCV and PIL (Python Imaging Library) handle image display differently, which can result in color differences.

*OpenCV:*


1. Default color space: BGR (Blue, Green, Red)

2. Displays images in BGR format

3. Uses cv2.imshow() function for display


*PIL:*


1. Default color space: RGB (Red, Green, Blue)

2. Displays images in RGB format

3. Uses Image.show() or Image.display() functions

Due to this difference in color spaces:

- Images displayed using OpenCV will have a different color representation compared to PIL.


It is being said that the AI agents greatly help in computer coding. I would say, although they may be working from the same databank of information, they need not provide the same answer. The developer has to know stuff beyond just the codes.


Wednesday, October 2, 2024

How do I install OpenCV to study images?

 Previously, we have seen how to install OpenCV using PIP.

Now we will see how to work with OpenCV.

In PyCharm, the default python file name is main.py. I already have a main.py and I will create a new python file as shown in the picture. 


When you create a new python file you can give a name to it, main-OpenCV.py herein.

main_opencv.py gets added to the project. The code window on the right is empty and starts at line 1.


Now let us look at the same image as in the previous using PIL.

Run the following code in main-OpenCV as shown.

------------

import cv2

# Load the image from file

image = cv2.imread('Images/TheKiss.jpg')


# Display the image in a window

cv2.imshow('Loaded Image', image)


# Wait for a key press and close the window

cv2.waitKey(0)

cv2.destroyAllWindows()

-----------------------

It takes a short while to generate the image as shown.

You would have noticed the difference between displaying using PIL and OpenCV. The method imshow() is a part of the matplotlib library but has also become a part of the OpenCV.  The imshow() creates a window managed by OpenCV in the context of the OpenCV applications. However the PIL program uses the default viewer settings that can change depending on the settings and the OS.

The images displayed by the two methods can be different as well.


Tuesday, October 1, 2024

How do you display an image using PILL?

PIL(Pillow) is another library that can be used to display images. Pillow can also be installed using pip in the Terminal as shown by running the code pip install Pillow


After installing Pillow , you can display the image using python code as shown.

---------------

from PIL import Image

# open the image file

image=Image.open('Images/TheKiss.jpg')

#Display the image

image.show()

-----------

When you run this code the image gets displayed.


When you use Image.open(), you should give the path either the absolute path or the path relative to the project, in the above the relative project is given ('Images/TheKiss.jpg).

Here is an example of using the absolute path (in the file system)

-------------

PIL import Image


# Open an image file image = Image.open(r'C:\Users\hoden\PycharmProjects\exploreImage\Images
\TheKiss.jpg')

# Display the image
image.show()

-----------

You can see  the absolute path is used. The code gets run and you see the same image. 



If you omit the 'r' in front of the absolute path, you will get an Unicode error. The 'r' tells the interpreter to treat it as 'raw' text.

If you right click the image you can access the image path, both relative and absolute.



PyCharm does a pretty good job providing timely help with drop-down hints, code completion and syntax highlighting.










Sunday, September 29, 2024

If your python environment does not have pip installed. What then?

Over a period of time, there was some computer problem which needed complete recovery. Before this event, I had installed PyCharm and had everything working without problem and I had even installed PIL library from within PyCharm. However, after the recovery, I lost some of the files.

Besides, PIL I wanted to install other libraries to work with images. When I tried to open an existing project in PyCharm, I encountered errors.

All Python packages were absent and the program was asking me for a Python interpreter.

What happened to my python?

Before proceeding to other image related libraries, I had to fix this problem.

If your Python environment does not have pip installed you need to install pip first. There are two ways to install pip supported by the pip's maintainers:

ensurepip       -------->run py -3 -m ensurepip

or run get-pip.py------->run get-pip.py using python interpreter

Because of the recovery created problem, I did not have python interpreter according to PyCharm (as shown in the first image above).

I need to install python even before I can install pip.

I tried to find python.dll which I could not as I had lost it during recovery.

Finally, I found it in the Windows.old folder [C:\Windows.old07042024\Users\hoden\AppData\Local ]

I copied the python.exe from the windows.old folder to Windows/system32.

 I tried to run from the command-line with administrative privileges:

-------

C:\Users\hoden\OneDrive\Desktop\PyCharm\PIL>python get-pip.py

Could not find platform independent libraries <prefix>

Collecting pip

  Downloading pip-24.2-py3-none-any.whl.metadata (3.6 kB)

Downloading pip-24.2-py3-none-any.whl (1.8 MB)

   ---------------------------------------- 1.8/1.8 MB 33.0 MB/s eta 0:00:00

Installing collected packages: pip

  WARNING: The scripts pip.exe, pip3.12.exe and pip3.exe are installed in 'C:\Users\hoden\OneDrive\Desktop\PyCharm\PIL\Scripts' which is not on PATH.

  Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location.

Successfully installed pip-24.2

-----------

I ran into another problem because of the 'Onedrive'. Permit me to call it 'oneDrive" interference.

Finally, I copied the python folder into Windows\System32 of local computer and restarted the computer(laptop). The problem was resolved as seen in PyCharm.

Finally, both the Python Console and the Python packages are all back in PyCharm.

More here:




 

Monday, September 23, 2024

How do you test reliability of I/O for installing a SQL Server?

SQLIOSIM is a tool for simulating SQL Server IO. SQLIOSIM performs reliability and integrity tests on the disk systems that SQL Server utilizes for data and log files. 

It simulates,

Read

Write

Checkpoint

Backup

Sort and 

Read-ahead activities of MS SQL Server.

Main objective of SQLIOSIM is to test the reliability of the underlying I/O subsystem even before installing SQL Server. This proactive tool helps preparing well for the installation. SQL Server installation is not needed for using it as it does not interact with the SQL Server. If an installation exists, using it may overwrite the files. It is not a tool for data security. It is an upgrade over the previous SQ7IOStress utility.

It is located in the Program Files/Microsoft SQL Server/MSSql16.<Named Instance>MSSQL/BINN folder of your installation.

In the installation described here, there are two SQLIOSIM related files in the BINN folder.

SQLIOSIM Application (SqLIOSIM.exe)----GUI

SQLIOSIM DOS file (SQLIOSIM.com)--DOS 

For using SQLIOSIM.com you need to configure the SQLIOSIM configuration file.


What if you cannot locate the file?

If the file is not found as in this installation, it can be found in GitHub. However, it is not found on the GitHub and one may need to go to Microsoft or raise the issue. As we see later, there may be a way out using the GUI.


https://github.com/microsoft/mssql-support/blame/master/sqliosim/Readme.md


The SQLIOSIM gui can be launched using the SQLIOSIM.exe application in the BINN folder.

Double click the SQLIOSIM application in the BINN folder to launch the GUI.


Click SIMULATOR t open the Simulator. Simulator->Configure will open the Files and Configuration GUI as shown.

From here on it can be used to configure the .mdx and .ldx file locations, size  and max size and Increment parameters on the computer and test patterns for integrity testing can be carried out. Do not choose the existing .mdx and .ldx file locations as they will be overwritten during testing.












Monday, September 16, 2024

How do you custom Install SQL Server 2022 on a laptop?

  In Part 1 we looked at hardware and software requirements and determined that the Surface Pro laptop could satisfy the requirements for the SQL Server 2022 installation.

If you are looking to working on your SQL Server in an isolated mode to gain some experience, you may not need to worry about a number of items in the Planning node of the SQL Server Installation center such as,

  • Azure extension for SQL server (New)
  • Download Data Migration Assistant(DMA)
  • How to get started with SQL Server 2022 Failover Clustering
  • Upgrade Documentation
  • Download SQL Server Migration Assistant (SSMA)

However, make sure you go through the other items. You could always bring up the SQL Server Installation Center later if you need to.

The next item on the SQL Server Installation center is Installation. A click on the installation node in the SQL Server Installation enter brings up this page (window).


On this page, these items are important, 

  • New SQL Server standalone installation or add features to an existing installation.
  • Install SQL Server Reporting Services
  • Install SQL Server Management Tools
  • Install SQL Server Data Tools

If this is a fresh install, you will be using the New SQL Server standalone installation, the first item's option.


Click "New SQL Server standalone installation"

The installation begins and you will be getting messages regarding installation progress, you just wait them out.

After a while you get his page(window).



Since we are using a free edition, just choose the first option Specify a free edition. However, have a look at the two other options

Also, note on the navigation items on the left of this page. The installation goes through each of these items. It may skip through some items that are not relevant.

Click the button Next> at the bottom. As the case may be, sometime you want to go back to the previous screen and a back button is present.

The following page is displayed regarding license terms.


If you accept by checking the "I accept..." control at the bottom you can install. Some of your data may be collected by the software. It may be prudent to keep a copy of the license terms should you be interested.

Click on the Next> button. As you may have observed, it skips the Global Rules step in the installation and goes to Microsoft Update. 


Accept the recommendation of Microsoft and place a checkmark. Here, you have an option to go to the shown links to check out FAQ and Privacy related statements.

Click Next>. It processes your request rapidly and skips Product Updates, to Install Setup Files .


After Install Setup Files the Install Rules page gets displayed. You may see Windows Powershell highlighted. You need not worry as Windows Powershell is already installed on Windows 11 Pro computers and laptops.

Click Next>


This is the Azure Extension for SQL Server and skip this as we are just looking at the SQL Server on our laptop exclusively.  This is wasn't there in the SQL Server 2012 edition. The Azure Cloud services had just appeared in beta agt that time.

Let us disregard this (do not check any of the boxes) and click Next>.

This takes you to the Feature Selection page as shown.


This is an important step and you should take time to read and understand the items. It describes broadly under Instance FeaturesShared Features and Redistribution Features. Microsoft Reporting Services used to be a  part of the installation in earlier editions of SQL Server. In this edition you can install it using the internet link shown on this screen.

Place a check mark on Database Engine Services. Windows Powershell, a powerful programming is already installed and it used to be a requirement in earlier editions. It comes with Windows 11. We will not be interested in Replication and you skip this one. Machine Learning Services and Language Extensions will be very useful should we use R, Python, Java and other programming languages. With the advent of AI this may become very useful. If you choose this, it will enter the installation queue and will be installed. It would take up 1419 MB of disk space as shown.


Choose the option "Full-Text..."

If you want to connect to non-SQL databases choose the option "Polybase query Service for External Data". I believe, Polybase Query had just appeared in Beta in 2012.

As you choose, items the disk space requirements gets updated as you see in your right side boxes.

For now, we skip Analysis Services. We also skip the whole of Shared Features as well as Redistribution Features. We can always go to the SQL Server Installation Center and modify items later.

At the very bottom, observe that the installation root directory is shown as, C:\Program Files\Microsoft SQL Server\

Click Next>.

It takes you to the Instance Configuration page as shown with a standard Default instance, named MSSQLSERVER. This naming has little changed over the editions. The SQL Server directory is also shown.


If you choose the Named instance by clicking on it. You need to give a name for your SQL Server.  

Let us choose a named instance. Herein, Regency2024 is chosen. The directory gets modified to reflect your choice.


As there are no SQL Servers installed presently on this machine, this being the first,  the Installed Instances does not list any server. 

Click Next>.

It goes to Polybase Configuration as shown. 


It pre-chooses a set of ports to which non-SQL Server databases may be connecting to. We will come to it, when we have to. This page was not there in SQL Server 2012 and Polybase had just made its appearance and was in its initial stages.

Click Next> takes you to the important, Server Configuration page as shown.


In SQL Server 2022, NT Service accounts are virtual accounts used to run SQL Server services. These accounts are automatically managed by Windows and are designed to provide a high level of security and ease of management.

All these do not require user intervention as they are managed by the Windows system. If they are set Manual it suggests they are set manual for resource optimization. They can be Started/Paused/stopped using Windows services. Microsoft recommends a separate account, but herein the default will be used.

Place check mark for "Grant Perform...." You may look at details going to the linked page shown.

You may also review this link: https://learn.microsoft.com/en-us/sql/relational-databases/databases/database-instant-file-initialization?view=sql-server-ver16

Click Next>.

This takes you to the Database Engine Configuration page shown in the next screen,


There are very important items on this page. At the very top there are tabbed pages in this Database Configuration node:

  • Server Configuration (the page presently displayed)
  • Data Directories
  • TempDB
  • MaxDOP
  • Memory
  • FILESTREAM

All bulleted items are configurable although defaults are accepted here.

In the Server Configuration tabbed page one has to choose between Windows authentication mode (default) and a Mixed Mode (SQL Server authentication and Windows authentication) needs to be chosen. Windows authentication is considered to be more secure because it uses Windows security model with all its features like password policies, NTLM protocols, etc. The management is simple. 

Mixed-mode authentication is more flexible. Please go here, for more details, or read my earlier posts.

Let us choose Windows authentication. We would like to Add Current User (the current user is also the one installing the SQL Server).


Click Add Current User. It adds OLIBU\hoden. These are the credentials of the ComputerName\Computer User (windows). You could add, or remove more users. Remember that they should be in the Windows Users list.  If you need to add people to this, you could do it later as well. Since it is windows User, no need to enter password, etc.

Click Next>

It skips everything else and goes to Ready to Install node of the navigation on the left ass shown. If you need to configure the items on this page, you could go to each of the tabbed page and configure. You may read here for more.


Only a part of this screen is shown. The details shows are available in the SQL Server Configuration file, Configuration.ini. When you install, all of the above will be installed.

Click Install.

It starts installing and shows a progress bar. After the installation is complete, this page is displayed.


The installation was successful. A installation log is generated for checking up the details.

Close the Installation center window.








 











 






























Monday, September 9, 2024

Can you Install SQL Server 2022 on a Surface Pro laptop - Part 1?

 This is where you really start looking installing SQL Server 2022 on a laptop, or a computer. This is in the planning stage of the installation. If you are looking at installation of SQL Servers there are a number of posts for other versions here.


It is possible to install SQL Server 2022 on a laptop as long as it satisfies the hardware and software requirements.

These are the hardware requirements from Microsoft site:

ComponentRequirement
StorageSQL Server requires a minimum of 6 GB of available hard drive space.

Disk space requirements vary with the SQL Server components you install. For more information, see Hard Disk Space Requirements later in this article. For information on supported storage types for data files, see Storage Types for Data Files.
MonitorSQL Server requires Super-VGA (800x600) or higher resolution monitor.
InternetInternet functionality requires Internet access (fees can apply).
Memory 1Minimum:

Express Editions: 512 MB

All other editions: 1 GB

Recommended:

Express Editions: 1 GB

All other editions: At least 4 GB and should be increased as database size increases to ensure optimal performance.
Processor SpeedMinimum: x64 Processor: 1.4 GHz

Recommended: 2.0 GHz or faster
Processor Typex64 Processor: AMD Opteron, AMD Athlon 64, Intel Xeon with Intel EM64T support, Intel Pentium IV with EM64T support

1 The minimum memory required for installing the Data Quality Server component in Data Quality Services (DQS) is 2 GB of RAM, which is different from the SQL Server minimum


For example I have a Surface Pro older version with the following specs:

Processor 12th Gen Intel(R) Core(TM) i7-1265U   2.70 GHz

Installed RAM 16.0 GB (15.8 GB usable)

System type 64-bit operating system, x64-based processor

Pen and touch Pen and touch support with 10 touch points

Additionally, the Surface Pro has a display capable of a resolution of 2496 x 1664

This makes it easy to decide that SQL Server 2022 can indeed be run on this laptop. However, we need to allocate 6GB for the database which presently, is more than needed.

The laptop also satisfies the software requirements.

Software Requirements:

  • Operating System: Windows 10 version 1607 or greater, or Windows Server 2016 or greater.
  • .NET Framework: Minimum operating systems include the minimum .NET framework required.
  • Network Software: Supported operating systems for SQL Server have built-in network software.