Godot Version
Godot 4.2.2
OS: Mac 14.5 (23F79)
Chip: Apple M2 Pro
Question
I tried to dynamically create image and save to jpg
var user_data_dir = OS.get_user_data_dir()
print("User data directory:", user_data_dir)
# Create a new image with specified width, height, and format
var image = Image.new()
image.create(256, 256, false, Image.FORMAT_RGB8) # Creating a 256x256 image with RGB format
# Fill the image with black
image.fill(Color.BLUE) # Fill the entire image with black color
image.fill_rect(Rect2(0, 0, 20, 20), Color.RED)
# Check if the image is empty (filled it with black)
var is_empty = true
for y in range(image.get_height()):
for x in range(image.get_width()):
var pixel_color = image.get_pixel(x, y)
if pixel_color != Color(0, 0, 0, 1): # Check against black color with alpha 1
is_empty = false
break
if not is_empty:
break
if is_empty:
print("The image is empty.")
else:
print("The image is not empty.")
# Save the image as a JPG file
var file_path_png = user_data_dir + "/black_image2.jpg"
var result_png = image.save_jpg(file_path_png)
if result_png == OK:
print("JPG saved successfully at: ", file_path_png)
else:
print("Failed to save JPG. Error code: ", str(result_png))
# Verify if the file exists
var png_exists = FileAccess.file_exists(file_path_png)
print("JPG exists: ", str(png_exists))
Log:
User data directory:/Users/xxxxx/Library/Application Support/Godot/app_userdata/gameproject
The image is empty.
Failed to save JPG. Error code: 31
JPG exists: true
Its not working why?
I tried to create image format RGB8, fill with color and save that image that’s it why the image is empty and that file is saved but file is empty.
Note: I’ve been learning Godot for the past two weeks after switching from Unity, so if this is a newbie question, please bear with me. Thanks!