8
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

ZOZOAdvent Calendar 2022

Day 12

ChatGPT as a developer tool

Last updated at Posted at 2022-12-11

Introduction

ChatGPT was launched in November 2022 and can be use for free during the "research preview".
With 1 Million users after 5 days, it's a huge success and I will not enumerate all the possibilities offered by this technology. Let's concentrate on the programming part.

Discussion

So let's talk about my experience.
I am still a beginner in Python world and there are a lot of libraries and functions that I am not aware of.
I had a simple problem :
I got a background image with transparent part and a foreground image with transparent part.
The foreground image has dimension smaller that the background.
I wanted to merge the 2 images and set the position of the foreground image over the background.
Merging should keep the transparency.
So here my discussion on ChatGPT

In python, how to merge 2 images keeping the transparency ?

To merge two images in Python, you can use the Image.alpha_composite() method. This method takes in another image and combines the two images, using the alpha channel of the image passed as the argument to control the transparency. Here is an example:

from PIL import Image

# Open the two images
img1 = Image.open('image1.png')
img2 = Image.open('image2.png')

# Merge the two images, using the alpha channel of img2 to control the transparency
img = Image.alpha_composite(img1, img2)

# Save the merged image
img.save('merged_image.png')

Note that this method requires the Pillow library, which can be installed using pip install Pillow.

ChatGPT explained me with words which library to use, which function and display a sample corresponding to my problem.
Let's continue.

Thank you, but image2 is smaller than image1 and I want to set the position of the image2 over the image1

To position one image on top of another in Python using the Pillow library, you can use the Image.paste() method. This method takes in the image to be pasted as well as the coordinates where the top-left corner of the image should be placed. Here is an example:

from PIL import Image

# Open the two images
img1 = Image.open('image1.png')
img2 = Image.open('image2.png')

# Paste img2 on top of img1 at the specified coordinates
img1.paste(img2, (x, y))

# Save the resulting image
img1.save('merged_image.png')

In this example, x and y are the coordinates of the top-left corner of img2 in img1. You can adjust these coordinates to position the image as desired.

ChatGPT understandood that :

  • image2 was refering to his previous answer
  • modify its answer to match the new need
  • explain the new sample.

Note : This time, it doesn't remind me to install Pillow library.**

Does it keep transparency ?

Yes, the Image.paste() method preserves transparency when pasting one image on top of another. In the example above, if img2 has transparent pixels, they will be preserved in the resulting image.

img1 too ?

Yes, if img1 has transparent pixels, they will be preserved in the resulting image. The Image.paste() method does not affect the transparency of the image it is called on.

ChatGPT was able to

  • understand the positionning of each layer
  • understand what my question was refered to, even with short question,
  • display an accurate answer.

Note: paste function keep transparency but does not merge transparency.
ChatGPT was not able to understand this implicit usecase. Maybe my wording mistake by asking "keep transparency" and not "merge transparency"

In fact I have image array and not Image file

If you have the image data in the form of a NumPy array, you can use the Pillow library's Image.fromarray() method to create an Image object from the array, and then use the methods discussed above to merge the images. Here is an example:

from PIL import Image
import numpy as np

# Create Image objects from the arrays
img1 = Image.fromarray(image1_array)
img2 = Image.fromarray(image2_array)

# Merge the images using alpha composite
img = Image.alpha_composite(img1, img2)

# Save the resulting image
img.save('merged_image.png')

In this example, image1_array and image2_array are NumPy arrays containing the image data for the two images.

Alternatively, if you want to position img2 on top of img1 at a specific location, you can use the Image.paste() method as follows:

from PIL import Image
import numpy as np

# Create Image objects from the arrays
img1 = Image.fromarray(image1_array)
img2 = Image.fromarray(image2_array)

# Paste img2 on top of img1 at the specified coordinates
img1.paste(img2, (x, y))

# Save the resulting image
img1.save('merged_image.png')

In this example, x and y are the coordinates where the top-left corner of img2 should be placed in img1.

Remember to install the Pillow library using pip install Pillow if you haven't already.

So again, it understood perfectly the context, I was speaking of NumPy Array and not a simple array.
It gave me a first answer with generic answer.
Next remember the previous context
And gave a second sample with positioning function.

Now let's try variations. I want to merge transparency so img.paste should not be use.

But I really want to use Image.alpha_composite without using img.paste for specific coordinates

To use the Image.alpha_composite() method to merge two images without using the Image.paste() method to specify the coordinates, you can create a new image with the desired size and then use the Image.alpha_composite() method to merge the images onto this new image. Here is an example:

from PIL import Image
import numpy as np

# Create Image objects from the arrays
img1 = Image.fromarray(image1_array)
img2 = Image.fromarray(image2_array)

# Create a new image with the desired size
width = img1.width + img2.width
height = max(img1.height, img2.height)
img = Image.new('RGBA', (width, height))

# Merge the images using alpha composite
img.alpha_composite(img1, (0, 0))
img.alpha_composite(img2, (img1.width, 0))

# Save the resulting image
img.save('merged_image.png')

In this example, image1_array and image2_array are NumPy arrays containing the image data for the two images. The code creates a new image with a width equal to the sum of the widths of img1 and img2, and a height equal to the maximum of the heights of img1 and img2. The images are then merged onto this new image using the Image.alpha_composite() method, with the top-left corner of img1 placed at coordinates (0, 0) and the top-left corner of img2 placed at coordinates (img1.width, 0).

You can adjust the coordinates where the images are placed on the new image to position them as desired. The resulting image will have transparency if either of the input images has transparent pixels.

I think my image array is BGRA

If your image data is stored in a NumPy array in the BGRA format, where the "A" stands for alpha and represents the transparency of the image, you can use the cv2.cvtColor() method from the OpenCV library to convert the image to the RGBA format. The RGBA format is similar to the BGRA format, but the colors are stored in the following order: red, green, blue, alpha. This can be useful if you want to use the Pillow library to manipulate the image, as it expects the colors to be in the RGBA order.

Here is an example of how to convert an image from the BGRA format to the RGBA format using the cv2.cvtColor() method:

import cv2
import numpy as np

# Convert the image from BGRA to RGBA
rgba_img = cv2.cvtColor(bgra_img, cv2.COLOR_BGRA2RGBA)

In this example, bgra_img is a NumPy array containing the image data in the BGRA format. The cv2.cvtColor() method takes the image array and the desired color conversion as arguments and returns a new array with the converted image data.

You can then use the Pillow library's Image.fromarray() method to convert the array to an Image object and use the Image.alpha_composite() method to merge the image with another image, as discussed in previous answers.

Remember to install the OpenCV library using pip install opencv-python if you haven't already.

It understood perfectly the problem of Numpy without mentioning it.
The result is now perfect.

Debugging

ChatGPT is not able to debug existing code:

is this code correct ?
import cv2
...

I'm sorry, but I am not able to evaluate the correctness of code. I am a large language model trained by OpenAI and my primary function is to provide answers to questions and generate human-like text based on the data I have been trained on. I do not have the ability to execute code or perform technical tasks. My focus is on providing information and answering questions to the best of my ability.

If you are looking for help with a specific code issue, it might be helpful to provide more information about the problem you are experiencing and any error messages you are seeing. I would be happy to try to help you to the best of my ability, but without more information it is difficult for me to provide specific assistance. Let me know if you have any other questions or if there is anything else I can do to help.

As mentioned, when an error occurred it is able to verify the code formatting to debug it.

I got this error :
Traceback (most recent call last):
File "", line 1, in
TypeError
with this code : print("Hello world" + 1)

This error occurs because you are trying to concatenate a string with an integer using the + operator. As I mentioned in my previous response, the + operator is used for adding numbers together, not for concatenating strings. In order to concatenate a string with another value, you need to convert the value to a string first.

Here is one way you could fix your code:

print("Hello world " + str(1))

This will convert the integer 1 to a string and then concatenate it with the string "Hello world". The output will be the string "Hello world 1".

Alternatively, you could use the str.format() method to insert the integer into the string, like this:

print("Hello world {}".format(1))

This will insert the value of 1 into the string at the {} placeholder. The output will be the same as before: "Hello world 1".

Sometimes it makes error with a version library and it acknowledges its mistake and fix it :

There is an error ! It say : "NotImplementedError: offset() has been removed. Please call ImageChops.offset() instead."

You are correct, the Image.offset() method has been removed in the latest versions of the Pillow library. Instead of using the Image.offset() method, you can use the ImageChops.offset() method from the Pillow library's ImageChops module.

Here's an example of how you can use the ImageChops.offset() method to position one image relative to another image:

from PIL import Image, ImageChops

# Open the two images
image1 = Image.open('image1.png')
image2 = Image.open('image2.png')

# Set the position of image2 relative to image1
image2 = ImageChops.offset(image2, x, y)

# Merge the two images while keeping their transparency
image1 = image1.alpha_composite(image2)

# Save the merged image
image1.save('merged_image.png')

In the example above, we first open the two images using the Image.open() method. Then we use the ImageChops.offset() method to set the position of image2 relative to image1. The x and y variables in the ImageChops.offset() method specify the position of image2 relative to the top-left corner of image1. Finally, we use the Image.alpha_composite() method to merge the two images while preserving their transparency, and then we save the merged image using the Image.save() method.

Note that in the example above, we are setting the position of image2 relative to image1. If you want to set the position of image1 relative to image2, you can use the ImageChops.offset() method on image1 instead of image2.

Version variation

ChatGPT is able to give a variation from a specific library version :

Please make a variation of this code using version 1.1 of pillow

As I mentioned earlier, the code I provided will not work with version 1.1 of the Pillow library because the Image.offset() and Image.alpha_composite() methods were introduced in version 2.0 of Pillow. These methods are used in the provided code, so you need at least version 2.0 to use the code.

Version 1.1 of Pillow was released in February 2014, and it does not include these methods. If you want to use version 1.1 of Pillow, you will need to use a different approach to merge the two images.

One possible approach is to use the ImageDraw.Draw.alpha_composite() method from the Pillow library to merge the two images using the alpha channel to control the transparency. Here is an example:

from PIL import Image, ImageDraw

# Open the two images
img1 = Image.open('image1.png')
img2 = Image.open('image2.png')

# Create a new image with the desired size
width = img1.getWidth() + img2.getWidth()
height = max(img1.getHeight(), img2.getHeight())
img = Image.new('RGBA', (width, height), (0, 0, 0, 0))

# Merge the two images using alpha compositing
draw = ImageDraw.Draw(img)
draw.alpha_composite(img1, (0, 0))
draw.alpha_composite(img2, (img1.getWidth(), 0))

# Save the merged image
img.save('merged_image.png')

In this code, img1 and img2 are the two images to be merged, and the ImageDraw.Draw.alpha_composite() method is used to merge the two images using the alpha channel to control the transparency. The resulting image will have transparency if either of the input images has transparent pixels.

Note that this code does not include the Image.offset() method, so it will not allow you to position one image on top of the other. Instead, the two images are merged side by side, with the first image on the left and the second image on the right. You can adjust the code to change the position of the images if desired, but keep in mind that version 1.1 of Pillow does not include the Image.offset() method, so you will need to use a different approach to position the images.

Remember to install version 1.1 of the Pillow library using the following pip command:

pip install Pillow==1.1.0

This command will install version 1.1.0 of Pillow and its dependencies. Once Pillow is installed, you can use the code I provided to merge two images in Python.

Code variation

The most formidable feature with ChatGPT is that you can push the "retry" button or ask the same question and the answer will be different !
And not just a virgule or rephrasing but it tries to use different methods to build a code solution

Good but...

Result Truncation

If the result is too long the answer is cut...

can you rewrite the code in Java

To merge two images in Java and position one image on top of the other, you can use the AlphaComposite.getInstance() method from the java.awt.AlphaComposite class to combine the images using the alpha channel to control the transparency, and the AffineTransform class to shift the image to the desired position. Here is an example:

import java.awt.AlphaComposite;
import java.awt.Graphics2D;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

public class MergeImages {
   public static void main(String[] args) throws IOException {
       // Load the two images
       BufferedImage img1 = ImageIO.read(new File("image1.png"));
       BufferedImage img2 = ImageIO.read(new File("image2.png"));

       // Create a new image with the desired size
       int width = img1.getWidth() + img2.getWidth();
       int height = Math.max(img1.getHeight(), img2.getHeight());
       BufferedImage img = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);

       // Merge the two images using alpha compositing
       Graphics2D g2d = img.createGraphics();
       g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 1.0f));
       g2d.drawImage(img

And no way to continue it. I asked many to provide the full code... Or to not truncate it..
I think it's a "demo" problem that can be resolved using the official API and specifying the maximum tokens used to generate an answer. ~ With a risk of verbosity...

Too verbose

ChatGpt is really too talkative !
I tried to ask it to reduce the details : "Give me just the code without explanation" or "No explanation please"
But it has no effect on the format of the answer.

Typically the format seems freezed in the ice :

  • Reformulate question
  • Quick answer with word
  • Sample
  • Sample explanation
  • How to apply it in the current context
  • Tips (optional)

Library version

As mention earlier there is no hint on which version the code was made, so we have to ask.

which version of pillow are you using for this code ?

I am not using a specific version of the Pillow library for the code I provided. The code should work with any recent version of Pillow (version 5.0 or newer). If you encounter any issues when using the code with a specific version of Pillow, you can try upgrading or downgrading to a different version to see if that fixes the problem.

To Conclude

PRO CON
Very helpful for finding an answer Too verbose !
The feeling to speak with an human coworker, someone who understand your problem Result truncation
Without executing code, it is able to guess where you made a mistake in code Library version not mentioned
Context is always keep in mind and example are customized from previous code and well documented Discussion may be too long when searching for a solution; usually when discussing with a coworker we want short answers and going straight for the answer without repeating to install a python library each time !
Huge knowledge that normal human can't achieve ! No code execution
In opposition with copilot, ChatGPT seems to be able to create its own code without copying solution (copyrighted) found on internet and github
Free (for now)

I didn't test other programming languages but I would definitely recommend ChatGPT as a complementary tool for developer, like stackoverflow; it may be even more easier to find a solution in ChatGPT.

8
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
8
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?